gpt4 book ai didi

c++ - 不可能使 > 工作

转载 作者:太空狗 更新时间:2023-10-29 20:23:01 25 4
gpt4 key购买 nike

尽管我花费了不合理的时间,但我还是有很多问题无法解决。我想要一个列表< list< int* >>>,但它不起作用。这是我的代码:

int main(int argc, const char * argv[]) {
int a=2;
int b=3;

list<list<int*>> test;

list< list<int*> >::iterator it;
it = test.begin();

it->push_back(&a);
it->push_back(&b);

b=4; //should modify the content of "test"

for(list <int*>::iterator it2 = it->begin(); it2 != it->end(); it2++) {
cout << *it2 << endl;
}
}

使用 xCode,它可以编译,但出现“线程 1:EXC_BAD_ACCESS”错误。希望大家多多指教!

谢谢!

最佳答案

test 是空的,所以 test.begin() 是一个单数迭代器,取消引用它是非法的。它会导致未定义的行为,类似于越界访问数组。

你需要这样做:

test.emplace_back();
it = test.begin();

这将向 test 添加一个新的值初始化元素,因此它将成为一个包含零元素列表的单元素列表。然后 将指向该单个元素。

关于c++ - 不可能使 <list <list< int* >> 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35146150/

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