![](https://img-blog.csdnimg.cn/20190927151124774.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
C++与STL
odk
哈哈⊙ω⊙
像我这样平凡的人
展开
-
STL:string容器常用操作
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string.h> #include <string> using namespace std; //string的构造函数 void test01() { string str; string str1("hello"); string str2(str1); string str3(5, 'k'); cout << str原创 2021-07-31 16:33:38 · 74 阅读 · 0 评论 -
STL:vector容器常用操作
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string.h> #include <string> #include <vector> #include<algorithm> using namespace std; /* vector迭代器*/ void test01() { vector<int> v; v.push_back(1); v.p原创 2021-07-31 16:35:22 · 130 阅读 · 0 评论 -
STL:deque容器小案例
有5名选手:选手ABCDE,10个评委分别对每一名选手打分,去除最高分,去除评委中最低 分,取平均分 创建五名选手,放到vector中 遍历vector容器,取出来每一个选手,执行for循环,可以把10个评分打分存到deque容 器中 sort算法对deque容器中分数排序,pop_back pop_front去除最高和最低分 deque容器遍历一遍,累加分数,累加分数/d.size() person.score = 平均分 #include "stdafx.h" #include <iostre原创 2021-07-31 18:39:35 · 86 阅读 · 0 评论 -
STL:内建函数对象记录
STL内建了一些函数对象。分为:算数类函数对象,关系运算类函数对象,逻辑运算类仿函 数。这些仿函数所产生的对象,用法和一般函数完全相同,当然我们还可以产生无名的临时 对象来履行函数功能。使用内建函数对象,需要引入头文件 #include 。 6个算数类函数对象,除了negate是一元运算,其他都是二元运算。 template T plus//加法仿函数 template T minus//减法仿函数 template T multiplies//乘法仿函数 template T divides//除法仿函数原创 2021-08-02 13:11:19 · 84 阅读 · 0 评论 -
C++:异常
一.异常的基本概念 1.异常: 出错后,将出错问题返回给调用处 2.c语言的异常处理比较简单,容易出错(如下),c++处理异常不容易出错 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string.h> #include <string> using namespace std; int mydive(int a, int b) { if (b == 0) return -1; //e原创 2021-07-29 16:53:08 · 69 阅读 · 0 评论 -
C++:类型转换
一.静态转换 static_cast<待转换的类型>(待转换的数据) 1.static_cast可以用来转换基本的内置数据类型 int char double… 2.static_cast不能转换没有发生继承关系之间的类 3.static_cast可以用来转换发送继承关系之间的类,但是不保存安全性 4.不能用来转换指针 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string.h> #incl原创 2021-07-29 14:59:37 · 90 阅读 · 0 评论 -
C++:类模板分文件问题
类模板成员函数的创建时机 要弄清楚类模板之所以.h文件和.c文件写在一起,首先要知道类模板成员函数的创建时机 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string.h> #include <stdlib.h> #include <string> using namespace std; class A { public: void showA() { cout <原创 2021-07-29 13:59:36 · 418 阅读 · 0 评论 -
C++:类模板遇到继承
1.类模板遇到继承 在继承时,继承的类必须是一个模板类<> 2.类模板遇到继承,可以将子类写成类模板 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string.h> #include <stdlib.h> #include <string> using namespace std; template <class T> class Base { publi原创 2021-07-29 13:31:07 · 153 阅读 · 0 评论 -
C++:函数模板的本质
编译过程 函数模板的本质 就是进行二次编译 第一次对函数模板进行编译,第二次在调用处对函数模板展开,进行二次编译原创 2021-07-29 12:23:52 · 142 阅读 · 0 评论 -
C++:菱形继承和虚继承
菱形继承 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string.h> #include <stdlib.h> #include <string> using namespace std; class animal { public: int age; }; class sheep:virtual public animal { public: int id; }; c原创 2021-07-29 10:41:51 · 70 阅读 · 0 评论 -
C++:浅拷贝和深拷贝
先考虑一种情况,对一个已知对象进行拷贝,编译系统会自动调用一种构造函数——拷贝构造函数,如果用户未定义拷贝构造函数,则会调用默认拷贝构造函数。 先看一个例子,有一个学生类,数据成员时学生的人数和名字: #include <iostream> using namespace std; class Student { private: int num; char *name; public: Student(); ~Student(); }; St原创 2021-07-28 19:25:25 · 653 阅读 · 0 评论 -
C++:delete void*问题
#define _CRT_SECURE_NO_WARNINGS 2 #include 3 #include <string.h> 4 #include 5 using namespace std; 6 class Person 7 { 8 public: 9 Person(int age,char *str) 10 { 11 cout << “有参构造” << endl; 12 mAge = age; 13 name = (char *)malloc(strlen(s原创 2021-07-28 18:47:42 · 682 阅读 · 0 评论 -
C++:explicit关键字
先考虑一种情况,对一个已知对象进行拷贝,编译系统会自动调用一种构造函数——拷贝构造函数,如果用户未定义拷贝构造函数,则会调用默认拷贝构造函数。 先看一个例子,有一个学生类,数据成员时学生的人数和名字: #include using namespace std; class Student { private: int num; char *name; public: Student(); ~Student(); }; Student::Student() { name = new char(20)原创 2021-07-28 18:45:23 · 59 阅读 · 0 评论 -
C++:静态成员及单例模式
先考虑一种情况,对一个已知对象进行拷贝,编译系统会自动调用一种构造函数——拷贝构造函数,如果用户未定义拷贝构造函数,则会调用默认拷贝构造函数。 先看一个例子,有一个学生类,数据成员时学生的人数和名字: #include using namespace std; class Student { private: int num; char *name; public: Student(); ~Student(); }; Student::Student() { name = new char(20)原创 2021-07-28 18:35:41 · 269 阅读 · 0 评论