1.const数据类型和constexpr的运用
const定义的值不能被改变,在整个作用域中都保持固定,当然,可以通过函数以形参的形式输入函数。代码如下:
#include <iostream>
using namespace std;constexpr int fibonacci(const int n) {return n == 1 || n == 2 ? 1 : fibonacci(n - 1) + fibonacci(n - 2);
}int foo(int i) {return i;
}int mains() {const int n = 10;int num = 10;num += 1; //1.num值可以修改//n+=1 //2.n值不可以修改,会报错cout<<fibonacci(n)<<endl; //3.n可以以形参的形式在函数内参与计算// 4,5为数组的定义,数组的长度必须为常数int num1[fibonacci(n)+1];//4.这种传入正确//5.int num2[foo(2)];这种传入错误,程序会报错return 0;
}
2.vector数据类型
参考博客vector类型介绍-CSDN博客。
标准库:集合或动态数组,我们可以放若干对象放在里面。vector他能把其他对象装进来,也被称为容器.如
(1)vector<int> vjihe; // 表示这个集合对象保存的是int型数据; // <int>:模板,vector本身就是个类模板,<int>实际上就是类模板的实例化过程。
(2)vector<student> vstudlist;// 存放学生类型的集合;
(3)vector<vector<string>> strchuan; // 相当于二维的字符串;
(4)vector<int *> vjihe2 // 不能向集合中装引用;
(5)vector<int &> vjihe3; // 引用知识一个别名,不是对象;
(6)vector<string> mystr; // 创建一个string类型的空的集合;
//push_back()
mystr.push_back(“abc”);
mystr.push_back(“efg”);
(7)元素拷贝
vector<string> mystr2(mystr); // 将mystr元素拷贝给mystr2vector<string> mystr3 = mystr; // 将mystr元素拷贝给mystr3
(8)用列表初始化方法给值,用{}括起来
vector<string> mystr4 = {“aaa”,”bbb”,”ccc”};
(9)创建指定数量的元素
vector<int> ijihe(15,-200);// 创建15个int类型的元素,每个元素的值为-200
vector<string> sjihe(15,”hello”);// 创建15个int类型的元素,每个元素的值为hello
vector<int> ijihe2(15); // 15个元素,每一个元素值默认为0
vector<string> sjihe(15); // 15个元素,每一个元素值默认为空
等等,代码如下:
#include <iostream>
#include <vector>
using namespace std;int main2() {vector<int> vec = { 1,2,3,4 };vec.push_back(1);//在vec后面添加一个元素int nums = vec.size();//返回vec元素的个数for (int i = 0; i < nums; i++) {cout << vec[i] << endl;//打印vec的元素}cout << 1 << endl;return 0;
3.初始化列表
c++11首先是把初始化列表的概念绑定到类型上,并将其称之为initializer_list,允许构造函数或者其他函数像参数一样使用初始化列表,这就为类对象的初始化与普通数组和POD的初始化方法提供了统一的桥梁。下面是由类写的代码如下:
#include <initializer_list>
#include <vector>
#include <iostream>
using namespace std;//建立一个类
class Mag {public:vector<int> vec;Mag(initializer_list<int> list) {//使用auto关键字进行类推导for (auto it = list.begin(); it != list.end(); ++it) {vec.push_back(*it);}}};int mainc() {cout << ".............classes start..............." << endl;Mag mag = {1,2,3,4,5};cout << "mag: ";for (auto it = mag.vec.begin(); it != mag.vec.end(); ++it) {cout << *it << ", ";}cout << endl;return 0;
}
4.auto通过元组存取不同类型的数据
代码如下(tuple和auto的作用在代码中有解释):
#include <iostream>
#include <tuple>
#include <string>
using namespace std;tuple<int,double,string> f() { //元组返回不同类型的数return make_tuple(1,2.3,"456");
}int maint() {cout << "..................tuple................." << endl;auto tp = f();//返回一个元组,auto自动给元组的各元素分配数据类型cout << get<0>(tp) << " " << get<1>(tp) << " " << get<2>(tp) << endl;//元组的输出元素return 0;
}
5.带参数的类的构造函数
代码如下:
#include <iostream>
using namespace std;//类1
class Base {public:int value1;int value2=2;Base(int value) {value1 = value;}
};//类2
class Line {public:void setLength(double len);double getlength(void);Line();//构造函数
private:double length;};//成员函数定义,包括构造函数
Line::Line(void) {cout << "Object is being created!!!" << endl;
}void Line::setLength(double len) {length = len;
}double Line::getlength(void) {return length;
}int maino() {//类1Base b(3);cout << b.value2 << endl;cout << b.value1 << endl;//类2Line line;line.setLength(6.0);cout << line.getlength() << endl;return 0;
}