1.list的基本操作
/*
list::assign(InputIterator first, InputIterator last) //分配新的元素
list::assign(size_type n, const value_type& val)
list::assign (initializer_list<value_type> il)
*/
//遍历操作
//list::begin() 返回迭代器指向第一个元素
//list::end() 指向最后一个元素后面的那个元素(past-the-end超过结尾)
//list::cbegin() 返回const_iterator指向第一个元素
//list::cend() 返回const_iterator指向最后一个后面的那个元素
//list::rbegin() 反向迭代器,指向最后一个元素
//list::rend() 反向迭代器,指向第一个元素之前的那个元素
//list::crbegin() return const_reverse_iterator to reverse beginning
//list::crend() 返回const_reverse_iterator指向第一个元素之前的那个元素
//list::front() 返回第一个元素的引用
//list::back() 返回最后元素的引用
/*
插入删除操作
list::insert(iterator it, const T& x) :迭代器指向元素前增加一个元素x
list::insert(iterator it, int n, const T& x) :迭代器指向元素前增加n个相同的元素x
list::insert(iterator it, const_iterator first, const_iterator last) :
迭代器it指向元素前插入另一个相同类型vector的[first, last)间的数据
list::emplace_front() 插入新元素到容器的开始位置
list::emplace_back() 插入新元素到容器的末尾
list::emplace(const_iterator position, Args&&... args) 功能和insert一样
list::push_front() 插入新元素到容器的开始位置
list::push_back() 插入新元素到容器的末尾
list::splice() 在list之间转移元素
list::unique() 去掉相同元素
list::clear() 删除所有元素
list::erase(const_iterator position) //删除迭代器指向位置的元素
list::erase(const_iterator first, const_iterator last) //删除[first,last)元素
list::pop_front() 删除第一个元素
list::pop_back() 删除最后一个元素
list::remove(const value_type& val) 删除val元素
list::remove_if(Predicate pred) 删除满足条件的元素
*/
/*
尺寸大小
list::empty() 判断是否为空
list::max_size() 最大size
list::size() 元素个数
list::resize() 改变size
*/
2 实例
#include <iostream>
#include <list>
using namespace std;
bool condition(int & n)
{
return (n >10);
}
bool mycomparison(int first, int second)
{
return (first < second);
}
int main()
{
list<int> test;
list<int> test1;
test.assign(5, 10);//5个10
test1.assign(test.begin(), test.end());//复制test -> test1:10,10,10,10,10
int a[] = { 1,2,3 };
//test
cout << "****************test**********************" << endl;
test.assign(a, a + 3); //test:1,2,3
test.emplace(test.end(), 4); //在末尾插入元素4,test:1,2,3,4
test.insert(test.begin(), test1.begin(), test1.end());//在test开始处插入test1[begin,end)->test=10,10,10,10,10,1,2,3,4
auto it1 = test.end();
it1--;
test.erase(it1);//删除末尾元素,test=10,10,10,10,10,1,2,3
auto it2 = test.begin();
it2++;
test.erase(it2,test.end());删除[2,最后一个后一个元素),test=10
if (!test.empty()) //true if the container size is 0, false otherwise.
cout << "test isn't empty" << endl;
else
cout << "test is empty" << endl;
cout << "test.size: " << test.size() << " test.maxsize : " << test.max_size() << endl;
cout << "test: ";
for (auto it : test)
{
cout << " " << it;
}
//test1
cout << "\n****************test1**********************" << endl;
test1.unique(); //test1:10
test1.emplace_front(0); //test1:0,10
test1.emplace_back(11); //test1:0,10,11
test1.reverse();//顺序翻转test1:11,10,0
test1.push_back(4);
test1.push_front(8);
test1.push_front(20);//test1:20,8,11,10,0,4
test1.sort();//重新排序 test1:0,4,8,10,11,20
test1.remove(10);//delete 10,tes1:0,4,8,11,20
test1.remove_if(condition); //condition:n>10 删除11,20 test1:0,4,8
cout << "test1: ";
for (auto it : test1)
{
cout << " " << it;
}
cout << endl;
test.swap(test1);
cout << "\n交换之后\n";
cout << "test: ";
for (auto it : test)
{
cout << " " << it;
}
cout << "\ntest1: ";
for (auto it : test1)
{
cout << " " << it;
}
test1 = test; //test1:0,4,8
int b[] = { 89,23,121,2,7,5,3,9,10 };
test.assign(b, b + 8);//test = 89,23,121,2,7,5,3,9,10
test.sort();//merge操作之前两个容器必须是有序的
test1.merge(test);//test1:0 2 3 4 5 7 8 9 23 89 121
cout << "\ntest1.merge(test): ";
for (auto it : test1)
{
cout << " " << it;
}
test.push_back(1);
test.sort();
test1.merge(test);//test1: 0 1 2 3 4 5 7 8 9 23 89 121
cout << "\n添加元素之后 test1.merge(test): ";
for (auto it : test1)
{
cout << " " << it;
}
cout << "\n";
system("pause");
return 0;
}