C++:如何将数组的元素推送到数组列表中?

标签 c++ list

我得到两个变量 ab 并想将它们直接添加到数组列表中。我怎样才能避免定义另一个数组,这个数组之后会被插入列表?

我正在搜索类似于以 // 开头的行的结构。

最小的例子:

#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/

相关文章:

c++ - std::time_point往返于std::string

list - 如何删除序言中的子列表?

java - AbstractList类的removeRange方法在子类中有什么用

python - 如何优化列表操作? (代码之战)

c++ - 如何以编程方式迭代所有 CMake 目标?

c++ - 对于 Visual Studio Community 2015,我可以使用 C++ 的代码覆盖功能吗?

python - 高效查找列表中的重复项

list - 将三个列表组合成 Haskell 中的三元组列表

c++ - 如果第一个相同,则按第二个排序插入成对的 STL 集合

c++ - 作为函数调用的一部分创建的临时对象何时被销毁?