1、resize——减小或者扩大容器的容量
#include <iostream>
#include <list>
int main ()
{
list<int> l(10);
cout << "Size of list l = "
<< l.size();
l.resize(100);
cout << "After l.resize(100)" << endl;
cout << "Size of list l = "
<< l.size();
l.resize(5);
cout << "After l.resize(5)" << endl;
cout << "Size of list l = "
<< l.size();
return 0;
}
运行结果:
// Size of list l = 10After l.resize(100)
// Size of list l = 100After l.resize(5)
// Size of list l = 5
2、reverse——将List迭代器内的元素排序反向
#include <iostream>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;
int main ()
{
list<int> l(10);
iota(l.begin(),l.end(),1);
copy(l.begin(),l.end(),
ostream_iterator<int>(cout," "));
cout << endl;
l.reverse();
copy(l.begin(),l.end(),
ostream_iterator<int>(cout," "));
cout << endl;
return 0;
}
运行结果:
// 1 2 3 4 5 6 7 8 9 10
// 10 9 8 7 6 5 4 3 2 1
3、size——返回容器内元素的个数
#include <iostream>
#include <list>
#include <algorithm>
int main ()
{
list<int> l(5,0);
copy(l.begin(),l.end(),
ostream_iterator<int>(cout," "));
cout << endl;
cout << "Size of list = "
<< l.size() << endl;
int size = l.size();
for ( int i=0; i<size; i++ )
// or while ( !l.empty() ) - safer
{
l.pop_front();
cout << "Size of list = "
<< l.size() << endl;
}
return 0;
}
运行结果:
// 0 0 0 0 0
// Size of list = 5
// Size of list = 4
// Size of list = 3
// Size of list = 2
// Size of list = 1
// Size of list = 0
4、sort——将容器元素按照指定规则排序
#include <iostream>
#include <list>
#include <algorithm>
#include <functional>
using namespace std;
template <class T>
class Print
{
public:
void operator () (T& t)
cout << t << " ";
}
};
//-----------------------------
int main ()
{
int ary[] = {3,2,5,7,3,6,7,2,4,5};
list<int> li(ary,ary+10);
Print<int> print;
cout << "Before sorting\nli : ";
for_each(li.begin(),li.end(),print);
cout << endl << endl;
li.sort(greater<int>());
cout << "After li.sort(greater())\nli : ";
for_each(li.begin(),li.end(),print);
cout << endl << endl;
li.sort(less<int>());
cout << "After li.sort(less())\nli : ";
for_each(li.begin(),li.end(),print);
cout << endl;
return 0;
}
运行结果:
// Before sorting
// li : 3 2 5 7 3 6 7 2 4 5
//
// After li.sort(greater<int>())
// li : 7 7 6 5 5 4 3 3 2 2
//
// After li.sort(less<int>())
// li : 2 2 3 3 4 5 5 6 7 7
5、splice——拼接,将指定容器的全部或者部分元素剪切到该容器的指定位置
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;
template <class T>
class Print
{
public:
void operator () (T& t)
{
cout << t << " ";
}
};
//====================================
int main ()
{
list<int> li1, li2, li3, li4;
Print print;
for ( int i=0; i<5; i++ )
{
li1.push_back(i);
li2.push_back(i+5);
li3.push_back(i+10);
li4.push_back(i+15);
}
cout << "li1 : ";
for_each(li1.begin(),li1.end(),print);
cout << endl;
cout << "li2 : ";
for_each(li2.begin(),li2.end(),print);
cout << endl;
cout << "li3 : ";
for_each(li3.begin(),li3.end(),print);
cout << endl;
cout << "li4 : ";
for_each(li4.begin(),li4.end(),print);
cout << endl << endl;
li1.splice(li1.end(),li2);
cout << "li1 : ";
for_each(li1.begin(),li1.end(),print);
cout << endl << endl;
li1.splice(li1.end(),li3,li3.begin(),li3.end());
cout << "li1 : ";
for_each(li1.begin(),li1.end(),print);
cout << endl << endl;
list<int>::iterator It;
It = find(li4.begin(),li4.end(),18);
li1.splice(li1.begin(),li4,It);
cout << "li1 : ";
for_each(li1.begin(),li1.end(),print);
cout << endl;
cout << "li4 : ";
for_each(li4.begin(),li4.end(),print);
cout << endl;
return 0;
}
运行结果:
// li1 : 0 1 2 3 4
// li2 : 5 6 7 8 9
// li3 : 10 11 12 13 14
// li4 : 15 16 17 18 19
//
// li1 : 0 1 2 3 4 5 6 7 8 9
//
// li1 : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
//
// li1 : 18 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
// li4 : 15 16 17 19
6、swap——交换两个容器的内容
#include <iostream>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;
void print (list<int>& l)
{
list<int>::iterator It = l.begin();
while ( It != l.end() )
{
cout << *(It++) << " ";
}
cout << endl;
}
//===============================
int main ()
{
list<int> li1(5), li2(5);
iota(li1.begin(),li1.end(),1);
iota(li2.begin(),li2.end(),5);
cout << "li1 : ";
print(li1);
cout << "li2 : ";
print(li2);
li1.swap(li2);
cout << endl <<"After swapping:" << endl;
cout << "li1 : ";
print(li1);
cout << "li2 : ";
print(li2);
return 0;
}
运行结果:
// li1 : 1 2 3 4 5
// li2 : 5 6 7 8 9
//
// After swapping:
// li1 : 5 6 7 8 9
// li2 : 1 2 3 4 5
7、unique——删除list中重复且相连的元素,确保元素的唯一性
#include <iostream>
#include <list>
#include <algorithm>
#include <iomanip>
#include <string>
using namespace std;
template <class T>
class Member
{
public:
Member(T f, T l) :
first_n(f), last_n(l) {}
void print();
private:
string last_n, first_n;
// 确保排序功能
friend bool operator < (Member& m1,
Member& m2)
{ return m1.last_n < m2.last_n; }
// 确保唯一性功能
friend bool operator == (Member& m1,
Member& m2)
{ return m1.last_n == m2.last_n; }
};
//---------------------------------------
template <class T>
void Member<T>::print()
{
cout.setf(ios::left);
cout << setw(15) << last_n.c_str()
<< first_n << endl;
}
typedef Member<string> M;
//========================================
int main ()
{
list<M> li1;
li1.push_back(M("Linda","Smith"));
li1.push_back(M("Robert","Frost"));
li1.push_back(M("Alex","Amstrong"));
list li2;
li2.push_back(M("Linda","Smith"));
li2.push_back(M("John","Wood"));
li2.push_back(M("Alex","Amstrong"));
li1.sort();
li2.sort();
li1.merge(li2);
cout << "li1 after sorting and mergin"
<< endl;
list<M>::iterator It = li1.begin();
while ( It != li1.end() )
{
(It++)->print();
}
cout << endl;
li1.unique();
cout << "After li1.unique()" << endl;
It = li1.begin();
while ( It != li1.end() )
{
(It++)->print();
}
cout << endl;
return 0;
}
运行结果:
// li1 after sorting and mergin
// Amstrong Alex
// Amstrong Alex
// Frost Robert
// Smith Linda
// Smith Linda
// Wood John
//
// After li1.unique()
// Amstrong Alex
// Frost Robert
// Smith Linda
// Wood John
除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。



