c++ - 在 std::forward_list 的开头插入元素

标签 c++ c++11 stl forward-list

我想从 std::forward_list 中删除元素并将其插入到列表的开头 但是没有插入方法......它只有 insert_after !

如何在 std::forward_list 的开头插入元素?

最佳答案

使用std::forward_list::push_front .

例子:

// forward_list::push_front
#include <iostream>
#include <forward_list>
using namespace std;

int main ()
{
  forward_list<int> mylist = {77, 2, 16};
  mylist.push_front (19);
  mylist.push_front (34);

  std::cout << "mylist contains:";
  for (int& x: mylist) std::cout << ' ' << x;
  std::cout << '\n';

  return 0;
}

输出:mylist 包含:34 ​​19 77 2 16

关于c++ - 在 std::forward_list 的开头插入元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21729569/

相关文章:

c++ - 在 C++ 中可以使用哪些其他有用的类型转换

c++ - 为什么这个函数没有导致错误?

c++ - 优化 C++11 随机生成器的使用

c++ - 成员函数声明的参数列表后的单个&符号是什么意思?

c++ - std::vector<char> 中元素的字节对齐方式是什么?

c++ - 是否可以在 C++20 中使用比较运算符在枚举值之间建立小于大于大于的排序关系

c++ - ffmpeg转OpenGL纹理黑屏

C++ 11 move 语义

c++ - 如何将 `std::vector<uchar>` 保存到 `std::ostream` 中?

c++ - 构造一对 unique_ptr<int> 和 int