c++ STL学习笔记

来源:互联网 发布:黛堡嘉莱价格淘宝 编辑:程序博客网 时间:2024/06/07 05:23

C++ STL算法学习报告

 首先介绍一下STL里面几个常用的容器:

1.    vector

头文件 #include<vector>

    用法:可以随机访问容器的内容,在序列末尾添加或删除对象,但是因为是从尾部删除,过程非常慢,因为必须移动插入或删除点后面的所有对象。

    常用操作:

         a. vector <数据类型> 变量名V   // 创建vector

         b. V.push_back(数据)           // 在这组数据的后面插入数据

         c. V.front()                     // 引用第一个元素

         d. V.back()                     // 引用最后一个元素

         e. V.begin()                    // 返回第一个元素指向的迭代器

         f. V.end()                //返回第一个元素指向的迭代器        g. V.size()                     //获得这组数据的数量

         h. V.pop_back()                // 删除最后一个元素

         i. V.insert(loc,value)             // 在loc位置插入数据迭代器loc

         j. sort(v.begin(),c.end(),[cmp])     // 对vector数组进行(按照cmp函数)排序(默认从小到大)

    举例:把一串数组存进数组里面:这组数是:1 2 3 4 5 6 7 8 9 10;在5的位置插入数据15 排序

#include <iostream>

#include<cstdio>

#include<algorithm>

#include<cstring>

#include<cmath>

#include<vector>

 

usingnamespace std;

 

vector<int> q;

 

intmain(){

    q.clear();

    for(int i= 1;i < 11;i++){

        q.push_back(i);//給vector数组填充

    }

    printf("数组第一个元素 %d\n",q.front());

    printf("数组最后一个元素%d\n",q.back());

    vector <int >::iterator it; //定义迭代器类似指针

    printf("数组大小%d\n",q.size());

    q.pop_back();

    printf("删除最后一个数数组大小%d\n",q.size());

    it = q.begin()+5;

    q.insert(it,15);

    printf("插入元素后输出:");

    for(it=q.begin();it != q.end();it++){

        printf("%d ",*it);

    }//输出

    printf("\n");

    for(vector<int>::size_type i = 0;i< q.size();i++){

        printf("%d ",q[i]);

    }//输出

        printf("\n排序后元素输出:");

    sort(q.begin(),q.end());

      for(it=q.begin();it != q.end();it++){

        printf("%d ",*it);

    }

    return 0;

}

运行结果:

数组第一个元素 1

数组最后一个元素10

数组大小10

删除最后一个数数组大小9

插入元素后输出:1 2 3 4 5 15 6 7 8 9

1 2 3 4 515 6 7 8 9

排序后元素输出:1 2 3 4 5 6 7 8 9 15

    

2.    map

头文件  #include<map>

    用法:映射map<k,t>  K表示键,T表示对象,根据特定的键映射到对象,可以进行快速的检索

    常用操作:

         a. map<key类型,value 类型> 变量名m   // 创建map

         b. m.begin()                    // 返回第一个元素指向的迭代器

         c. m.end()                //返回第一个元素指向的迭代器        d. m.size()                     //获得这组数据的数量

e. m.clear()                   //清空迭代器

         f. m.empty()                   // 如果为空返回true

         g. m.insert(pair<loc,value> val)    // 在loc位置插入数据迭代器loc

         h. m.find(key_type,key)             // 返回一个迭代器指向键值为key的元素,未找到返回很多end()

         i. m.lower_bound(key_type,key)   // 返回第一个>=key的迭代器

         j. m.upper_bound(key_type,key)   // 返回第一个 >key 的迭代器

       k. m.erase(loc)                 //删除指定位置的元素

 举例:构建一个每个id(整数)对应一个人名字(字符串)并实现插入,删除等操作

#include <iostream>

#include <cstdio>

#include <algorithm>

#include <cstring>

#include <cmath>

#include <vector>

#include <map>

 

using namespace std;

 

map <int,string>m;

 

