[英]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 我知道这个问题以前曾被问过,而且我知道在C ++ 11中,您可以
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.
我正在使用Visual Studio Express 2012。
How do I accomplish this? 我该如何完成?
EDIT: error screenshot attached: 编辑:错误截图附:
Visual Studio 2012 does not support vector initialization via initializer lists . Visual Studio 2012 不支持通过初始化程序列表进行矢量初始化 。 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.
VS2012随附的标准库缺少很多C ++ 11支持,而VS2012 C ++编译器本身支持该库。
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. 遗憾的是,就像VS2012和gcc 4.7一样,对新C ++ 11功能的出色编译器支持受到部分库支持的阻碍,该支持似乎总是落后于编译器。
在拥有支持向量初始化程序列表的编译器之前,可以使用boost :: assign :: list_of
Using this compiler and it's standard libraries, as previously stated by @Michael Goldshteyn can't be done. 如@Michael Goldshteyn先前所述,无法使用此编译器及其标准库。 But if You're willing to include boost libraries, You can use code like this:
但是,如果您愿意包含Boost库,则可以使用如下代码:
#include <boost/assign/std/vector.hpp>
using namespace boost::assign;
{
std::vector< int > myElements;
myElements += 1,2,3,4,5;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.