gpt4 book ai didi

c++ - 如何用容器列表实现功能

转载 作者:行者123 更新时间:2023-11-28 02:53:02 25 4
gpt4 key购买 nike

我正在学习容器列表。我想编写函数来替换(索引上的元素)、插入(索引上的元素),我想知道如何删除它们。我正在学习这两天,我看了视频和阅读文章,但我只是不知道如何编写这段代码。我知道列表如何与指针一起工作。

这是我想象的样子(这只是一个开始)

void replace(list<Contact> &listOf, int index, const Contact &information) {

for(list<int>::iterator it = listOf.begin(); it != listOf.end(); it++){

}
}

我不知道 for 循环是否写对了,但我想它会遍历列表,如果找到要替换的索引,它就会直接覆盖。

我认为函数 insert 具有相同的参数。

这就是我想象的删除它的方式,但我不确定如何实现。

Contact delete(list<Contact> &listOf, int index) {

}

我在程序开始时创建了 Contact 结构,名称和姓氏都是字符串。

最佳答案

应该写循环

for (list<Contact>::iterator it = listOf.begin(); it != listOf.end(); ++it) {
do what you wan't with *it.
}

关于c++ - 如何用容器列表实现功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22621274/

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