今天看了一本英文书,简单做了翻译对于c++新特性经常用但却不知道辨别,虽然c++17都已经出来了!
一起学习吧!
主要:初始化,auto、nullptr等
//C++ 11 新特性
#include "stdafx.h"
#include <vector>
#include<list>
#include <set>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//第一个 书写格式
list<int> list1;
vector<list<int> > listw; //之前的
listw.push_back(list1);
vector < list<int>> list2; //C++11 支持种写法
list2.push_back(list1);
//添加nullptr 代替NULL 0
#include "stdafx.h"
#include <vector>
#include<list>
#include <set>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//第一个 书写格式
list<int> list1;
vector<list<int> > listw; //之前的
listw.push_back(list1);
vector < list<int>> list2; //C++11 支持种写法
list2.push_back(list1);
//添加nullptr 代替NULL 0
void f(int);
void f(void *);
void f(void *);
f(0);
f(NULL); //有歧义不知道调用上面的哪一个函数
f(nullptr); //这个解决了这个问题调用f(void *)
f(NULL); //有歧义不知道调用上面的哪一个函数
f(nullptr); //这个解决了这个问题调用f(void *)
//添加了一个auto 的数据类型,支持自定义转换数据类型
auto i = 42;
double f();
auto d = f(); //就自动的变为double类型
auto i = 42;
double f();
auto d = f(); //就自动的变为double类型
//auto i; //wrong 必须要初始化
static auto vat = 0.21; //添加前缀也是准许的
//auto 在递归是的应用
static auto vat = 0.21; //添加前缀也是准许的
//auto 在递归是的应用
vector<string> v;
auto pos = v.begin(); //pos = vector<string>::interaator 是不是方便多了
//在lambada 表达式中的应用
auto pos = v.begin(); //pos = vector<string>::interaator 是不是方便多了
//在lambada 表达式中的应用
auto L = [](int x)->bool{ //具体的使用方法可以学习lambada表达式的使用
return x;
};
return x;
};
//c++11的初始化方式
int valures[] {1, 2, 4};
std::vector<int> v{ 2, 4, 56, 7, 78, 8, 9, 9 };
std::vector<string> s{ "monday", "tuesday" };
int x1(5.3);
int x2 = 5.3;
//int x3{ 5.3 };//ERROR 不被润许
/// int x4 = {5.3}; error
char c{ 7 };//ok
//char c{ 999 }; //wrong out of char
int valures[] {1, 2, 4};
std::vector<int> v{ 2, 4, 56, 7, 78, 8, 9, 9 };
std::vector<string> s{ "monday", "tuesday" };
int x1(5.3);
int x2 = 5.3;
//int x3{ 5.3 };//ERROR 不被润许
/// int x4 = {5.3}; error
char c{ 7 };//ok
//char c{ 999 }; //wrong out of char
vector<int> v1{1, 3, 5, 6, 78, 8};
//vector<int> v2{23,345,5,4.4}; wrong 不准许类型不一致
//vector<int> v2{23,345,5,4.4}; wrong 不准许类型不一致
//对于循环for
vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
//利用这种方式进行遍历替换了迭代器
for(auto &delm: vec)
{
cout << delm << endl;
}
vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
//利用这种方式进行遍历替换了迭代器
for(auto &delm: vec)
{
cout << delm << endl;
}
//第二种bian遍历
int array[] = { 1, 3, 4, 56, 7, 78 };
for(auto &x : array)
{
cout << x << endl;
int array[] = { 1, 3, 4, 56, 7, 78 };
for(auto &x : array)
{
cout << x << endl;
}
//最重要的改变
std::set<int> coll;
coll.insert(1);
coll.insert(move(1)); //注意这里,在这里创建对象不进行重新生成一次拷贝构造,而是掉用原来已有的!
//最重要的改变
std::set<int> coll;
coll.insert(1);
coll.insert(move(1)); //注意这里,在这里创建对象不进行重新生成一次拷贝构造,而是掉用原来已有的!
//keyword contexpr
contexpr int squre(int) //支持在编译时期被检测使用
return x*x;{
}
return 0;
}
}