【问题标题】:How do I store arrays in an STL list?如何将数组存储在 STL 列表中?
【发布时间】: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 中?我应该使用结构吗?我只是在某处错过了*&amp; 吗?

【问题讨论】:

    标签: c++ arrays stl


    【解决方案1】:

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

    struct A {
       int array[2];
    };
    
    std::list <A> alist;
    

    【讨论】:

    • 如何将数组推送到alist
    【解决方案2】:

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

    编辑:我最初将其命名为 std::vector&lt;std::pair&lt;int&gt; &gt;,但我不确定在两种类型相同的情况下它是否被重载以仅接受 1 个参数...稍微挖掘一下,没有发现任何证据,所以我对其进行了修改,明确指出firstsecond 都是ints。

    【讨论】:

    • 标准中的定义没有提供第二种类型的默认类型,所以必须明确提供两种类型。
    【解决方案3】:

    这是使用boost::array 代替“经典”C 样式数组的好情况。 这应该有效:

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

    【讨论】:

      【解决方案4】:

      我建议您在这种情况下使用 std::pair 来存储值。它位于
      &lt;utility&gt;

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

      【讨论】:

        【解决方案5】:

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

        等等。消除一些铿锵声警告。

        输出:

        3 4 5 6 1 2 
        

        【讨论】:

          【解决方案6】:

          在 C++11 中,有一个 ::std::array wrapper 可用,可用于标准容器,如下所示:

          #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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-08-27
            • 2012-07-20
            • 2021-01-28
            • 2014-09-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多