开发者

quick vector initialization c++ [duplicate]

开发者 https://www.devze.com 2022-12-20 05:44 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicates: C++: Easiest way to initialize an STL vector with hardcoded elements
This question already has answers here: Closed 12 years ago.

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 );
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号