如何在 STL 列表中存储数组?

发布于 2024-07-18 10:31:49 字数 1601 浏览 2 评论 0 原文

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

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);

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

萌︼了一个春 2024-07-25 10:31:49

存储在标准库容器中的内容必须是可分配和可复制的——数组两者都不是。 最好的选择是创建 std::vector 列表。 或者,您可以将数组包装在结构中:

struct A {
   int array[2];
};

std::list <A> alist;

The thing stored in a Standard Library container must be assignable and copyable - arrays are neither. Your best bet is to create a list of std::vector. Alternatively, you can wrap the array in a struct:

struct A {
   int array[2];
};

std::list <A> alist;
执妄 2024-07-25 10:31:49

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

编辑:我最初将其设置为 std::vector; >,但我不确定它是否被重载以在两种类型相同的情况下只接受 1 个参数...稍微挖掘一下没有发现任何证据,所以我将其修改为显式声明 firstsecond 都是 int

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.

Edit: 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.

很快妥协 2024-07-25 10:31:49

这是使用 boost::array 的好情况而不是“经典”的 C 风格数组。
这应该有效:

std::list<boost::array<int,2> > my_list;
boost::array<int,2> foo={{1,2}};
my_list.push_back(foo);

This is a good situation for using boost::array instead of "classic" C-style arrays.
This should work:

std::list<boost::array<int,2> > my_list;
boost::array<int,2> foo={{1,2}};
my_list.push_back(foo);
逆光飞翔i 2024-07-25 10:31:49

在这种情况下,我建议您使用 std::pair 来存储值。 它位于
<实用程序>

您可以在列表中存储指向数组的指针,但随后您将必须处理所有内存管理。 如果您只需要成对的值,那么使用pair 会简单得多。

I'd suggest you use std::pair to store the values in this case. It is located in
<utility>.

You could store pointers to the arrays in the list but then you would have to deal with all the memory management. Using pair is a lot simpler if pairs of values are all you need.

桃扇骨 2024-07-25 10:31:49

对于 C++11,有一个 ::std::array 可用的包装器,可与标准容器一起使用,如下所示:

#include <array>
#include <iostream>
#include <list>
#include <cstdint>

int
main()
{
    using t_Buffer = ::std::array<::std::int32_t, 2>;
    using t_Buffers = ::std::list<t_Buffer>;
    t_Buffers buffers;
    buffers.emplace_back(t_Buffer{1, 2});
    ::std::cout << buffers.front()[0] << " " << buffers.front()[1] << ::std::endl;
    return(0);
}

在线运行此代码

With C++11 there is a ::std::array wrapper available which can be used with standard containers like this:

#include <array>
#include <iostream>
#include <list>
#include <cstdint>

int
main()
{
    using t_Buffer = ::std::array<::std::int32_t, 2>;
    using t_Buffers = ::std::list<t_Buffer>;
    t_Buffers buffers;
    buffers.emplace_back(t_Buffer{1, 2});
    ::std::cout << buffers.front()[0] << " " << buffers.front()[1] << ::std::endl;
    return(0);
}

Run this code online

ぶ宁プ宁ぶ 2024-07-25 10:31:49

从 C++11 开始,我们可以使用标准 std::array:

#include <array>
#include <list>
#include <iostream>

int main () {
    std::list<std::array<int, 2>> l {{3,4},{5,6}};
    l.push_back({1,2});

    for (const auto &arr : l)
        for (const auto &v : arr)
            std::cout << v << ' ';
}

l.push_back({{1,2}});

等来消除某些 clang 警告。

输出:

3 4 5 6 1 2 

As of C++11, we can do this with the standard std::array:

#include <array>
#include <list>
#include <iostream>

int main () {
    std::list<std::array<int, 2>> l {{3,4},{5,6}};
    l.push_back({1,2});

    for (const auto &arr : l)
        for (const auto &v : arr)
            std::cout << v << ' ';
}

or

l.push_back({{1,2}});

etc. to silence some clang warning.

Output:

3 4 5 6 1 2 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文