What is the easiest way to initialize a std::vector with hardcoded elements?

后端 未结 29 2652
终归单人心
终归单人心 2020-11-22 05:07

I can create an array and initialize it like this:

int a[] = {10, 20, 30};

How do I create a std::vector and initialize it sim

相关标签:
29条回答
  • 2020-11-22 05:38

    The simplest, ergonomic way (with C++ 11 or later):

    auto my_ints = {1,2,3};
    
    0 讨论(0)
  • 2020-11-22 05:40

    If you want something on the same general order as Boost::assign without creating a dependency on Boost, the following is at least vaguely similar:

    template<class T>
    class make_vector {
        std::vector<T> data;
    public:
        make_vector(T const &val) { 
            data.push_back(val);
        }
    
        make_vector<T> &operator,(T const &t) {
            data.push_back(t);
            return *this;
        }
    
        operator std::vector<T>() { return data; }
    };
    
    template<class T> 
    make_vector<T> makeVect(T const &t) { 
        return make_vector<T>(t);
    }
    

    While I wish the syntax for using it was cleaner, it's still not particularly awful:

    std::vector<int> x = (makeVect(1), 2, 3, 4);
    
    0 讨论(0)
  • 2020-11-22 05:41

    For vector initialisation -

    vector<int> v = {10,20,30}
    

    can be done if you have C++11 compiler.

    Else, you can have an array of the data and then use a for loop.

    int array[] = {10,20,30}
    for(unsigned int i=0; i<sizeof(array)/sizeof(array[0]); i++)
    {
         v.push_back(array[i]);
    }
    

    Apart from these, there are various other ways described above using some code. In my opinion, these ways are easy to remember and quick to write.

    0 讨论(0)
  • 2020-11-22 05:41

    There are a lot of good answers here, but since I independently arrived at my own before reading this, I figured I'd toss mine up here anyway...

    Here's a method that I'm using for this which will work universally across compilers and platforms:

    Create a struct or class as a container for your collection of objects. Define an operator overload function for <<.

    class MyObject;
    
    struct MyObjectList
    {
        std::list<MyObject> objects;
        MyObjectList& operator<<( const MyObject o )
        { 
            objects.push_back( o );
            return *this; 
        }
    };
    

    You can create functions which take your struct as a parameter, e.g.:

    someFunc( MyObjectList &objects );
    

    Then, you can call that function, like this:

    someFunc( MyObjectList() << MyObject(1) <<  MyObject(2) <<  MyObject(3) );
    

    That way, you can build and pass a dynamically sized collection of objects to a function in one single clean line!

    0 讨论(0)
  • 2020-11-22 05:41

    It is pretty convenient to create a vector inline without defining variable when writing test, for example:

    assert(MyFunction() == std::vector<int>{1, 3, 4}); // <- this.
    
    0 讨论(0)
  • 2020-11-22 05:43

    I build my own solution using va_arg. This solution is C++98 compliant.

    #include <cstdarg>
    #include <iostream>
    #include <vector>
    
    template <typename T>
    std::vector<T> initVector (int len, ...)
    {
      std::vector<T> v;
      va_list vl;
      va_start(vl, len);
      for (int i = 0; i < len; ++i)
        v.push_back(va_arg(vl, T));
      va_end(vl);
      return v;
    }
    
    int main ()
    {
      std::vector<int> v = initVector<int> (7,702,422,631,834,892,104,772);
      for (std::vector<int>::const_iterator it = v.begin() ; it != v.end(); ++it)
        std::cout << *it << std::endl;
      return 0;
    }
    

    Demo

    0 讨论(0)
提交回复
热议问题