List of commonly used methods in C ++

Reference https://www.cnblogs.com/fnlingnzb-learner/p/5889483.html
Lists sequentially stored in the element list. Compared to vector (Vectors), which allows rapid insertion and removal, but a random access than slower.

Common functions:
ASSIGN () to list the assignment
back () Returns the last element
begin () returns a pointer to the first element of the iterator
clear () removes all elements
empty () if the list is empty then return to true
End () Returns the end iterator
erase () removes an element
front () returns the first element
get_allocator () returns the list configurator
insert () element is inserted into a list in
MAX_SIZE () returns the maximum number of list elements can be accommodated
merge () merge two a list
pop_back () Removes the last element
pop_front () removes the first element
push_back () add an element to the end of the list of
push_front () to add an element in the list of head
rbegin () returns a pointer to the first element of reverse iteration is
remove () remove elements from the list
the remove_if () delete elements specified conditions
rend () points to the end of the list reverse iterator to
a resize () to change the list size
reverse () the list of elements inverted
size () returns the element number in list number
sort () to sort list
splice () merge two list
swap () swaps two list
UNIQUE () to delete duplicate elements in list

A bug
in the code below need be noted that the definition of the iterator iter, list<int>::iterator iter;when the method definition iterator will compile in the main function, but the function defined in put_list iterator, if such use will be reported as follows wrong, you need to use the previous typedef list<int> INLIST;, INLIST::iterator iter;I do not know why

/home/jlm/桌面/main.cpp:16:7: error: expected primary-expression before ‘int’
  list<int>::iterator iter;

Sample code common 1-

#include <iostream>
#include <string>
#include <list>

using namespace std;
typedef list<int> INLIST;

//从前向后显示list队列的全部元素
void put_list(list<int> list,string name)
{
	if(list.empty())
	{
		cout<<"list--"<<name<<"is empty!"<<endl;
		return;
	}
	INLIST::iterator iter;
	
	cout<<"The contents of "<<name<<":";
	for(iter=list.begin();iter!=list.end();iter++)
		cout<<*iter<<" ";
	cout<<endl;
}

//测试list容器的功能
int main(void)
{
	//三种初始化list的方式
	//list1对象初始为空
	list<int> list1;
	//list2对象最初有10个值为6的int元素
	list<int> list2(10,6);
	//list3对象最初有3个值为6的int元素
	list<int> list3(list2.begin(),--list2.end());

	//声明一个名为iter的迭代器
	list<int>::iterator iter;

	//从前向后显示各个list对象的元素
	put_list(list1,"list1");
	put_list(list2,"list2");
	put_list(list3,"list3");

	//向list1序列后面添加两个元素
	list1.push_back(2);
	list1.push_back(4);
	cout<<"list1.push_back(2) and list.push_back(4): "<<endl;	
	put_list(list1,"list1");

	//在list1序列中插入数据
	list1.insert(++list1.begin(),3,9);
	cout<<"list1.insert(list1.begin()+1,3,9): "<<endl;

	//测试引用类函数
	cout<<"list1.front()= "<<list1.front()<<endl;
	cout<<"list1.back()= "<<list1.back()<<endl;

	//从list1序列的前后各移去一个元素
	list1.pop_front();
	list1.pop_back();
	cout<<"list1.pop_front() anf list1.pop_back(): "<<endl;
	put_list(list1,"list1");

	//清除list1中的第二个元素
	list1.erase(++list1.begin());
	cout<<"list1.erase(++list1.begin()): "<<endl;
	put_list(list1,"list1");

	//对list2赋值并显示
	list2.assign(8,1);
	cout<<"list2.assign(8,1): "<<endl;
	put_list(list2,"list2");

	//显示序列的状态信息
	cout<<"list1.max_size(): "<<list1.max_size()<<endl;
	cout<<"list1.size(): "<<list1.size()<<endl;
	cout<<"list1.empty(): "<<list1.empty()<<endl;

	//list序列容器的运算
	put_list(list1,"list1");
	put_list(list3,"list3");
	cout<<"list1>list3: "<<(list1>list3)<<endl;
	cout<<"list1<list3: "<<(list1<list3)<<endl;
	
	//对list1容器排序
	list1.sort();
	put_list(list1,"list1");
	
	//结合处理
	list1.splice(++list1.begin(),list3);
	put_list(list1,"list1");
	put_list(list3,"list3");
}

Comparative example code of two -list

#include <iostream>
#include <string>
#include <list>

using namespace std;
typedef list<int> INLIST;

//从前向后显示list队列的全部元素
void put_list(list<int> list,string name)
{
	if(list.empty())
	{
		cout<<"list--"<<name<<"is empty!"<<endl;
		return;
	}
	INLIST::iterator iter;
	
	cout<<"The contents of "<<name<<":";
	for(iter=list.begin();iter!=list.end();iter++)
		cout<<*iter<<" ";
	cout<<endl;
}

//测试list容器的功能
int main(void)
{
	//三种初始化list的方式
	//list1有1个值为9的元素
	list<int> list1(1,9);
	//list2对象最初有3个值为6的int元素
	list<int> list2(3,6);

	cout<<"(list1(1,9)>list2(3,6)): "<<(list1>list2)<<endl;

	//list3有3个值为6的元素
	list<int> list3(3,6);
	//list2对象最初有3个值为6的元素
	list<int> list4(3,6);
	cout<<"(list3(3,6)>list4(3,6)): "<<(list3>list4)<<endl;

	//list5有4个值为6的元素
	list<int> list5(4,6);
	//list6对象最初有3个值为6的元素
	list<int> list6(3,6);
	cout<<"(list5(4,6)>list6(3,6)): "<<(list5>list6)<<endl;

	//list7有4个值为6的元素
	list<int> list7(100,1);
	//list8对象最初有3个值为6的元素
	list<int> list8(3,6);
	cout<<"(list7(100,1)>list8(3,6)): "<<(list7>list8)<<endl;
}
(list1(1,9)>list2(3,6)): 1
(list3(3,6)>list4(3,6)): 0
(list5(4,6)>list6(3,6)): 1
(list7(100,1)>list8(3,6)): 0

From the results obtained can be analyzed, (list1> list2) result of the two lists are compared one by one out, the results of comparison of the corresponding element type of size comparison, if the size of the result is returned occur.

Guess you like

Origin blog.csdn.net/qq_34122731/article/details/90733111