Fast C ++ Vector Initialization

Possible duplicates:
C ++: The easiest way to initialize a STL vector with hard-coded elements
Using STL Allocator with STL vectors

Out of curiosity, I want to know quick ways to initialize vectors

I only know this

double inputar[]={1,0,0,0};
vector<double> input(inputar,inputar+4);
+3
source share
1 answer

This IMHO is one of the drawbacks of the current C ++ standard. Vector makes a big replacement for C-arrays, but initializing one of them is much larger than PITA.

The best I've heard is the Boost destination package . According to the docs, you can do it with it:

#include <boost/assign/std/vector.hpp> // for 'operator+=()'
#include <boost/assert.hpp>; 
using namespace std;
using namespace boost::assign; // bring 'operator+=()' into scope

{
    vector<int> values;  
    values += 1,2,3,4,5,6,7,8,9; // insert values at the end of the container
    BOOST_ASSERT( values.size() == 9 );
    BOOST_ASSERT( values[0] == 1 );
    BOOST_ASSERT( values[8] == 9 );
}
+3
source

Source: https://habr.com/ru/post/1733196/


All Articles