How to fill a vector with non-trivial initial values?

后端 未结 6 1479
一向
一向 2021-02-05 12:38

I know how to fill an std::vector with non-trivial initial values, e.g. sequence numbers:

void IndexArray( unsigned int length, std::vector&a         


        
相关标签:
6条回答
  • 2021-02-05 13:18

    There is also a iota() function in adobe.ASL, (and a value_iterator as well). In boost, there is a counting_iterator, and I suspect a few other ways to do generate number sequences on the fly in boost.

    0 讨论(0)
  • 2021-02-05 13:25

    I usually go with std::generate plus a simple generator:

    template <typename T>
    struct gen {
        T x;
        gen(T seed) : x(seed) { }
    
        T operator ()() { return x++; }
    };
    
    generate(a.begin(), a.end(), gen<int>(0));
    
    0 讨论(0)
  • 2021-02-05 13:26

    You can use the generate algorithm, for a more general way of filling up containers:

    #include <iostream>
    #include <algorithm>
    #include <vector>
    
    struct c_unique {
       int current;
       c_unique() {current=0;}
       int operator()() {return ++current;}
    } UniqueNumber;
    
    
    int main () {
      vector<int> myvector (8);
      generate (myvector.begin(), myvector.end(), UniqueNumber);
    
      cout << "\nmyvector contains:";
      for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
    
      return 0;
    }
    

    This was shamelessly lifted and edited from cplusplusreference.

    0 讨论(0)
  • 2021-02-05 13:30

    If you're using SGI STL (or a derivative, such as STLPort), you can use iota. :-)

    void IndexArray(unsigned int length, vector<unsigned int>& v)
    {
        vector<unsigned int>(length).swap(v);
        iota(v.begin(), v.end(), 0);
    }
    
    0 讨论(0)
  • 2021-02-05 13:32

    I know this has already been answered, but I prefer the "fill" function in the algorithm library, since it's seems more intuitive for me to read:

    // fill algorithm example
    #include <iostream>
    #include <algorithm>
    #include <vector>
    using namespace std;
    
    int main () {
      vector<int> myvector (8);                       // myvector: 0 0 0 0 0 0 0 0
    
      fill (myvector.begin(),myvector.begin()+4,5);   // myvector: 5 5 5 5 0 0 0 0
      fill (myvector.begin()+3,myvector.end()-2,8);   // myvector: 5 5 5 8 8 8 0 0
    
      cout << "myvector contains:";
      for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
        cout << " " << *it;
    
      cout << endl;
    
      return 0;
    }
    

    This too was also shamelessly lifted from cplusplusreference.

    0 讨论(0)
  • 2021-02-05 13:32

    If you have a C style array you can use std:copy, e.g.,

    int c_array[] = {3,4,5};
    
    const int* pbegin = &c_array[0];
    const size_t c_array_size = sizeof(c_array) / sizeof(c_array[0]);
    const int* pend  = pbegin + c_array_size;
    
    std::vector<int> v;
    v.reserve(c_array_size);
    std::copy(pbegin, pend, std:back_inserter(v));
    
    0 讨论(0)
提交回复
热议问题