有趣的网站之家,做招聘网站的怎么让人注册简历,电脑培训班一般需要多少钱,网站建设 服务质量保证1.const数据类型和constexpr的运用 const定义的值不能被改变#xff0c;在整个作用域中都保持固定#xff0c;当然#xff0c;可以通过函数以形参的形式输入函数。代码如下#xff1a;
#include iostream
using namespace std;constexpr int fibonacci(const int …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值可以修改//n1 //2.n值不可以修改会报错coutfibonacci(n)endl; //3.n可以以形参的形式在函数内参与计算// 4,5为数组的定义数组的长度必须为常数int num1[fibonacci(n)1];//4.这种传入正确//5.int num2[foo(2)];这种传入错误,程序会报错return 0;
}
2.vector数据类型
参考博客vector类型介绍-CSDN博客。
标准库集合或动态数组我们可以放若干对象放在里面。vector他能把其他对象装进来也被称为容器.如
1vectorint vjihe; // 表示这个集合对象保存的是int型数据 // int:模板vector本身就是个类模板int实际上就是类模板的实例化过程。
2vectorstudent vstudlist;// 存放学生类型的集合
3vectorvectorstring strchuan; // 相当于二维的字符串
4vectorint * vjihe2 // 不能向集合中装引用
5vectorint vjihe3; // 引用知识一个别名不是对象
6vectorstring mystr; // 创建一个string类型的空的集合 //push_back() mystr.push_back(“abc”); mystr.push_back(“efg”);
7元素拷贝 vectorstring mystr2(mystr); // 将mystr元素拷贝给mystr2vectorstring mystr3 mystr; // 将mystr元素拷贝给mystr3
8用列表初始化方法给值用{}括起来 vectorstring mystr4 {“aaa”,”bbb”,”ccc”};
9创建指定数量的元素
vectorint ijihe(15,-200);// 创建15个int类型的元素每个元素的值为-200
vectorstring sjihe(15,”hello”);// 创建15个int类型的元素每个元素的值为hello
vectorint ijihe2(15); // 15个元素每一个元素值默认为0
vectorstring sjihe(15); // 15个元素每一个元素值默认为空 等等,代码如下
#include iostream
#include vector
using namespace std;int main2() {vectorint 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.初始化列表 c11首先是把初始化列表的概念绑定到类型上并将其称之为initializer_list允许构造函数或者其他函数像参数一样使用初始化列表这就为类对象的初始化与普通数组和POD的初始化方法提供了统一的桥梁。下面是由类写的代码如下
#include initializer_list
#include vector
#include iostream
using namespace std;//建立一个类
class Mag {public:vectorint vec;Mag(initializer_listint 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;tupleint,double,string f() { //元组返回不同类型的数return make_tuple(1,2.3,456);
}int maint() {cout ..................tuple................. endl;auto tp f();//返回一个元组auto自动给元组的各元素分配数据类型cout get0(tp) get1(tp) get2(tp) endl;//元组的输出元素return 0;
}
5.带参数的类的构造函数
代码如下
#include iostream
using namespace std;//类1
class Base {public:int value1;int value22;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;
}