- 1.(P145)
容器类别的共同操作函数(只列了个别几个):
c.maxsize() _Returns the maximum number of elements possible
c1.swap(c2)/swap(c1, c2) Swaps the data of c1and c2
_c.begin() / c.end() _Returns an iterator for the first element/ the position after the last element
c.rbegin() Returns a reverse iterator for the first element of a reverse iteration
c.rend() Returns a reverse iterator for the position after the last element of a reverse iteration
_c.insert(pos,elem) _Inserts a copy of elem (return value and the meaning of pos differ)
_c.erase(beg,end) _Removes all elements of the range [beg,end) (some containers return next element not removed)
c.clar() Removes all elements (makes the container empty)
- 2.(P149)
vector的大小(size)和容量(capacity)
size指示当前容器的元素数量
capacity指示vector可以容纳的元素数量,超过这个值就会重新配置内部存储器。
使用resize(num)或resize(num, elem)将元素数量改为num个。
使用reserve(num)将vector可容纳数量改为num
capacity >= size
vector容器不能向不存在的空间赋值,也就是说,vector的assign()可以赋值的范围是[begin(), end)。注意这是size的范围,不是capacity的范围。
- 3.(P158)
Class vector
貌似这个不怎么用,确切的说是不好。
《Effective C++》里面就说道了:避免使用vector
而且在网上也找了一篇讨论帖:
http://topic.csdn.net/t/20040511/15/3054586.html
- 4.(P163)
Deque
deque与vector大部分类似,以下是几个不同点:
1.deque不提供容量操作( capacity()和reserve() )。
2.deque直接提供头部元素的插入和删除(pushfront() 和 popfront() )
deque还有个特点,元素的插入或删除可能导致内存重新分配,所以任何插入或者删除动作都会使所有指向deque元素的pointer, reference, iterator失效。唯一的例外是在头部或尾部插入元素。
- 5.(P169)
List
list增加了remove()和remove_if()的特殊版本作为自己的成员函数。因此,面对list,应该调用成员函数remove()而不是面向vector<>或deque<>那样调用的STL算法。
List的特殊变动性操作(Special Modifying Operations)
关于Splice函数,注意通过链表来理解如何转移,比如:
c1.splice(pos, c2)操作后,原c2内就为空了。
看下面这个例子,注意结果。
// Author: Tanky Woo
// Blog: www.wutianqi.com
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
void printLists (const list& l1, const list& l2)
{
cout << "list1: ";
copy (l1.begin(), l1.end(), ostream_iterator(cout," "));
cout << endl << "list2: ";
copy (l2.begin(), l2.end(), ostream_iterator(cout," "));
cout << endl << endl;
}
int main()
{
// create two empty lists
list list1, list2;
// fill both lists with elements
for (int i=0; i<6; ++i) {
list1.push_back(i);
list2.push_front(i);
}
printLists(list1, list2);
// insert all elements of list1 before the first element with value 3 of list2
// – find() returns an iterator to the first element with value 3
list2.splice(find(list2.begin(),list2.end(), // destination position
3),
list1); // source list
printLists(list1, list2);
// move first element to the end
list2.splice(list2.end(), // destination position
list2, // source list
list2.begin()); // source position
printLists(list1, list2);
// sort second list, assign to list1 and remove duplicates
list2.sort();
list1 = list2;
list2.unique();
printLists(list1, list2);
// merge both sorted lists into the first list
list1.merge(list2);
printLists(list1, list2);
system("PAUSE");
return EXIT_SUCCESS;
}
结果为:
list1: 0 1 2 3 4 5 list2: 5 4 3 2 1 0
list1: list2: 5 4 0 1 2 3 4 5 3 2 1 0
list1: list2: 4 0 1 2 3 4 5 3 2 1 0 5
list1: 0 0 1 1 2 2 3 3 4 4 5 5 list2: 0 1 2 3 4 5
list1: 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 list2:
- 6.(P180)
set/multiset的特殊搜寻函数
就像list提供了特殊版本的remove(),remove_if()一样,set/multiset也提供了特殊的搜寻函数,他们比同名的STL函数能够更快的执行相应工作。
_count (elem) _Returns the number of elements with value elem
_find(elem) _Returns the position of the first element with value elem or end()
lowerbound(elem) _Returns the first position, where elem would get inserted (the first element >= elem)
upperbound (elem)_ Returns the last position, where elem would get inserted (the first element > elem)
equalrange (elem) _Returns the first and last position, where elem would get inserted (the range of elements == elem)
- 7.(P181)
对于set/multiset的赋值(assign)或交换(swap),如果准则不同,准则本身也会被赋值或者交换
- 8.(P183)
set/multiset未提供remove()操作。但是却可以直接c.erase(elem)删除"与elem相等"的元素。
set/multiset的安插(insert)型函数一次只能插入一个元素。
set的安插(insert)函数返回值是个pair型,first表示新元素的位置或者已含有的相同元素的位置;second表示是否安插成功。
std::pair::iterator,bool> status;
//insert value and assign return value
status = c.insert(value);
//process return value
if (status.second) {
std::cout << value << " inserted as element "
}
else {
std::cout << value << " already exists as element "
}
std::cout << std::distance(c.begin().status.first) + 1
<< std::endl;
- 9.(198)
可以把set/multiset视为特殊的map/multimap,只不过set元素的value和key指向同一对象。因此map/multimap拥有set/multiset的所有能力和所有操作函数。
map/multimap和set/multimap一样,自身提供了特殊的搜寻函数。注意这里的成员函数find()的参数是key,也就是说,不能以find()搜寻拥有某特定value的元素,不过可以用findif()。count(key),find(key), lowerbound(key),upperbound(key), equalrange(key)等参数都是key。
在 map/multimap中,所有元素的key都被视为常数。因此元素的实质类型是pair
map的下标操作要熟悉,并且下标操作得到的直接是value。