#include<iostream>
#include <forward_list>
using namespace std;
void print(forward_list<int> m_list)
{
for (auto p : m_list)
{
cout << p << "\t";
}
cout << endl;
}
void main()
{
forward_list<int> m_list1{ 1, 2, 3, 4, 5 };
//1.將m_list1初始化m_list2
forward_list<int>m_list2(m_list1);
//2.front()返回第一個元素的引用,若用引用去接,則可以對第一個元素進行修改
auto &p = m_list2.front();
p = 1000;
cout << "m_list1:";
print(m_list1);
cout << "m_list2:";
print(m_list2);
//3.forward_list::remove()
// 刪除容器中所有等於指定值的元素。
//刪除表2中的3
m_list2.remove(3);
cout << "remove->del 3 int the m_list2:";
print(m_list2);
//刪除表2中的4
//4.forward_list::remove_if()
// 刪除容器中所有滿足指定條件的元素。
m_list1.remove_if([](int n){return n == 4; });
cout << "remove_if->del 4 int the m_list1:";
print(m_list1);
//5.erase_after(p)
//刪除p指向的位置之後的元素,或刪除從b之後直到(但不包含)e之間的元素。
//返回一個指向被刪除元素之後元素的迭代器,若不存在這樣的元素,則返回尾後迭代
cout << "Ty will del elements,current count:" << distance(m_list2.begin(), m_list2.end()) << endl;
forward_list<int>::iterator itPre = m_list2.before_begin();
auto itx = m_list2.erase_after(itPre);//刪除第一個元素,返回第二個元素 應該是2
cout << *itx << endl;
//6.挨個刪除節點;
while (!m_list2.empty())
{
//項目中往往需要得到這個節點做一些處理工作然後再刪除
//在這裏打印
int p = m_list2.front();
cout << p << endl;
m_list2.pop_front();
}
system("pause");
}
結果: