如何将数组存储在 STL 列表中? [英] How do I store arrays in an STL list?

查看:21
本文介绍了如何将数组存储在 STL 列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 C++ 和 STL,有人知道如何将整数数组存储为 STL 列表或向量中的节点吗?我需要存储未知数量的数字对,并且来自其他语言,我的第一个想法是使用某种类似列表或向量的数据结构......但我遇到了一些麻烦.我 100% 肯定我犯了一个明显的初学者 C++ 错误,并且真正了解该语言的人会看一看我正在尝试做的事情并能够让我直截了当.

Using C++ and the STL, does anybody know how to store integer arrays as nodes in an STL list or vector? I have an unknown number of pairs of numbers that I need to store, and coming from other languages my first thought was to use some sort of list- or vector-like data structure... but I'm running into some trouble. I am 100% sure that I'm making an obvious beginner's C++ mistake, and that somebody who actually knows the language will take one look at what I'm trying to do and be able to set me straight.

所以,这就是我尝试过的.像这样声明一个列表是有效的:

So, here's what I've tried. Declaring a list like so works:

stl::list<int[2]> my_list;

然后我可以轻松地创建一个二元素数组,如下所示:

And then I can easily make a two-element array, like so:

int foo[2] = {1,2};

这编译并运行得很好.但是,一旦我尝试将 foo 添加到我的列表中,就像这样:

This compiles and runs just fine. However, as soon as I try to add foo to my list, like so:

my_list.push_back(foo);

我收到了一整套复杂的编译器错误,我没有真正理解这些错误(我的 C++-fu 几乎不存在):

I get a whole gnarly set of compiler errors, none of which I really understand (my C++-fu is almost non-existent):

/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 中?我应该使用结构吗?我只是在某处缺少 *& 吗?

So, anybody have ideas as to what I'm doing wrong here? Any pointers (no pun intended) would be most helpful. Is it just not possible to store arrays in a std::list? Should I be using a struct? Am I just missing a * or & somewhere?

推荐答案

您不能在 STL 容器中存储数组.对于一般情况,您会使用向量的向量或类似的向量.对于您的具体情况,我将使用 std::pair 向量,如下所示: std::vector>.std::pair 是一个具有两个成员的类,firstsecond,无论您将其模板化为何种类型.

You can't store arrays in STL containers. You'd use a vector of vectors or somesuch for the general case. For your specific case, I'd use a vector of std::pair, like so: std::vector<std::pair<int, int> >. std::pair is a class that has two members, first and second, of whatever type you templatize it to be.

我最初将它作为 std::vector<std::pair<int>>,但我不确定在两种类型相同的情况下它是否被重载以只接受 1 个参数......一点点挖掘没有发现这方面的证据,所以我将其修改为显式声明 firstsecond 都是 ints.

I originally had it as std::vector<std::pair<int> >, but I wasn't sure if it was overloaded to accept only 1 parameter in the case that both types are the same... a little digging turned up no evidence of this, so I modified it to explicitly state that both first and second are ints.

这篇关于如何将数组存储在 STL 列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