1、max_size——容器能申请到的最大容量
#include <iostream>
#include <list>
int main ()
{
list li(10);
cout << "size() of li = "
<< li.size() << endl;
cout << "max_size = "
<< li.max_size() << endl;
return 0;
}
运行结果:
// size() of li = 10
// max_size = 4294967295
2、merge——合并两个容器,当源list均有序时,得到的list仍是有序的,当源list无序时,得到的list不能保证有序,因为当list1的前两个元素即表现出无序时,合并后的结果将是直接把list2接到list1的后面
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;
int main ()
{
int ary[] = {2,5,9,7,2,7,6,5};
list<int> list1(ary,ary+4);
list<int> list2(ary+4,ary+8);
cout << "list1 : ";
copy(list1.begin(),list1.end(),
ostream_iterator<int>(cout," "));
cout << endl;
cout << "list2 : ";
copy(list2.begin(),list2.end(),
ostream_iterator<int>(cout," "));
cout << endl << endl;
// 合并前先排序
list1.sort();
list2.sort();
list1.merge(list2);
cout << "After \"list1.merge(list2)\" :" << endl;
cout << "list1 : ";
copy(list1.begin(),list1.end(),
ostream_iterator(cout," "));
cout << endl;
cout << "size of list2 = " << list2.size()
<< endl;
cout << "list2 is " << (list2.empty() ? "" : "not ")
<< "empty" << endl;
return 0;
}
运行结果:
// list1 : 2 5 9 7
// list2 : 2 7 6 5
//
// After "list1.merge(list2)" :
// list1 : 2 2 5 5 6 7 7 9
// size of list2 = 0
// list2 is empty
3、pop_back——让容器的最后一个元素出栈
#include <iostream>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;
int main ()
{
list<int> l(5);
iota(l.begin(),l.end(),1);
copy(l.begin(),l.end(),
ostream_iterator<int>(cout," "));
cout << endl;
while ( !l.empty() )
{
l.pop_back();
copy(l.begin(),l.end(),
ostream_iterator<int>(cout," "));
cout << endl;
}
return 0;
}
运行结果:
// 1 2 3 4 5
// 1 2 3 4
// 1 2 3
// 1 2
// 1
4、pop_back——让容器的第一个元素出栈
#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();
while ( !l.empty() )
{
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
5、push_back——将某个元素从容器后端入栈
#include <iostream>
#include <list>
#include <iomanip>
#include <string>
using namespace std;
template <class T>
class Name
{
public:
Name(T f, T l) : first(f), last(l) {}
void print()
{
cout.setf(ios::left);
cout << setw(15) << first.c_str()
<< last << endl;
}
private:
T first, last;
};
//==========================================
int main ()
{
typedef Name<string> N;
typedef list<N> L;
L l;
L::iterator It;
N n1(string("Albert"),string("Johnson"));
N n2("Lana","Vinokur");
l.push_back(n1);
l.push_back(n2);
l.push_back(N("Linda","Bain"));
It = l.begin();
while ( It != l.end() )
(It++)->print();
cout << endl;
return 0;
}
运行结果:
// Albert Johnson
// Lana Vinokur
// Linda Bain
6、push_front——将某个元素从容器头入栈
#include <iostream>
#include <list>
#include <iomanip>
#include <string>
using namespace std;
template <class T>
class Name
{
public:
Name(T f, T l) : first(f), last(l) {}
void print()
{
cout.setf(ios::left);
cout << setw(15) << first.c_str()
<< last << endl;
}
private:
T first, last;
};
//==========================================
int main ()
{
typedef Name<string> N;
typedef list<N> L;
L l;
L::iterator It;
N n1(string("Albert"),string("Johnson"));
N n2("Lana","Vinokur");
l.push_front(n1);
l.push_front(n2);
l.push_front(N("Linda","Bain"));
It = l.begin();
while ( It != l.end() )
(It++)->print();
cout << endl;
return 0;
}
运行结果:
// Linda Bain
// Lana Vinokur
// Albert Johnson
7、rbegin、rend——容器逆序输出的头和尾
#include <iostream>
#include <list>
#include <algorithm>
#include <numeric>
#include <iterator>
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;
list<int>::reverse_iterator It = l.rbegin();
while ( It != l.rend() )
cout << *(It++) << " ";
cout << endl;
return 0;
}
运行结果:
// 1 2 3 4 5 6 7 8 9 10
// 10 9 8 7 6 5 4 3 2 1
8、remove——在容器中找到指定元素并删除
#include <iostream>
#include <list>
#include <algorithm>
#include <string>
using namespace std;
template <class T, class D>
class Salary
{
public:
Salary(T t) : id(t) {}
Salary(T t,D d) : id(t), sal(d) {}
void print ()
{ cout << id << " " << sal << endl; }
private:
T id;
D sal;
friend bool operator ==
(const Salary& s1,const Salary& s2)
{ return s1.id == s2.id; }
};
//==========================================
int main ()
{
typedef Salary<string,double> S;
typedef list<S> L;
L l;
l.push_back(S("012345",70000.0));
l.push_back(S("012346",60000.0));
l.push_back(S("012347",72000.0));
L::iterator It = l.begin();
while ( It != l.end() )
(It++)->print();
cout << endl;
S s("012345");
l.remove(s);
It = l.begin();
while ( It != l.end() )
(It++)->print();
cout << endl;
return 0;
}
运行结果:
// 012345 7000
// 012346 60000
// 012347 72000
//
// 012346 60000
// 012347 72000
除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。



