C++ STL初学笔记

C++  STL初学笔记

更系统的版本见徐本柱的PPT

Ref:https://github.com/huihut/interview/tree/master/STL

set

在这儿:http://www.cnblogs.com/pdev/p/4035020.html

#include <vector>

可看作可以动态改变大小的数组。

注意:vector作为数组,下标自动从0开始

定义:vector <数据类型> v

    扩展:二维数组:   vector<vector<int> > Array2D;

应用:void  v.insert(int a,数据类型b) :在数组的第a个数后面插入b

        void  v.erase(int a,int b) :删除数组中位置从ab的元素

      int  v.begin() :返回数组位置(迭代器)

      int  v.end() :返回数组结尾位置(迭代器)

      数据类型  v.front() :返回第一个数据

        数据类型  v.back() :返回末尾的数据

      void  v.push_back(n) :在容器尾端插入新元素n

      void  v.pop_back() :删除结尾的一个元素

        void  v.reserve(int n) :重新定义数组大小

      int  v.empty() :判断容器是否为空。是则返回1

      int  v.size() :返回容器中元素个数

--------------------------------------------------------------------------

容器的迭代器:相当于数组的指针

Eg: 

 1 #include <cstdio>
 2 #include <vector>
 3 #include <cstdlib>
 4 #include <iostream>
 5 using namespace std;
 6 
 7 int main(int argc, char *argv[])
 8 {
 9     vector<int> arr;
10     int n, data;
11     while (cin >> n)
12     {
13         arr.clear();    //clean the array
14         if (arr.empty())
15         {
16             cout << "It's empty!" << endl;
17         }
18         for (int i = 0; i < n; ++i)
19         {
20             cin >> data;
21             arr.push_back(data);
22         }
23         cout << "The size of the array is " << arr.size() << endl;
24         cout << "All the element(s) in the array is(are) ";
25         /*
26         for (vector<int>::iterator it = arr.begin(); it != arr.end(); ++it)
27         {
28             cout << *it << " ";
29         }
30         */                    //用迭代器
31         for (int x=0;x<n;x++)
32             cout<<arr[x]<<" ";        //注意:vector和map还可以用这种类似数组下标的方式访问
33         cout << endl;
34     }
35     return EXIT_SUCCESS;
36 }
View Code

---------------------------------------------------------------------------

#include <deque>

同vector,但可以在头部插入元素

定义:deque <数据类型> d

应用:void  d.push_back(数据类型 n) :在数组尾端插入新元素n

      void  d.push_front(数据类型 n) :在数组头部插入新元素n

      void  d.pop_back(数据类型 n) :在数组尾部删除一个元素n

      void  d.pop_front(数据类型 n) :在数组头部删除一个元素n

        void  d.clear() :删除所有元素

        void  d.erase(int n) :删除指定位置元素

        void  d.insert(int a,数据类型 b) :指定位置插入元素

其他vector中的操作也适用。

 --------------------------------------------------------------------------

#include <string>

字符串

定义:string s(“字符串”)

应用:可以在数组中使用:string a[10];        string a[10][10];

                                    i=a[10][10].lenght();      cout<<a[10][10];

                                    a[5][6]==a[2][4];

void  string s2(s1) :将s1复制到s2     (s1可以是字符串or字符数组)

        void  string s(字符数组chs,int n) :将chs的前n个字符作为s的初值

        void  s1.swap(s2) :两string值互换

        void  s.append(string s1) :在末尾添加一段字符串s1

        void  s.erase(int a,int b) :在a位置开始删b个字符

        void  s.replace(int a,int b,string s1) :将a~b这一段替换成s1    (若s1置为””可实现删除a~b这一段的字符串)

        (s1==s2)、(s1<=s2)、(s1<s2) :比较字符串(按字典序)

        S1=s2;    把s2赋给s1

        S3=s1+s2;    把s2接到s1后

        S[i]=’X’;    以字符数组的形式操作(注意:单个字符属于char,用单引号)

        int  s.length() :返回字符串长度

        s[n]:Return the character @ position n in s (position starts @ 0)

        int  s.size() : 返回字符串长度(换行符、空格等控制符算一个字符)

        int  s.empty() :返回字符串是否为空。返回值1为是

        cin>>s5;  读取有效字符到遇到空格

    getline(cin,s6);  读取字符到遇到换行,空格可读入,直到‘ ’结束

    getline(cin,s7,'a'); 直到‘a’结束,其中任何字符包括' '都能够读入

    s.find(“string”) : 返回s中子串”string”出现的位置(若找不到,输出-1)

