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

查看:130
本文介绍了如何存储阵列在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};

这编译并运行就好了。然而,当我试图添加到我的列表中,像这样:

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 ++ - 福几乎是不存在的):

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 ::名单?我应该使用结构?我只是缺少一个 * &放大器; 地方

推荐答案

您不能存储阵列中的STL容器。你会使用向量的载体或诸如此类的一般情况。针对您的特殊情况下,我会使用std ::对的载体,就像这样:的std ::矢量&lt;的std ::对&LT; INT,INT&GT; &GT; 的std ::对是有两个成员的一类,第一第二,任何类型你模板化的那样。

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 ::矢量&lt;的std ::对&LT; INT&GT; &GT; ,但我不知道这是否超载只接受1这两种类型都一样......一点点挖掘结果显示,没有这方面的证据的情况下的参数,所以我修改它明确规定,无论第一第二 INT 秒。

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天全站免登陆