list支持的常见的操作:
#include<iostream>
#include <list>
#include <vector>
#include <cctype>
#include <cmath>
using namespace std;
bool myconparsion(double first,double second){
return (int(first)<int(second));
}
bool is_old(const int& value){
return (value%2==0);
}
bool compare_nocase(const string& first,const string& second){
unsigned int i=0;
while(i<first.length() && i<second.length()){
if(tolower(first[i]<tolower(second[i]))) return true;
else if (tolower(first[i]>tolower(second[i]))) return false;
++i;
}
return (first.length() < second.length());
}
bool same_intergal_part(double first,double second){
return (int(first)==int(second));
}
class is_near{
bool operator()(double first,double second){
return (fabs(first-second)<3);
}
};
int main()
{
list<int> first, second;
first.assign(7, 100);
second.assign(first.begin(), first.end());
vector<int> myvector = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
first.assign(myvector.begin(), myvector.end());
cout << "list first contains: ";
for (auto &X : first)
cout << X << " ";
cout << endl;
cout << "second contains: ";
for (auto &X : second)
cout << X << " ";
cout << endl;
first.back() = 10;
cout << first.back() << endl;
list<int>::iterator It;
cout << "first changed: ";
for (It = first.begin(); It != first.end(); ++It)
{
*It += 1;
cout << ' ' << *It;
}
cout << endl;
cout << "first changed: ";
for (decltype(first.cbegin()) It = first.cbegin(); It != first.cend(); ++It)
{
cout << ' ' << *It;
}
cout << endl;
cout << "first changed: ";
for (decltype(first.crbegin()) It = first.crbegin(); It != first.crend(); ++It)
{
cout << ' ' << *It;
}
cout << endl;
cout << "first changed: ";
for (decltype(first.rbegin()) It = first.rbegin(); It != first.rend(); ++It)
{
*It += 2;
cout << ' ' << *It;
}
cout << endl;
list<pair<int, char>> mynewlist;
mynewlist.emplace(mynewlist.begin(), 100, 'x');
mynewlist.emplace(mynewlist.begin(), 10, 'n');
mynewlist.emplace(mynewlist.begin(), 1, 'x');
mynewlist.emplace(mynewlist.begin(), 100, 'x');
mynewlist.emplace(mynewlist.begin(), 10, 'n');
mynewlist.emplace(mynewlist.begin(), 100, 'x');
mynewlist.emplace(mynewlist.begin(), 10, 'n');
cout << "mynewlist contains: ";
for (auto X : mynewlist)
cout << "(" << X.first << " " << X.second << ")" << " ";
cout << endl;
mynewlist.emplace_back(1, 'a');
mynewlist.emplace_back(2, 'b');
mynewlist.emplace_back(3, 'c');
mynewlist.emplace_back(4, 'd');
cout << "mynewlist contains: ";
for (auto X : mynewlist)
cout << "(" << X.first << " " << X.second << ")" << " ";
cout << endl;
mynewlist.emplace_front(4, 'd');
mynewlist.emplace_front(3, 'c');
mynewlist.emplace_front(2, 'b');
mynewlist.emplace_front(1, 'a');
cout << "mynewlist contains: ";
for (auto X : mynewlist)
cout << "(" << X.first << " " << X.second << ")" << " ";
cout << endl;
cout << boolalpha << mynewlist.empty() << noboolalpha << endl;
cout << "mynewlist.erase(position): ";
list<pair<int, char>>::iterator it1, it2;
it1 = it2 = mynewlist.begin();
advance(it1, 1);
it1 = mynewlist.erase(it1);
for (auto X : mynewlist)
cout << "(" << X.first << " " << X.second << ")" << " ";
cout << endl;
cout << "mynewlist.rease(iterator):";
advance(it2, 4);
mynewlist.erase(it1, it2);
for (auto X : mynewlist)
cout << " (" << X.first << X.second << ")" << " ";
cout << endl;
list<int> mylist;
int *p;
p = mylist.get_allocator().allocate(5);
for (int i = 0; i < 5; i++)
p[i] = i;
cout << "The list arry contains: ";
for (int i = 0; i < 5; i++)
cout << " " << p[i] << " ";
cout << endl;
mylist.get_allocator().deallocate(p, 5);
list<int> mylist1;
for (int i = 0; i < 10; ++i)
mylist1.push_back(i * 2);
cout << "mynewlist1 contains: ";
for (auto X : mylist1)
cout << X << " ";
cout << endl;
list<int>::iterator It1;
It1 = mylist1.begin();
advance(It1, 3);
mylist1.insert(It1, 10);
mylist1.insert(It1, 2, 1);
cout << "mynewlist1 contains: ";
for (auto X : mylist1)
cout << X << " ";
cout << endl;
vector<int> myvector1;
for (int i = 0; i < 10; ++i)
myvector1.push_back(i + 11);
mylist1.insert(It1, myvector1.begin(), myvector1.end());
cout << "mylist1 contains: ";
for (auto X : mylist1)
cout << X << " ";
cout << endl;
list<double> newfirst, newsecond;
newfirst.push_back(1.2);
newfirst.push_back(2.1);
newfirst.push_back(1.1);
newfirst.push_back(1.0);
newfirst.push_back(0.99);
newfirst.push_back(3.4);
newsecond.push_back(11.3);
newsecond.push_back(13.2);
newsecond.push_back(2.7);
newsecond.push_back(12.5);
newfirst.sort();
newsecond.sort();
newfirst.merge(newsecond);
cout << "newfirst contains: ";
for(auto X:newfirst)
cout << X << " ";
cout <<endl;
newsecond.push_back(2.1);
first.merge(second,myconparsion);
cout << "newfirst contains: ";
for(auto X:newfirst)
cout << X << " ";
cout <<endl;
mylist=mylist1;
mylist1.pop_back();
mylist1.pop_front();
mylist.push_back(2);
mylist.push_front(1);
cout << "mylist1.remove() contains: ";
mylist1.remove(14);
for(auto X:mylist1)
cout << X << " ";
cout << endl;
mylist1.remove_if(is_old);
cout << "newfirst contains: ";
for(auto X:mylist1)
cout << X << " ";
cout <<endl;
list<int> mylist2;
mylist2.resize(3);
cout << "mylist2 contains: ";
for(auto X:mylist2)
cout << X << " ";
cout <<endl;
mylist2.resize(13,6);
cout << "mylist2 contains: ";
for(auto X:mylist2)
cout << X << " ";
cout <<endl;
mylist1.reverse();
cout << "mylist1 contains: ";
for(auto X:mylist1)
cout << X << " ";
cout << endl;
list<string> liststring;
list<string>::iterator StringIt;
liststring.push_back("One");
liststring.push_back("Two");
liststring.push_back("Three");
liststring.sort();
cout << "liststring contains: ";
for(auto X:liststring)
cout << X << " ";
cout << endl;
liststring.sort(compare_nocase);
cout << "liststring contains: ";
for(auto X:liststring)
cout << X << " ";
cout << endl;
list<int> listmy1;
list<int> listmy2;
list<int>::iterator ListIt;
for(int i=0;i<9;++i)
listmy1.push_back(i+2);
for(int i=0;i<8;++i)
listmy2.push_back(i*10);
ListIt = listmy1.begin();
advance(ListIt,1);
listmy1.splice(ListIt,listmy2);
listmy2.splice(listmy2.begin(),listmy1,ListIt);
ListIt=mylist1.begin();
advance(ListIt,3);
mylist1.splice(mylist1.begin(),mylist1,ListIt,listmy1.end());
cout << "listmy1 contains: ";
for(auto X:listmy1)
cout << X << " ";
cout << endl;
cout << "listmy2 contains: ";
for(auto X:listmy2)
cout << X << " ";
cout << endl;
listmy1.swap(listmy2);
cout << "listmy2 contains: ";
for(auto X:listmy2)
cout << X << " ";
cout << endl;
double newdoubles[]={1.2,3.4,1.5,5.6,7.0,1.4,12,23.0,45.8,21.7,12.9,16.7,11.4,12.7,18.9};
list<double> doublelist(newdoubles,newdoubles+16);
doublelist.sort();
doublelist.unique();
doublelist.unique(same_intergal_part);
doublelist.unique(is_near());
cout << " doublelist contains: ";
for(list<double>::iterator It=doublelist.begin();It!=doublelist.end();++It)
cout << *It << " ";
cout << endl;
}