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

后端 未结 29 2651
终归单人心
终归单人心 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:47

    If your compiler supports C++11, you can simply do:

    std::vector<int> v = {1, 2, 3, 4};
    

    This is available in GCC as of version 4.4. Unfortunately, VC++ 2010 seems to be lagging behind in this respect.

    Alternatively, the Boost.Assign library uses non-macro magic to allow the following:

    #include <boost/assign/list_of.hpp>
    ...
    std::vector<int> v = boost::assign::list_of(1)(2)(3)(4);
    

    Or:

    #include <boost/assign/std/vector.hpp>
    using namespace boost::assign;
    ...
    std::vector<int> v;
    v += 1, 2, 3, 4;
    

    But keep in mind that this has some overhead (basically, list_of constructs a std::deque under the hood) so for performance-critical code you'd be better off doing as Yacoby says.

    0 讨论(0)
  • If your compiler supports Variadic macros (which is true for most modern compilers), then you can use the following macro to turn vector initialization into a one-liner:

    #define INIT_VECTOR(type, name, ...) \
    static const type name##_a[] = __VA_ARGS__; \
    vector<type> name(name##_a, name##_a + sizeof(name##_a) / sizeof(*name##_a))
    

    With this macro, you can define an initialized vector with code like this:

    INIT_VECTOR(int, my_vector, {1, 2, 3, 4});
    

    This would create a new vector of ints named my_vector with the elements 1, 2, 3, 4.

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

    There are various ways to hardcode a vector, i will share few ways:

    1. Initializing by pushing values one by one
    // Create an empty vector 
        vector<int> vect;  
    
        vect.push_back(10); 
        vect.push_back(20); 
        vect.push_back(30); 
    
    1. Initializing like arrays
    vector<int> vect{ 10, 20, 30 };
    
    1. Initializing from an array
        int arr[] = { 10, 20, 30 }; 
        int n = sizeof(arr) / sizeof(arr[0]); 
    
        vector<int> vect(arr, arr + n); 
    
    1. Initializing from another vector
        vector<int> vect1{ 10, 20, 30 }; 
    
        vector<int> vect2(vect1.begin(), vect1.end()); 
    
    0 讨论(0)
  • 2020-11-22 05:48
    // Before C++11
    // I used following methods:
    
    // 1.
    int A[] = {10, 20, 30};                              // original array A
    
    unsigned sizeOfA = sizeof(A)/sizeof(A[0]);           // calculate the number of elements
    
                                                         // declare vector vArrayA,
    std::vector<int> vArrayA(sizeOfA);                   // make room for all
                                                         // array A integers
                                                         // and initialize them to 0 
    
    for(unsigned i=0; i<sizeOfA; i++)
        vArrayA[i] = A[i];                               // initialize vector vArrayA
    
    
    //2.
    int B[] = {40, 50, 60, 70};                          // original array B
    
    std::vector<int> vArrayB;                            // declare vector vArrayB
    for (unsigned i=0; i<sizeof(B)/sizeof(B[0]); i++)
        vArrayB.push_back(B[i]);                         // initialize vArrayB
    
    //3.
    int C[] = {1, 2, 3, 4};                              // original array C
    
    std::vector<int> vArrayC;                            // create an empty vector vArrayC
    vArrayC.resize(sizeof(C)/sizeof(C[0]));              // enlarging the number of 
                                                         // contained elements
    for (unsigned i=0; i<sizeof(C)/sizeof(C[0]); i++)
         vArrayC.at(i) = C[i];                           // initialize vArrayC
    
    
    // A Note:
    // Above methods will work well for complex arrays
    // with structures as its elements.
    
    0 讨论(0)
  • 2020-11-22 05:48

    "How do I create an STL vector and initialize it like the above? What is the best way to do so with the minimum typing effort?"

    The easiest way to initialize a vector as you've initialized your built-in array is using an initializer list which was introduced in C++11.

    // Initializing a vector that holds 2 elements of type int.
    Initializing:
    std::vector<int> ivec = {10, 20};
    
    
    // The push_back function is more of a form of assignment with the exception of course
    //that it doesn't obliterate the value of the object it's being called on.
    Assigning
    ivec.push_back(30);
    

    ivec is 3 elements in size after Assigning (labeled statement) is executed.

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