0705第七讲标准模版库
1、namespace:命名空间,解决相同作用域同名函数或变量问题
要求:使用时必须以’命名空间::成员名称’的格式

类型转换:强制类型转换(向下转换)
动态类型转换:dynamic_cast,把父类指针转换子类指针(如果转型成功返回子类指针,如果转型失败返回NULL,向下转型安全)

格式:dynamic_cast<子类指针>(父类指针)
如果转型成功,那么得到一个子类指针
如果转型失败,那么得到一个NULL

静态类型转换:static_cast 和强制类型转换效果相同,不能判别是否成功

动态类型转换:必须有virtual函数
eg:

class Base{
public:virtual void print()const{cout<<"virtual Base..."<<endl;}void outB() const{cout<<"Base..."<<endl;}
};
class D1:public Base{
public:void print()const{cout<<"virtual D1..."<<endl;}void outD1()const{cout<<"D1..."<<endl;}
};
class D2:public Base{
public:void print()const{cout<<"virtual D2..."<<endl;}void outD2()const{cout<<"D2..."<<endl;}
};
int main(int argc, const char * argv[]) {D1 d1;D2 d2;Base* b[2] = {&d1,&d2};for (int i = 0; i < 2; ++i) {if (D1* p1 = dynamic_cast<D1*>(b[i]) ) {cout<<i<<"..."<<endl;p1->print();p1->outD1();}else if(D2* p2 = dynamic_cast<D2*>(b[i])){cout<<i<<"..."<<endl;p2->print();p2->outD2();}}return 0;
}

2、字符串:string,实际上是一个模版类basic_string
头文件:#include,使用时必须包含using namespace std;
构造函数:
1)默认构造函数,初始化一个空字符串。 string str
2)构造函数,使用字符串初始化对象 string str(“good day”)
3)构造函数使用一个隐式转换 string sr = “i am a student”拷贝构造函数
4)类字符串还有拷贝构造函数、赋值操作符重载函数和输出运算符重
string str = “i am a student”;
cout<

#include <iostream>
using namespace::std;
#include <vector>
#include <list>
#include <map>
class Point{int x;int y;
public:Point(int _x,int _y):x(_x),y(_y){}void print(){cout<<"x = "<<x<<" y = "<<y<<endl;}
};
typedef list<Point*>PointList;
void funcVector(){Point p1(1,1),p2(1,2),p3(1,3),p4(1,4),p5(1,5);vector<Point*> v;v.push_back(&p1);v.push_back(&p2);v.push_back(&p3);v.push_back(&p4);cout<<"v[2]: ";v[2]->print();cout<<"v.at(2): ";v.at(2)->print();cout<<"size before insert: "<<v.size()<<endl;v.insert(v.begin(),&p5);cout<<"size after insert: "<<v.size()<<endl;cout<<"using for recycle:"<<endl;for (int i = 0;i < v.size() ; ++i) {cout<<"v.at("<<i<<"):";v.at(i)->print();}cout<<"using iteraror:"<<endl;cout<<"size : "<<v.size()<<endl;vector<Point*>::iterator itor;//创建对象iter = v.begin();while (itor!=v.end()) {(*itor++)->print();}cout<<"the first element:";v.front()->print();cout<<"the last element:";v.back()->print();cout<<"maxsize\t\t"<<v.max_size()<<endl;cout<<"capacity\t"<<v.capacity()<<endl;v.clear();cout<<"size after clear:\t"<<v.size()<<endl;
}

v[2]: x = 1 y = 3
v.at(2): x = 1 y = 3
size before insert: 4
size after insert: 5
using for recycle:
v.at(0):x = 1 y = 5
v.at(1):x = 1 y = 1
v.at(2):x = 1 y = 2
v.at(3):x = 1 y = 3
v.at(4):x = 1 y = 4
using iteraror:
size : 5
x = 1 y = 5
x = 1 y = 1
x = 1 y = 2
x = 1 y = 3
x = 1 y = 4
the first element:x = 1 y = 5
the last element:x = 1 y = 4
maxsize 2305843009213693951
capacity 8
size after clear: 0

void funcList(){PointList lst;Point p1(1,1),p2(1,2),p3(1,3),p4(1,4),p5(1,5);lst.push_front(&p1);lst.push_back(&p1);lst.push_front(&p2);lst.push_back(&p2);lst.push_front(&p3);lst.push_back(&p3);lst.push_front(&p4);lst.push_back(&p4);
//    lst.insert(5, &p5);lst.erase(lst.begin());lst.remove(&p2);// 移除内容为&p2的项PointList::iterator itor = lst.begin();while (itor != lst.end()) {(*itor++)->print();}cout<<endl;
//    for (int i = 0;i < lst.size() ; ++i) {//        cout<<"lst.at("<<i<<"):"<<endl;
//        lst.at(i)->print();
//    }
}

x = 1 y = 3
x = 1 y = 1
x = 1 y = 1
x = 1 y = 3
x = 1 y = 4

