用硬编码的元素初始化 std: : vector 最简单的方法是什么?

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 similarly elegant?

The best way I know is:

std::vector<int> ints;

ints.push_back(10);
ints.push_back(20);
ints.push_back(30);

Is there a better way?

转载于:https://stackoverflow.com/questions/2236197/what-is-the-easiest-way-to-initialize-a-stdvector-with-hardcoded-elements

csdnceshi71
Memor.の Not exactly what is asked, but if you need to initialize the vector with just one value, you can use vector::assign() as well.
接近 2 年之前 回复
weixin_41568183
零零乙 This is called list initialization.
接近 2 年之前 回复
csdnceshi53
Lotus@ Changed the title to make this explicitly a C++03 question. It seemed easier than going through and fixing all the answers to make sense with the new standard C++.
6 年多之前 回复
csdnceshi68
local-host tr1::array is useful because ordinary arrays don't provide the interface of STL containers
10 年多之前 回复
weixin_41568208
北城已荒凉 you have me curious... if I needed fixed size, could I not use plain old arrays themselves? Looking at tr1 array right now...
10 年多之前 回复
weixin_41568184
叼花硬汉 if you are not going to change the size of ints after initialization, consider using tr1 array.
10 年多之前 回复

23个回答

One method would be to use the array to initialize the vector

static const int arr[] = {16,2,77,29};
vector<int> vec (arr, arr + sizeof(arr) / sizeof(arr[0]) );
csdnceshi53
Lotus@ A solution involving an auxiliary variable, sizeof() and multiple arithmetic operations including a division is anything but "the easiest way". -1.
3 年多之前 回复
csdnceshi51
旧行李 Doesn't this imply that two duplicate copies of the integers are now being stored in memory? Is there at least a way to destroy the static const int arr[] afterward to free up that memory? Even without the static keyword, a global variable would remain allocated for the entirety of the program. This could become an issue when dealing with many/large vectors of doubles, strings, etc.
大约 4 年之前 回复
csdnceshi62
csdnceshi62 Sometimes you need to modify the resulting vector. For example, you may need to always have some default parameters and sometimes add a few customized to them.
6 年多之前 回复
csdnceshi52
妄徒之命 When you pass an array into a function by pointer, you should always pass the size of the array as well, unless the function infers the size of the array from the array contents (e.g. if the last element contains terminating value etc.). That's why C++ has std::vector, so passing it by reference will solve your problem.
7 年多之前 回复
csdnceshi59
ℙℕℤℝ Actually ... the sizeof trick works just fine until I pass the array into a function. Then it seems to not work any more. Is there a way around that?
7 年多之前 回复
csdnceshi59
ℙℕℤℝ It seems that if I use static const string attr[]={"abc", "def","ghi"} that I can't use sizeof to get the array length. It also doesn't seem to work for arrays of char *'s. Am I doing something wrong?
7 年多之前 回复
csdnceshi66
必承其重 | 欲带皇冠 Not only is this method easy and standalone without any extra requirements, but it can also be made forwards-compatible with C++0x and their container initialization lists with the help of a macro, which eases syntax and portability a lot. +1.
接近 8 年之前 回复
weixin_41568174
from.. sizeof(array) is one of the few exceptions that allows to get the total size of elements of the array and NOT the arr pointer dimension. So basically he's using vector(pointer_to_first_element, pointer_to_first_element + size_in_bytes_of_the_whole_array / size_of_one_element) that is: vector(pointer_to_first_element, pointer_after_final_element). The type is already given with the <int>, so the vector knows how much is one element. Remember that iterators can be treated as pointers so you're basically using the vector(iterator begin, iterator end) constructor
接近 8 年之前 回复
csdnceshi68
local-host Can you explain why you're using those parameters when defining the vec vector.
8 年多之前 回复
csdnceshi63
elliott.david I didn't downvoate this, but I was tempted. Mainly because this saves you almost nothing over just using the initialized array in the first place. However, that's really C++'s fault, not yours.
9 年多之前 回复
csdnceshi78
程序go It will work fine without static or const, however they both make it more explicit as to how it should be used and allow the compiler to make additional optimizations.
10 年多之前 回复

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.

