1、assign——初始化或者重置容器内元素
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
using namespace std;
int main ()
{
int ary[]={1,2,3,4,5};
list<int> l;
// 将数组ary分配给l容器
l.assign(ary,ary+5);
copy(l.begin(),l.end(),
ostream_iterator<int>(cout," "));
cout << endl;
// 用3个100替换l容器内容
l.assign(3,100);
copy(l.begin(),l.end(),
ostream_iterator<int>(cout," "));
cout << endl;
return 0;
}
运行结果:
// 1 2 3 4 5
// 100 100 100
2、back——返回容器的最后一个元素值,等价于*(容器.end()-1)或容器[容器.size-1]
#include <iostream>
#include <list>
#include <algorithm>
#include <string>
#include <iterator>
using namespace std;
template<class T, class D>
class Member
{
public:
Member(T t, D d) : name(t), sal(d) {}
void print();
private:
T name;
D sal;
};
template<class T, class D>
void Member<T,D>::print()
{
cout << name << " " << sal << endl;
}
//--------------------------------------
int main ()
{
typedef Member<string,double> M;
list<M> l;
l.push_back(M("Robert",60000));
l.push_back(M("Linda",75000));
list<M>::iterator It = l.begin();
cout << "Entire list:" << endl;
while ( It != l.end() )
(It++)->print();
cout << endl;
cout << "Return from back()" << endl;
l.back().print();
return 0;
}
运行结果:
// Entire list:
// Robert 60000
// Linda 75000
//
// Return from back()
// Linda 75000
3、begin——返回容器头
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
#include <numeric>
using namespace std;
int main ()
{
list<int> l(5);
iota(l.begin(),l.end(),1);
list<int>::iterator It = l.begin();
while ( It != l.end() )
cout << *It++ << " ";
cout << endl;
// list容器的第三个元素
It = l.begin()+2;
cout << *It << endl;
return 0;
}
运行结果:
// 1 2 3 4 5
// 3
4、clear——容器就像一个盒子,clear便是拿走盒子里面的东西
#include <iostream>
#include <list>
using namespace std;
int main ()
{
list<int> l(5,10);
cout << "Size of list = "
<< l.size() << endl;
l.clear();
cout << "After l.clear() size of list = "
<< l.size() << endl;
return 0;
}
运行结果:
// Size of list = 5
// After l.clear() size of list = 0
5、empty——容器就像一个盒子,empty便是判断盒子里面是否有东西
#include <iostream>
#include <list>
using namespace std;
int main ()
{
list<int> l;
cout << "List is ";
l.empty() ? cout << "" : cout << "not ";
cout << "empty" << endl;
l.push_back(100);
cout << "List is ";
l.empty() ? cout << "" : cout << "not ";
cout << "empty" << endl;
return 0;
}
运行结果:
// List is empty
// List is not empty
6、end——容器迭代器最后一个元素的下一个迭代
#include <iostream>
#include <list>
#include <numeric>
using namespace std;
int main ()
{
list<int> li(10);
iota(li.begin(),li.end(),1);
list<int>::iterator It = li.begin();
while ( It != li.end() )
cout << *(It++) << " ";
cout << endl;
return 0;
}
运行结果:
// 1 2 3 4 5 6 7 8 9 10
7、erase——从容器中删除某个元素或者某个区间
#include <iostream>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;
template <class T>
void print (list<T>& l)
{
list<int>::iterator It = l.begin();
while ( It != l.end()
{
cout << *(It++) << " ";
}
cout << endl;
}
//=====================
int main ()
{
list<int> li(10);
iota(li.begin(),li.end(),1);
print(li);
list<int>::iterator It;
It = find(li.begin(),li.end(),6);
// 删除It位置的元素
li.erase(It);
print(li);
It = find(li.begin(),li.end(),4);
// 删除从容器头到It位置的元素
li.erase(li.begin(),It);
print(li);
return 0;
}
运行结果:
// 1 2 3 4 5 6 7 8 9 10
// 1 2 3 4 5 7 8 9 10
// 4 5 7 8 9 10
8、front——返回容器的第一个元素
#include <iostream>
#include <list>
int main ()
{
int ary[] = {1,2,3,4,5};
list li;
for ( int i=0; i<5; i++ )
{
li.push_front(ary[i]);
cout << "front() : "
<< li.front() << endl;
}
return 0;
}
运行结果:
// front() : 1
// front() : 2
// front() : 3
// front() : 4
// front() : 5
9、insert——向容器中插入一个、多个或者一组元素
#include <iostream>
#include <list>
#include <algorithm>
#include <numeric>
using namespace std;
template <class T>
void print (list<T>& l)
{
list<int>::iterator It = l.begin();
while ( It != l.end() )
{
cout << *(It++) << " ";
}
cout << endl;
}
//====================================
int main ()
{
list<int> li1(10,0);
list<int> li2(5);
list<int>::iterator It;
iota(li2.begin(),li2.end(),1);
cout << "li1 : ";
print(li1);
cout << "li2 : ";
print(li2);
It = li1.begin();
// 在It位置后面插入20
li1.insert(++It,20);
cout << "li1 : ";
print(li1);
// 将两个 25 插入list容器头
li1.insert(li1.begin(),2,25);
cout << "li1 : ";
print(li1);
// 在li2对象尾部插入li1对象的内容
li1.insert(li1.end(),li2.begin(),li2.end());
cout << "li1 : ";
print(li1);
return 0;
}
运行结果:
// li1 : 0 0 0 0 0 0 0 0 0 0
// li2 : 1 2 3 4 5
// li1 : 0 20 0 0 0 0 0 0 0 0 0
// li1 : 25 25 0 20 0 0 0 0 0 0 0 0 0
// li1 : 25 25 0 20 0 0 0 0 0 0 0 0 0 1 2 3 4 5
除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。



