STL学习(一)

来源:互联网 发布:常州小河淘宝客服招聘 编辑:程序博客网 时间:2024/06/07 06:45

STL概述
STL容器基本功能:初始化、大小相关的三个操作函数和比较。
1)初始化
每个容器都有一个default的构造函数,一个copy构造函数和一个析构函数。使用的方法大概如下:
ContType c;
ContType c1(c2);
ContType c(beg, end);

2)三个大小相关操作函数:size()、empty()、max_size()。其中empty()为size()==0的一个快捷方式,但实际操作上empty()可能会比size()==0的效率要高。

3)比较操作:比较的时候可能涉及到不同类型的容器的比较,这个时候要用到比较算法。还有一个性能上需要注意的,当对容易赋值时候,源容器的所有元素被拷贝到目标容器,目标容易的所有元素被全部移除,拷贝的操作代价比较高。如果两个容器类别相同,而且拷贝后源容器不在使用,那么使用swap()性能上比拷贝优异的多,它只交换某些内部的指针(指向实际数据如元素、配置器、排序准则),所以时间复杂度是“常数”,不像实际复制操作的复杂度为“线性”。

vector概述
vector为一个动态的数组,最大的特点就是可以容量有时可能重新分配,而重新分配的时候和vector元素相关的所有references、pointers、iterators都会失效,而且内存的重新分配会很耗时间。如果想避免vector重新分配内存,可以有两种方法:
1)初始化的时候分配足够的内存。std::vector<int> v(10),但是这样会调用类的默认构造函数,而这样的话可能会很耗费时间。
2)还有一种方法就是在std::vector<int> v;后用v.reserve(10)来给vector充足的余量。

 

/** vector Demo*/#include <iostream>#include <vector>#include <string>#include <algorithm>using namespace std;int main(int, char *[]){//create empty vector for stringsvector<string> sentence;//print size()cout<<"size() "<<sentence.size()<<endl;//reserve memory for five elements to avoid reallocationsentence.reserve(5);//append some elementssentence.push_back("Hello,");sentence.push_back("how");sentence.push_back("are");sentence.push_back("you");sentence.push_back("?");//print elements separated with spacescopy(sentence.begin(), sentence.end(), ostream_iterator<string>(cout, " "));cout<<endl;//print "technical data"cout<<"max_size(): "<<sentence.max_size()<<endl;cout<<"size(): "<<sentence.size()<<endl;cout<<"capacity(): "<<sentence.capacity()<<endl;//swap second and fourth elementif (sentence.size() > 3)swap(sentence[1], sentence[3]);//insert element "always" before element "?"sentence.insert(find(sentence.begin(), sentence.end(), "?"), "always");sentence.~vector();//sentence.clear();//assign "!" to the last elementif (!sentence.empty()){sentence.back() = "!";}//print elements separated with spacescopy(sentence.begin(), sentence.end(), ostream_iterator<string>(cout, " "));cout<<endl;//print "technical data" againcout<<"max_size(): "<<sentence.max_size()<<endl;cout<<"size(): "<<sentence.size()<<endl;cout<<"capacity(): "<<sentence.capacity()<<endl;//eraseif(sentence.size() > 2){sentence.erase(sentence.begin(), sentence.begin() + 1);}sentence.push_back("Hello");sentence.insert(sentence.begin(), "insert");//print elements separated with spacescopy(sentence.begin(), sentence.end(), ostream_iterator<string>(cout, " "));cout<<endl;//use as common arrayvector<char> v(10);strcpy(&v[0], "HELLO!");//OKprintf("%s\n", &v[0]);//ERROR (might work, but not portable)printf("%s\n", v.begin()); return 0;}


vector使用一些注意的地方:
1.不要把迭代器当作第一元素的地址来传递,vector迭代器是由版本定义的,也许不是个一般指针。
printf("%s\n", v.begin()); //ERROR (might work, but not portable)
printf("%s\n", &v[0]);     //OK
对于printf("%s\n", v.begin()); ,在VC6.0上可以正确的运行(STL为PJ STL),但是在red hat上运行错误(STL 为SGI版本的)。
2.在使用下标访问和使用back()和front()函数的时候一定要注意vector的大小和是否为空。
3.使用clear()与~vector<Elem>()的区别是析构之后capacity为0,不能再使用这个vector,而clear之后capacity并没有变化。

参考:

《C++标准程序库》

原创粉丝点击