微博:
@TankyWoo基
新博客:
TankyWoo

《C++标准程序库》学习笔记4 — 第六章

  • 1.(P145)

容器类别的共同操作函数(只列了个别几个):

c.max_size()
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<bool>

貌似这个不怎么用,确切的说是不好。

《Effective C++》里面就说道了:避免使用vector<bool>

而且在网上也找了一篇讨论帖:

http://topic.csdn.net/t/20040511/15/3054586.html

  • 4.(P163)

Deque

deque与vector大部分类似,以下是几个不同点:

1.deque不提供容量操作( capacity()和reserve() )。

2.deque直接提供头部元素的插入和删除(push_front() 和 pop_front() )

deque还有个特点,元素的插入或删除可能导致内存重新分配,所以任何插入或者删除动作都会使所有指向deque元素的pointer, reference, iterator失效。唯一的例外是在头部或尾部插入元素。

  • 5.(P169)

List

list增加了remove()和remove_if()的特殊版本作为自己的成员函数。因此,面对list,应该调用成员函数remove()而不是面向vector<>或deque<>那样调用的STL算法。

List的特殊变动性操作(Special Modifying Operations)

list

关于Splice函数,注意通过链表来理解如何转移,比如:

c1.splice(pos, c2)操作后,原c2内就为空了。

看下面这个例子,注意结果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Author: Tanky Woo
// Blog:    www.wutianqi.com
#include <cstddef> 
#include <iostream> 
#include <vector> 
#include <list> 
#include <deque> 
#include <set> 
#include <algorithm> 
#include <iterator> 
using namespace std; 
 
void printLists (const list<int>& l1, const list<int>& l2) 
{ 
    cout << "list1: "; 
    copy (l1.begin(), l1.end(), ostream_iterator<int>(cout," ")); 
    cout << endl << "list2: "; 
    copy (l2.begin(), l2.end(), ostream_iterator<int>(cout," ")); 
    cout << endl << endl; 
} 
 
int main() 
{ 
    // create two empty lists 
    list<int> 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()

lower_bound(elem)
Returns the first position, where elem would get inserted (the first element >= elem)

upper_bound (elem)
Returns the last position, where elem would get inserted (the first element > elem)

equal_range (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表示是否安插成功。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::pair<std::set<float>::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的元素,不过可以用find_if()。count(key),find(key), lower_bound(key),upper_bound(key), equal_range(key)等参数都是key。

在 map/multimap中,所有元素的key都被视为常数。因此元素的实质类型是pair<const key, T>。这个限制是为了确保你不会因为变更元素的key而破坏已排序好的元素次序。

map的下标操作要熟悉,并且下标操作得到的直接是value。

posted on 2011-01-27 12:24  Tanky Woo  阅读(1767)  评论(0编辑  收藏  举报

导航