码迷,mamicode.com
首页 > 其他好文 > 详细

STL基础

时间:2016-09-14 21:45:12      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

vector:

1.头文件#include<vector>

2.声明vector对象,vector<int> vec;

3.尾部插入a:vec.push_back(a);

4.使用下标访问元素,cout<<vec[0]<<endl;

5.使用迭代器访问元素:

     for( vector<int>::iterator it=vec.begin();it!=vec.end();it++)
           cout<<*it<<endl;

6.插入元素:    vec.insert(vec.begin()+i,a);在第i+1个元素前面插入a;

7.删除元素:    vec.erase(vec.begin()+2);删除第3个元素

                    vec.erase(vec.begin()+i,vec.end()+j);删除区间[i,j-1];区间从0开始

8.向量大小:vec.size();

9.清空:vec.clear();

10.重载<

 1 struct MyObject
 2 {
 3     int x;
 4     int y;
 5 
 6   bool operator< (const MyObject &a)  const
 7     {
 8         if(x!=a.x)
 9             return x<a.x;
10         else     
11             return y<a.y;
12     }
13 };

tip:关于结构体定义的几种情况:

 1 struct  Student     
 2 {
 3      string name;
 4 }Stu;
 5 
 6 struct 
 7 {
 8         string name;
 9 }Stu; 
10 
11 typedef  struct  Student     
12 {
13      string name;
14 }Stu;
15 
16 typedef  struct 
17 {
18         string name;
19 }Stu; 

set:  set<int> s;

1.元素插入:insert()

2.遍历:

    set<int>::iterator it;

    for(it=s.begin();it!=s.end();it++)

4.元素删除

    s.erase(2);        //删除键值为2的元素

    s.clear();

5.元素检索:find(),若找到,返回该键值迭代器的位置,否则,返回最后一个元素后面一个位置。

    set<int>::iterator it;

    it=s.find(5);    //查找键值为5的元素

    if(it!=s.end())    //找到

            cout<<*it<<endl;

        else            //未找到

            cout<<"未找到";

6.自定义比较函数

    (1)元素不是结构体:

        //自定义比较函数myComp,重载“()”操作符

       

 1  struct myComp
 2 
 3         {
 4 
 5             bool operator()(const your_type &a,const your_type &b)
 6 
 7             [
 8 
 9                 return a.data-b.data>0;
10 
11             }
12 
13         }

 

        set<int,myComp>s;

        set<int,myComp>::iterator it;

    (2)如果元素是结构体,可以直接将比较函数写在结构体内。

        

 1 struct Info
 2 
 3         {
 4 
 5             string name;
 6 
 7             float score;
 8 
 9             bool operator < (const Info &a) const
10 
11             {
12 
13                 return a.score<score;
14 
15             }
16 
17         }

 

        set<Info> s;

 

        set<Info>::iterator it;

stack:

1.入栈:s.push(x);

2.出栈:s.pop();注意,出栈操作只是删除栈顶元素,并不返回该元素。

3.访问栈顶:s.top()

4.判断栈空:s.empty(),当栈空时,返回true

5.访问栈中的元素个数:s.size()

queue:

1.入队:q.push(x); 将x 接到队列的末端。

2.出队:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。

3.访问队首元素:q.front(),即最早被压入队列的元素。

4.访问队尾元素:q.back(),即最后被压入队列的元素。

5.判断队列空:q.empty(),当队列空时,返回true。

 

6.访问队列中的元素个数:q.size();

 

 

 

 

 

 

1.元素插入:insert()
2.中序遍历:类似vector遍历(用迭代器)
3.反向遍历:利用反向迭代器reverse_iterator。
    例:
    set<int> s;
    ......
    set<int>::reverse_iterator rit;
    for(rit=s.rbegin();rit!=s.rend();rit++)
4.元素删除:与插入一样,可以高效的删除,并自动调整使红黑树平衡。
            set<int> s;
            s.erase(2);        //删除键值为2的元素
            s.clear();
5.元素检索:find(),若找到,返回该键值迭代器的位置,否则,返回最后一个元素后面一个位置。
            set<int> s;
            set<int>::iterator it;
            it=s.find(5);    //查找键值为5的元素
            if(it!=s.end())    //找到
                cout<<*it<<endl;
            else            //未找到
                cout<<"未找到";
6.自定义比较函数
    (1)元素不是结构体:
        例:
        //自定义比较函数myComp,重载“()”操作符
        struct myComp
        {
            bool operator()(const your_type &a,const your_type &b)
            [
                return a.data-b.data>0;
            }
        }
        set<int,myComp>s;
        ......
        set<int,myComp>::iterator it;
    (2)如果元素是结构体,可以直接将比较函数写在结构体内。
        例:
        struct Info
        {
            string name;
            float score;
            //重载“<”操作符,自定义排序规则
            bool operator < (const Info &a) const
            {
                //按score从大到小排列
                return a.score<score;
            }
        }
        set<Info> s;
        ......
        set<Info>::iterator it;

STL基础

标签:

原文地址:http://www.cnblogs.com/yzwhykd/p/5873499.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!