C++ std::List 在中间和结尾之间插入?

标签 c++ list

如果我有这样一个基本列表:

std::list<int> mylist;
mylist.push_front(1);
mylist.push_front(2);
mylist.push_front(3);
mylist.push_front(4);
mylist.push_front(5);
mylist.push_front(6);
mylist.push_front(7);

如何在 3 和 4 之间插入数字 8?

最佳答案

使用:std::list::insertstd::advance

auto it = mylist.begin();

std::advance(it,4); //locate the position
mylist.insert(it,8); //insert the element

关于C++ std::List 在中间和结尾之间插入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19574982/

相关文章:

C++ 列表没有类型错误

linq - 根据另一个列表从列表条件中删除某些元素

c++ - 如何在 VS C++ 6.0 中递增枚举?

c++ - Visual Studio 2010 C++ 中 win32form 上的控制台应用程序输出

c++ - 将第一个值插入链表时尝试访问头部时出现段错误

具有多父多态层次结构的 C++ VTable 实现

c++ - 如何检查矩阵中每一行/列的相等性?

c# - 如何在声明它的同一行中初始化 C# 列表。 (IEnumerable 字符串集合示例)

c# - List<string[]> 通过 Linq 确定最大长度

python - 如何将 txt 文件中的元组添加到不带引号的列表中? Python