c++ - 如何在 C++ 中正确填写列表列表

标签 c++ list stl std

考虑以下代码:

#include <string>
#include <list>

using namespace std;

int main(int argc, const char * argv[])
{
    list<int> l{1,2,3,4};
    list<list<int>> ll;
    ll.push_back(l);
    return 0;
}

push_back 之后,ll 列表包含一个空元素。 我想知道为什么它没有填充列表 l 的内容。

注意:我在 Mac OS 10.9 上使用 Xcode 5.0.1。

编辑 1:

这是 lldb 输出:

(lldb) p l
(std::__1::list<int, std::__1::allocator<int> >) $0 = size=4 {
  [0] = 1
  [1] = 2
  [2] = 3
  [3] = 4
}
(lldb) p ll
(std::__1::list<std::__1::list<int, std::__1::allocator<int> >, std::__1::allocator<std::__1::list<int, std::__1::allocator<int> > > >) $1 = size=1 {
  [0] = size=0 {}
}
(lldb) 

编辑2

正如@molbdnilo 所说,这看起来像是调试器问题,因为当使用 ll 的第一项初始化新列表时,我得到的内容与 l 中的内容相同。

最佳答案

希望此示例代码有助于在列表 STL 中进行操作,

#include <iostream>

#include <string>
#include <list>

using namespace std;

int main(int argc, const char * argv[])
{
   list<int> l{1,2,3,4};
   list<int> l1{5,6,7,8};
   list<list<int>> ll;
   ll.push_back(l);
   ll.push_back(l1);
   list<list<int>>::iterator itr;
   for (itr=ll.begin(); itr != ll.end(); itr++)
   {
       list<int>tl=*itr;
       list<int>::iterator it;
       for (it=tl.begin(); it != tl.end(); it++)
       {
           cout<<*it;
       }
       cout<<endl<<"End"<<endl;
   }
   return 0;

关于c++ - 如何在 C++ 中正确填写列表列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20333914/

相关文章:

c++ - 将 vector 映射到特定范围

c++ - Visual Studio 2010 的 apache hadoop 源代码中的 "Error: expected a declaration"

c++ - DllRegisterServer 错误 0xc0000005,(C++ COM Dll)。如何在 Visual Studio 2008 中调试我的 DllRegisterServer 函数?

python - 如何在递归期间创建和编辑列表?

c++ - std::lower_bound 和 std::upper_bound 的基本原理?

c++ - inplace_merge 需要额外内存的原因是什么

c++ - 如何使用 boost::python 将 std::pair 公开给 python?

c++ - 如何在 C++ 中将 UTC 日期和时间转换为 time_t?

java - 创建 ListIterator 时会发生什么?

Python:如何将列表中的所有值转换为其 ascii 值?