csdnceshi67
bug^君 You can just initialise it, and it will be empty: std::vector<T> vector;
2 年多之前 回复
csdnceshi63
elliott.david Since vectors are self-sizing, would it be ok to initialize it as empty too? Like in the constructor: this->vect = {}; ?
2 年多之前 回复

you can do that using boost::assign.

vector<int> values;  
values += 1,2,3,4,5,6,7,8,9;

detail here

csdnceshi68
local-host I haven't seen a worse case of operator overloading abuse in a long time. Does the += there tack on 1,2,3,4.. to the end of values, or does it add 1 to the 1st element, 2 to the 2nd element, 3 to the 3rd element (as syntax like this should in MATLAB-like languages)
6 年多之前 回复

A more recent duplicate question has this answer by Viktor Sehr. For me, it is compact, visually appealing (looks like you are 'shoving' the values in), doesn't require c++11 or a third party module, and avoids using an extra (written) variable. Below is how I am using it with a few changes. I may switch to extending the function of vector and/or va_arg in the future intead.


// Based on answer by "Viktor Sehr" on Stack Overflow
// https://stackoverflow.com/a/8907356
//
template <typename T>
class mkvec {
public:
    typedef mkvec<T> my_type;
    my_type& operator<< (const T& val) {
        data_.push_back(val);
        return *this;
    }
    my_type& operator<< (const std::vector<T>& inVector) {
        this->data_.reserve(this->data_.size() + inVector.size());
        this->data_.insert(this->data_.end(), inVector.begin(), inVector.end());
        return *this;
    }
    operator std::vector<T>() const {
        return data_;
    }
private:
    std::vector<T> data_;
};

std::vector<int32_t>    vec1;
std::vector<int32_t>    vec2;

vec1 = mkvec<int32_t>() << 5 << 8 << 19 << 79;  
// vec1 = (5,8,19,79)
vec2 = mkvec<int32_t>() << 1 << 2 << 3 << vec1 << 10 << 11 << 12;  
// vec2 = (1,2,3,5,8,19,79,10,11,12)

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!

If the array is:

int arr[] = {1, 2, 3};
int len = (sizeof(arr)/sizeof(arr[0])); // finding length of array
vector < int > v;
std:: v.assign(arr, arr+len); // assigning elements from array to vector 

"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.

csdnceshi58
Didn"t forge In the similar lines , I am trying to initialise the map, std::map<int, bool> catinfo = { {1, false} }; But then get this error error: in C++98 'catinfo' must be initialized by constructor, not by '{...}'
接近 7 年之前 回复

Just thought I'd toss in my $0.02. I tend to declare this:

template< typename T, size_t N >
std::vector<T> makeVector( const T (&data)[N] )
{
    return std::vector<T>(data, data+N);
}

in a utility header somewhere and then all that's required is:

const double values[] = { 2.0, 1.0, 42.0, -7 };
std::vector<double> array = makeVector(values);

But I can't wait for C++0x. I'm stuck because my code must also compile in Visual Studio. Boo.

csdnceshi57
perhaps? Can you explain the const T (&data)[N] part? How is the size of the array deduced in your call makeVector(values)?
5 年多之前 回复
csdnceshi68
local-host This technique can also be used to overload a function to accept an array with typed size.
接近 8 年之前 回复

For God's sake, use the modern C++[11,14,17,...] way:

std::vector<int> vec = {10,20,30};

The old way of looping over a variable-length array or using sizeof() is truly terrible on the eyes and completely unnecessary in terms of mental overhead. Yuck.

csdnceshi63
elliott.david In fairness, this was originally a C++03 question, but I hope that people/companies adopt the new standards. C++ still needs a variable-length array (VLA) implementation in the standard library similar to what is available in Eigen and Boost.
接近 2 年之前 回复

In C++11:

static const int a[] = {10, 20, 30};
vector<int> vec (begin(a), end(a));
csdnceshi74
7*4 Actually I had an incoming int[] (some C lib) and wanted to push into a vector (C++ lib). This answer helped, the rest didn't ;-)
大约 5 年之前 回复
weixin_41568131
10.24 If you're using C++11 already, you may as well go for the direct approach - vector<int> arr = {10, 20, 30};.
5 年多之前 回复
共23条数据 1 3 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