简体   繁体   中英

How to push_back multiple values into a vector?

I know this question has been asked before, and I know that in C++11 you can do

vector<int> v = {2,5,8,11,14};
vector<int> v{2,5,8,11,14};

and

v.push_back({x,y});

But it gives me a compile error. I'm using Visual Studio Express 2012.

How do I accomplish this?

EDIT: error screenshot attached:

在此处输入图片说明

Visual Studio 2012 does not support vector initialization via initializer lists . There is a lot of C++11 support missing from the standard library included with VS2012 that is supported by the VS2012 C++ compiler itself.

Sadly, as is the case for VS2012 and was the case for gcc 4.7, awesome compiler support for the new C++11 features is hampered by partial library support which seems to always lag behind the compiler.

在拥有支持向量初始化程序列表的编译器之前,可以使用boost :: assign :: list_of

Using this compiler and it's standard libraries, as previously stated by @Michael Goldshteyn can't be done. But if You're willing to include boost libraries, You can use code like this:

#include <boost/assign/std/vector.hpp>

using namespace boost::assign;

{
    std::vector< int > myElements;
    myElements += 1,2,3,4,5;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM