gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:47:22 25 4
gpt4 key购买 nike

考虑以下代码:

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com