int main(){

m.clear();

m.insert(pair<int,string>(123456,"张三"));

m.insert(pair<int,string>(123457,"李四"));

m.insert(pair<int,string>(123458,"王二"));

m.insert(pair<int,string>(123459,"王海"));

map<int,string>::iterator it;

printf("map的大小 %d\n",m.size());

if(m.empty()){

printf("map为空");

}

printf("输出map:\n");

for(it = m.begin(); it != m.end();it++){

cout << it->first <<"  " << it ->second<< endl;

}

it = m.find(123456);

if(it != m.end()){

cout << "键值查找成功" << endl;

cout << it->first <<"  " << it ->second<< endl;

}

it = m.lower_bound(123457);

cout << "第一个大于等于键值的数   "<<it->first << "  " << it ->second << endl;

it = m.upper_bound(123457);

cout << "第一个大于键值的数  " << it->first << "  " << it ->second << endl;

if(m.find(1234567) != m.end()){

printf("查找成功\n");

}

else{

printf("查找失败\n");

m.insert(pair<int,string>(123450,"wangshan"));

printf("插入成功\n");

}

printf("插入数据后输出map:\n");

for(it = m.begin(); it != m.end();it++){

cout << it->first <<"  " << it ->second<< endl;

}

m.erase(123458);

printf("删除数据后输出map:\n");

for(it = m.begin(); it != m.end();it++){

cout << it->first <<"  " << it ->second<< endl;

}

return 0;

}

运行结果:

  map的大小  4

输出map:

123456 张三

123457 李四

123458 王二

123459 王海

键值查找成功

123456 张三

第一个大于等于键值的数   123457  李四

第一个大于键值的数  123458  王二

查找失败

插入成功

插入数据后输出map:

123450 wangshan

123456 张三

123457 李四

123458 王二

123459 王海

删除数据后输出map:

123450 wangshan

123456 张三

123457 李四

123459 王海

3.    set

   头文件  #include<set>

    用法:数学里面的集合,用来存储数据,相同的数字不会出现两次

    常用操作:

         a. set<数据类型> 变量名s   // 创建set

         b. s.begin()                    // 返回第一个元素指向的迭代器

         c. s.end()                //返回第一个元素指向的迭代器        d. s.size()                     //获得这组数据的数量

e. s.clear()                   //清空迭代器

         f. s.empty()                   // 如果为空返回true

         g. s.insert(pair<loc,value>val)    // 在loc位置插入数据迭代器loc

         h. s.find(key_type,key)             // 返回一个迭代器指向键值为key的元素,未找到返回很多end()

         i. s.lower_bound(key_type,key)   // 返回第一个>=key的迭代器

         j. s.upper_bound(key_type,key)   // 返回第一个 >key 的迭代器

         k.s.erase(loc)                 //删除指定位置的元素

 举例:存储1,2,3,4,5,6,7,8,9,10实现集合的操作:

      #include <iostream>

#include <cstdio>

#include <algorithm>

#include <cstring>

#include <cmath>

#include <vector>

#include <set>

 

using namespace std;

 

set <int> s;

 

int main(){

   s.clear();

   for(int i = 1;i < 11;i++){

       s.insert(i);

    }

   set<int>::iterator it;

   printf("set的大小  %d\n",s.size());

   if(s.empty()){

       printf("set为空");

    }

   printf("输出set:\n");

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

       printf("%d ",*it);

    }

   printf("\n");

   it = s.find(10);

   if(it != s.end()){

        cout << "查找成功" << endl;

       cout << *it << endl;

    }

   it = s.lower_bound(8);

   cout << "第一个大于等于8的数    "<< *it <<endl;

   it = s.upper_bound(8);

   cout << "第一个大于键值8的数   " << *it <<endl;

   if(s.find(11) != s.end()){

       printf("11查找成功\n");

    }

   else{

       printf("11查找失败\n");

       s.insert(11);

       printf("插入成功\n");

    }

   printf("插入11数据后输出set:\n");

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

       cout << *it << " ";

    }

   cout << endl;

   s.erase(4);

     printf("删除数据4后输出set:\n");

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

       cout << *it<< " ";

    }

   return 0;

}

运行结果:

set的大小  10

输出set:

1 2 3 4 5 6 7 8 9 10

查找成功

10

第一个大于等于8的数    8

第一个大于键值8的数   9

11查找失败

插入成功

插入11数据后输出set:

1 2 3 4 5 6 7 8 9 10 11

删除数据4后输出set:

1 2 3 5 6 7 8 9 10 11

 

0 0
原创粉丝点击