c++ 容器(list学习总结)

     list是一个线性双向链表结构,它的数据由若干个节点构成,每一个节点都包括一个信息块(即实际存储的数据)、一个前驱指针和一个后驱指针。它无需分配指定的内存大小且可以任意伸缩,这是因为它存储在非连续的内存空间中,并且由指针将有序的元素链接起来。由于其结构的原因,list 随机检索的性能非常的不好,因为它不像vector 那样直接找到元素的地址,而是要从头一个一个的顺序查找,这样目标元素越靠后,它的检索时间就越长。检索时间与目标元素的位置成正比。虽然随机检索的速度不够快,但是它可以迅速地在任何节点进行插入和删除操作。因为list 的每个节点保存着它在链表中的位置,插入或删除一个元素仅对最多三个元素有所影响,不像vector 会对操作点之后的所有元素的存储地址都有所影响,这一点是vector 不可比拟的。

list 的特点:

(1) 不使用连续的内存空间这样可以随意地进行动态操作;
(2) 可以在内部任何位置快速地插入或删除,当然也可以在两端进行push 和pop 。
(3) 不能进行内部的随机访问,即不支持[ ] 操作符和vector.at() ;
(4) 相对于verctor 占用更多的内存。

初学list:需要掌握的知识:

(1)定义一个list 

(2)向list中加入元素 

(3)如何知道list是否为空 

(4)如何使用for循环来遍历一个list 

(5)如何使用STL的通用算法for_each来遍历list 

(6)list成员函数begin() 和 end() 以及它们的意义 

(7)iterator范围的概念和一个范围的最后一个位置实际上并不被处理这一事实 

第一:定义,插入,遍历打印。

    代码实现如下:

#include<stdio.h>
#include<iostream>
#include<list>
#include<string>
#include<algorithm>
using namespace std;

 

void PrintIt(string& StringToPoint)
{
cout << StringToPoint << endl;
}

int main()

{

list<string> test;

list<string>::iterator testiterator;

test.push_back("no");
test.push_back("march");
test.push_front("ok");
test.push_front("loleina");
test.push_front("begin");
test.push_back("end");

 

for (testiterator = test.begin(); testiterator != test.end(); ++testiterator)
{
cout << *testiterator << endl;
}
cout << "-------------" << endl;


for_each(test.begin(), test.end(), PrintIt);
cout << "-------------" << endl;

system("PAUSE");
return 0;
}

定义了一个字符串类型的list。需要包含提供STL的 list类的头文件#include <list>即可;list的成员函数push_back()把一个对象放到一个list的后面,而 push_front()把对象放到前面。

我们想要遍历一个list,比如打印一个list中的所有对象来看看list上不同操作的结果。要一个元素一个元素的遍历一个list, 可以这样做:

A. 这个程序定义了一个iterator(类似指针),testiterator。它指向了这个list的第一个元素。 这可以调用testiterator.begin()来做到,它会返回一个指向list开头的iterator。然后把它和testiterator.end()的 返回值来做比较,到了那儿的时候就停下来。 容器的end()函数会返回一个指向容器的最后一个位置的iterator。 在上面的例子中,每一次执行for循环,我们就重复引用iterator来得到我们打印的字符串。

    注意:不能用testiterator.begin()+2来指向list中的第三个对象,因为STL的list是以双链的list来实现的,所有的数据存放不一定是连续存放的。 它不支持随机存取。

B.使用STL的通用算法for_each()来遍历一个iterator的范围,然后调用PrintIt()来处理每个对象。 不需要初始化、比较和给iterator增量。for_each()完成了这些工作。执行于对象上的操作被很好的 打包在这个函数以外了,不用再做那样的循环了,代码更加清晰了。 

第二:count()和count_if() 的基本使用

     STL的通用算法count()和count_it()用来给容器中的对象记数。就象for_each()一样,count()和count_if() 算法也是在iterator范围内来做的。

#include<stdio.h>
#include<iostream>
#include<list>
#include<string>
#include<algorithm>
using namespace std;

class IsLoleina
{
public:
bool operator()(string& name)
{
return name == "loleina";
}

};

int main()
{
list<string> test;
list<int> score;
list<string>::iterator testiterator;

test.push_back("no");
test.push_back("march");
test.push_front("ok");
test.push_front("loleina");
test.push_front("begin");
test.push_back("end");


score.push_back(100);
score.push_back(90);
score.push_back(80);
score.push_back(70);
score.push_back(100);
score.push_back(20);


int countNum(0);

countNum= count(score.begin(), score.end(), 100);
cout << "there are " << countNum << " scores of 100" << endl;
cout << "-------------" << endl;

int countLoleina(0);
countLoleina=count_if(test.begin(), test.end(), IsLoleina());
cout << "there are " << countLoleina << " loleina" << endl;


system("PAUSE");
return 0;
   count()算法统计等于某个值的对象的个数。count_if() 带一个函数对象的参数。函数对象是一个至少带有一个operator()方法的类。有些STL算法作为参数接收函数对象并调用这个函数对象的operator()方法。函数对象被约定为STL算法调用operator时返回true或false。它们根据这个来判定这个函数。举个例子会 说的更清楚些。count_if()通过传递一个函数对象来作出比count()更加复杂的评估以确定一个对象是否应该被记数。

posted @ 2016-02-03 14:45  loleina  阅读(73734)  评论(1编辑  收藏  举报