【发布时间】:2010-10-24 00:56:16
【问题描述】:
使用 C++ 和 STL,有人知道如何将整数数组作为节点存储在 STL 列表或向量中吗?我需要存储未知数量的数字对,并且来自其他语言,我的第一个想法是使用某种类似列表或向量的数据结构......但我遇到了一些麻烦。我 100% 确定我犯了一个明显的初学者 C++ 错误,并且真正了解该语言的人会看一看我正在尝试做的事情并能够让我明白。
所以,这就是我尝试过的。像这样声明一个列表是可行的:
stl::list<int[2]> my_list;
然后我可以轻松地制作一个二元素数组,如下所示:
int foo[2] = {1,2};
这编译并运行得很好。但是,一旦我尝试将foo 添加到我的列表中,就像这样:
my_list.push_back(foo);
我得到了一整套编译器错误,我都没有真正理解(我的 C++-fu 几乎不存在):
/usr/include/c++/4.0.0/ext/new_allocator.h: In member function ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = int [2]]’:
/usr/include/c++/4.0.0/bits/stl_list.h:440: instantiated from ‘std::_List_node<_Tp>* std::list<_Tp, _Alloc>::_M_create_node(const _Tp&) [with _Tp = int [2], _Alloc = std::allocator<int [2]>]’
/usr/include/c++/4.0.0/bits/stl_list.h:1151: instantiated from ‘void std::list<_Tp, _Alloc>::_M_insert(std::_List_iterator<_Tp>, const _Tp&) [with _Tp = int [2], _Alloc = std::allocator<int [2]>]’
/usr/include/c++/4.0.0/bits/stl_list.h:773: instantiated from ‘void std::list<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = int [2], _Alloc = std::allocator<int [2]>]’
test.cpp:5: instantiated from here
/usr/include/c++/4.0.0/ext/new_allocator.h:104: error: ISO C++ forbids initialization in array new
那么,有人知道我在这里做错了什么吗?任何指针(没有双关语)都是最有帮助的。是否不可能将数组存储在 std::list 中?我应该使用结构吗?我只是在某处错过了* 或& 吗?
【问题讨论】: