c++ STL学习笔记

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
6、 函数模板和类模板 3 6.1函数模板 4 6.1.1为什么要有函数模板 4 6.1.2函数模板语法 5 6.1.3函数模板和模板函数 6 6.1.4函数模板做函数参数 6 6.1.5函数模板遇上函数重载 8 6.1.6 C++编译器模板机制剖析 10 6.2类模板 18 6.2.1为什么需要类模板 18 6.2.2单个类模板语法 18 6.2.3继承中的类模板语法 20 6.2.4类模板语法知识体系梳理 21 6.2.5类模板中的static关键字 23 6.3类模板在项目开发中的应用 25 6.4作业 29 7、C++的类型转换 29 7.1 类型转换名称和语法 29 7.2 类型转换一般性介绍 29 7.3 典型案例 30 7.3.1 static_cast用法和reinterpret_cast用法 30 7.3.2 dynamic_cast用法和reinterpret_cast用法 31 7.3.3 const_cast用法 33 7.4 总结 33 8、异常处理机制专题 33 8.1 异常处理的基本思想 34 8.1.1传统错误处理机制 34 8.1.2异常处理的基本思想 34 8.2 C++异常处理的实现 35 8.2.1异常基本语法 35 8.2.2栈解旋(unwinding) 39 8.2.3异常接口声明 40 8.2.4异常类型和异常变量的生命周期 40 8.2.5异常的层次结构(继承在异常中的应用) 46 8.3标准程序库异常 47 8.4训练强化 51 9 C++输入和输出流 51 9.1 I/O流的概念和流类库的结构 51 9.2标准I/O流 53 9.2.1标准输入流 55 9.2.2标准输出流 59 9.3文件I/O 66 9.3.1文件流类和文件流对象 66 9.3.2C++文件的打开与关闭 67 9.3.3C++对ASCII文件的读写操作 69 9.3.4 C++对二进制文件的读写操作 74 9.4作业练习 75 10、STL实用技术专题 79 10.1 STL(标准模板库)理论基础 79 10.1.1基本概念 79 10.1.2容器 80 10.1.3迭代器 82 10.1.4算法 82 10.1.5C++标准库 82 10.1.6模板简要回顾 85 10.2容器 86 10.2.1 STL的string 86 10.2.2Vector容器 90 10.2.3Deque容器 96 10.2.4stack容器 101 10.2.5Queue容器 103 10.2.6List容器 105 10.2.7优先级队列priority_queue 110 10.2.8Set和multiset容器 111 10.2.9Map和multimap容器 118 10.2.10容器共性机制研究 123 10.2.11其他 124 10.3算法 125 10.3.1算法基础 125 10.3.2STL算法中函数对象和谓词 138 10.3.3常用的遍历算法 148 10.3.4常用的查找算法 152 10.3.5常用的排序算法 154 10.3.6常用的拷贝和替换算法 156 10.3.7常用的算术和生成算法 157 10.3.8常用的集合算法 158 10.4 STL综合案例 159 10.4.1案例学校演讲比赛 159 10.4.2案例:足球比赛 161

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值