Possible Duplicates:
C++: Easiest way to initialize an STL vector with hardcoded elements Using STL Allocator with STL Vectors
out of curiosity i want to know开发者_JS百科 quick ways of initializing vectors
i only know this
double inputar[]={1,0,0,0};
vector<double> input(inputar,inputar+4);
This is IMHO one of the failings of the current C++ standard. Vector makes a great replacement for C arrays, but initializing one is much more of a PITA.
The best I have heard of is the Boost assignment package. According to the docs, you can do this 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 );
}
精彩评论