Useful functions:  (stdio.h)

      strstr

      strchr

      strtok

Useful Statements:    while(cin>>s)   or   while(getline(cin,s))

       {These functions are defined in the cctype header

             isalnum(c)   返回true,若c是字母or数字

             isalpha(c)   返回true,若c是字母

             isdigit(c)   返回true,若c是数字

             islower(c)   返回true,若c是小写字母

             ispunct(c)   返回true,若c是标点符号

             isspace(c)   返回true,若c是空格

             isupper(c)   返回true,若c是大写字母

             toupper(c)    若c是小写,则返回对应的大写字母。否则直接返回c

             tolower(c)    若c是大写,则返回对应的小写字母。否则直接返回c   

       }

参考:

http://blog.csdn.net/isgray/article/details/6853700

find(): http://blog.csdn.net/longshengguoji/article/details/8539471

         http://www.cnblogs.com/processakai/archive/2011/06/24/2089348.html

sstream: http://www.2cto.com/kf/201403/285676.html

------------------------------------------------------------------------------

#include <stack>

栈(容量无限制)

定义:stack <数据类型> s

应用:void  s.push(数据类型 s) :入栈

        数据类型  s.top() :输出栈顶元素

      Void  s.pop() :栈顶元素出栈

        Int s.empty() :检测栈是否为空    1为是

      int  s.size() :返回栈中元素个数

-----------------------------------------------------------------------

#include <queue>

队列

定义:queue <数据类型> q

应用:void  q.push(数据类型 n) :入队

        Void  q.pop() :出队

        Int  q.empty() :检测队是否为空    1为是

        数据类型  q.back() :输出队尾元素

        数据类型  q.front() :输出队首元素

        Int  q.size() :输出队列的元素个数

-------------------------------------------------------------------------

#include <queue>

拓展:优先队列

定义:priority_queue <数据类型> q

      (优先队列保证队列中最大的元素总是位于队首)

应用:同队列

 -------------------------------------------------------------------------

堆操作

首先,需要#include <algorithm>

void  make_heap(start_pointer,end_pointer,comp)        在指定范围的数组上建堆

void  pop_heap(start_pointer,end_pointer,comp)            删除堆顶元素,然后重建堆

void  push_heap(start_pointer,end_pointer,comp)          假设数组区间a[start]……a[end-1]已经是一个堆,然后将a[end]作为要入堆的新元素加进去,使得a[start]……a[end]是一个堆

void  sort_heap(start_pointer,end_pointer,comp)            假设数组区间a[start]……a[end-1]已经是一个堆,然后对其中的序列进行排序(排序后就不是一个有效堆了>_<)

注释:start_pointer、end_pointer分别表示起始位置、终止位置的指针

(调用方法示例:make_heap(&number[0],&number[12],cmp);

Cmp为比较函数(若不指定,默认建大根堆)

---------------------------------------------------------------------------

#include<map>

内部用红黑树实现,可实现一个Hash表用于查找(根据key查找value

Reference:http://www.oschina.net/question/234345_48876

Eg:

 1 #include <map>
 2 #include <string>
 3 #include <utility>
 4 #include <cstdlib>
 5 #include <iostream>
 6 using namespace std;
 7 
 8 int main(int argc, char *argv[]) 
 9 {
10     int n, m;
11     string name;
12     int phone_id;
13     map<string, int> hs;
14     while (cin >> n >> m) 
15     {
16         hs.clear();
17         for (int i = 0; i < n; ++i) 
18         {
19             cin >> name >> phone_id;
20             hs.insert(pair<string, int>(name, phone_id));    //插入
21         }
22         cout << "List:" << endl;
23         for (map<string, int>::iterator it = hs.begin(); it != hs.end(); ++it)             //用迭代器遍历
24         {
25             cout << it->first << " " << it->second << endl;        //输出key和value
26         }
27         while (m--) 
28         {
29             cin >> name;
30             if (hs.find(name) != hs.end())        //find返回name所在位置
31             {
32                 cout << hs[name] << endl;
33             }
34             else
35             {
36                 cout << "No such peron in your telephone directory." << endl;
37             }
38         }
39     }
40     return EXIT_SUCCESS;
41 }
View Code
原文地址:https://www.cnblogs.com/pdev/p/3955358.html