c/c++ reference 阅读之一——vector,list,deque

来源:互联网 发布:网络推广成功案例总结 编辑:程序博客网 时间:2024/06/05 20:06

         之前学c++的时候也略微懂点stl的东东,不过没有好好地,全面地了解过它!现在一方面有了blog,另一方面考虑到今后工作可能会用的上,所以找了本c/c++ reference电子书学习,今天是第一天,看了点比较简单的东西,也把它写了出来,呵呵!同时也产生了一些个问题,希望在之后几天的学习中可以领悟或者解决这些个问题。 

  vector:说的土一点,它就是一个一维数组,但它实现了好多个操作,可以在数组的一端进行push,pop(所以可以把它当成一个简单的栈来用);可以在指定一个或连续几个位置插入(insert函数),删除(erase函数)元素;可以得到第一个,最后一个元素的值,back,front;可以分别得到第一个,最后一个的下一个的iterator(相当于指针)

总共有以下几种iterator:

input_iterator Read values with forward movement. These can be incremented, compared, and dereferenced. output_iterator Write values with forward movement. These can be incremented and dereferenced. forward_iterator Read or write values with forward movement. These combine the functionality of input and output iterators with the ability to store the iterators value. bidirectional_iterator Read and write values with forward and backward movement. These are like the forward iterators, but you can increment and decrement them. random_iterator Read and write values with random access. These are the most powerful iterators, combining the functionality of bidirectional iterators with the ability to do pointer arithmetic and pointer comparisons. reverse_iterator Either a random iterator or a bidirectional iterator that moves in reverse direction.

等日后有对每一种iterator有具体理解了,再来解释!

最后可以用at函数定位vector中的某一个,比用“[ ]”操作符安全,因为它不会出现越界的情况!其他一些返回空啊,返回vector大小阿,就不介绍了。具体看下面函数:

Container constructors create vectors and initialize them with some data Container operators compare, assign, and access elements of a vector assign assign elements to a vector at returns an element at a specific location back returns a reference to last element of a vector begin returns an iterator to the beginning of the vector capacity returns the number of elements that the vector can hold clear removes all elements from the vector empty true if the vector has no elements end returns an iterator just past the last element of a vector erase removes elements from a vector front returns a reference to the first element of a vector insert inserts elements into the vector max_size returns the maximum number of elements that the vector can hold pop_back removes the last element of a vector push_back add an element to the end of the vector rbegin returns a reverse_iterator to the end of the vector rend returns a reverse_iterator to the beginning of the vector reserve sets the minimum capacity of the vector resize change the size of the vector size returns the number of items in the vector swap swap the contents of this vector with another

 

下面简单说一下list,deque!基本上跟vector类似,只是多了几个接口,并且可以在两头进行pop,push操作(有点双向队列/栈的味道),另外对于list,还有merge合并两个list,sort对这个list进行排序(可以自己定义大小比较函数),splice把一个list连接到当前list上,unique,remove删除指定元素,remove_if如果条件为真,删除所有元素等功能!

以下为list函数表:

Container constructors create lists and initialize them with some data Container operators assign and compare lists assign assign elements to a list back returns a reference to last element of a list begin returns an iterator to the beginning of the list clear removes all elements from the list empty true if the list has no elements end returns an iterator just past the last element of a list erase removes elements from a list front returns a reference to the first element of a list insert inserts elements into the list max_size returns the maximum number of elements that the list can hold merge merge two lists pop_back removes the last element of a list pop_front removes the first element of the list push_back add an element to the end of the list push_front add an element to the front of the list rbegin returns a reverse_iterator to the end of the list remove removes elements from a list remove_if removes elements conditionally rend returns a reverse_iterator to the beginning of the list resize change the size of the list reverse reverse the list size returns the number of items in the list sort sorts a list into ascending order splice merge two lists in constant time swap swap the contents of this list with another unique removes consecutive duplicate elements

今天是全面开始了解stl的第一天,就先到这里吧!
原创粉丝点击