我得到两个变量 a
和 b
并想将它们直接添加到数组列表中。我怎样才能避免定义另一个数组,这个数组之后会被插入列表?
我正在搜索类似于以 //
开头的行的结构。
最小的例子:
#include <list>
#include <boost/array.hpp>
using namespace std;
int main()
{
cout << endl;
/* given values */
int a = 1;
int b = 2;
/* arrayList contains arrays of integers. */
list<boost::array<int, 2> > arrayList;
/* item to add values */
boost::array<int, 2> item;
item[0] = a;
item[1] = b;
arrayList.push_back(item);
// arrayList.push_back({{a, b}});
cout << arrayList.front()[0] << ", " << arrayList.front()[1] << endl;
return 0;
}
我的 g++ 版本如下:
gcc-版本 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
。
编译(在 Eclipse 中)抛出此警告,列为错误:
警告:erweiterte Initialisierungsliste nur mit -std=c++0x oder -std=gnu++0x verfügbar [standardmäßig aktiviert] main.cpp/testArrayListAndFind line 24 C/C++ Problem
这意味着:扩展的初始化列表只有...或...可用。
最佳答案
如果您不支持 C++11,您可以使用 boost::assign::list_of
:
arrayList.push_back(boost::assign::list_of(1)(2));
关于C++:如何将数组的元素推送到数组列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10905897/