void funcSet(){set<int>s;s.insert(10);s.insert(-5);s.insert(123);s.insert(2);s.insert(44);s.insert(10);//10不能插入,重复(自动被忽略)set<int>::iterator here;for(here=s.begin();here!=s.end();++here){cout<<*here<<" ";}cout<<endl;int key;cout<<"Enter an integer to search.\n";cin>>key;here=s.find(key);if(here!=s.end()){cout<<key<<"is in this set"<<endl;}else{cout<<key<<"is not in this set"<<endl;}
}

-5 2 10 44 123
Enter an integer to search.
5
5is not in this set

void funcMap(){map<string,char>m;m.insert(map<string,char>::value_type("key1",'1'));m["key2"] = '2';///使用multimap 键值可以重复但不能使用这种赋值方式,使用map自动忽略重复键值项m["key4"] = '4';m["key3"] = '3';map<string, char>::iterator itor = m.begin();while (itor != m.end()) {cout<<itor->first<<endl;cout<<itor->second<<endl;++itor;}
}

(使用insert函数 结果自动排序)
key1
1
key2
2
key3
3
key4
4

0705第七讲标准模版库相关推荐

  1. QT5——模版库、工具类及控件

    文章目录 qt模版库 字符串类 操作字符串 查询字符串数据 字符串的转换 容器类 QList类.QLinkedList类和QVector类 QList类 QLinkedList类 QVector类 J ...

  2. C++入门到精通 ——第七章 STL标准模板库大局观

    七.STL标准模板库大局观 Author: XFFer_ 先分享一本 <C++ 标准库 第二版> ,望在STL的道路上从入门到放弃!(开玩笑的啦,愈行愈远~) 链接: https://pa ...

  3. STL学习系列一:STL(标准模板库)理论基础

    STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.现然主要出现在C++中,但在被引入C++之前该技术就已经存在了很长的一段时间. STL的从广 ...

  4. 信息学奥赛中的STL(标准模板库)--2022.09.30

    1.信息学奥赛一本通 第5版 第8章 C++实用技巧与模版库(6节) 第一节  排序算法 第二节 运算符重载 第三节  字符串(string) 第四节 FIFO队列和优先队列 第五节  动态数组 第六 ...

  5. c++标准模板库:STL

    目录 一.基本概念 STL详细的说明 五大组件 容器 算法 迭代器 仿函数 空间配置器 二.STL优点 原生性 通用性 易用性 其他特性 高可重用性 高性能 移植性 跨平台 小结 三.三大组件 一.基 ...

  6. C++ Primer plus学习笔记-第十六章:string类和标准模板库

    第十六章:string类和标准模板库 前言:这一章已经相当靠近全书的后面部分了:这一章我们会深入探讨一些技术上的细节,比如string的具体构造函数,比如适用于string类的几个函数,比如我们还会介 ...

  7. C++的标准库和C++的标准模板库(STL)

    https://www.cnblogs.com/jpfss/p/10025771.html C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标准头文件中定义.在C++开发 ...

  8. C++标准库和标准模板库(转)

    转自原文http://blog.csdn.net/sxhelijian/article/details/7552499 C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标 ...

  9. C++标准库和标准模板库

    C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标准头文件中定义. 在C++开发中,要尽可能地利用标准库完成.这样做的直接好处包括: (1)成本:已经作为标准提供,何苦再 ...

最新文章

  1. 380万播放量,也许是全网最火的机器学习视频
  2. MySQL存储过程---动态的表名
  3. SQL Server 表和索引存储结构
  4. 企业日志分析之linux系统message收集展示
  5. 十大场景带你玩转外国
  6. c++ 定时器_【话说定时器系列】之十:PWM输入模式测量脉宽及占空比实验
  7. CodeForces - 817F MEX Queries(线段树lazy序)
  8. 如何用python制作九九乘法表_Python一行代码给儿子制作九九乘法表
  9. 数据库优化:8 种常见的SQL错误用法
  10. php只显示一部分文章,typecho同一个页面下调用不同分类的文章但是却只显示一个分类文章...
  11. django外调用url_Django url
  12. 关于高考报志愿的一些规划建议
  13. ci持续集成工程师前景_『中级篇』docker之CI/CD持续集成-项目生成镜像(76)
  14. modelsim不停出现loading……无法仿真
  15. Configuration property name ‘fdfs.thumbImage‘ is not valid---springcloud工作笔记163
  16. 20155301 Exp7 网络欺诈防范
  17. 毕业设计之 ---- 基于JAVA WEB的网上购物系统的设计与实现
  18. 两个数的最大公因数和最小公倍数
  19. 80004005错误代码_0x80004005,教您0x80004005错误代码解决方法
  20. 2022.3.19-2022.3.27每周刷题

热门文章

  1. 科技爱好者周刊(第 219 期):如何防止帐号被黑
  2. 小学计算机软件介绍ppt,小学信息技术优秀课件
  3. 争议南科大 何须尽责朱清时
  4. ByteBuffer总结
  5. 【日常需求】一次使用EasyExcel而引发的问题与思考~
  6. Java数学竞赛的名次情况,网传丘成桐「怒斥」2020丘赛清华排名:一流大学的数学人才,究竟该怎么培养?...
  7. Java第十天:多态 异常处理
  8. 高效的使用DOM操作
  9. 计算机总出现安全警报如何处理,打开文件出现安全警告怎么取消?
  10. SQL语句的书写顺序和解析顺序