C++11/14的新特性(更简洁)

这篇文章主要介绍了C++11/14的新特性(更简洁),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

新的字符串表示方式――原生字符串(Raw String Literals)

C/C++中提供了字符串,字符串的转义序列,给输出带来了很多不变,如果需要原生义的时候,需要反转义,比较麻烦。

C++提供了,原生字符串,即字符串中无转义,亦无需再反义。详细规则见带码:

 #include  using namespace std; string path = "C:\Program Files (x86)\alipay\aliedit\5.1.0.3754"; string path2 = "C:\\Program Files (x86)\\alipay\\aliedit\\5.1.0.3754"; //更简洁的表示 string path3 = R"(C:\Program Files (x86)\alipay\aliedit\5.1.0.3754)"; string path4 = R"(C:\Program "Files" (x86)\\alipay\aliedit\5.1.0.3754)"; int main(int argc, char *argv[]) {   cout<

新的for循环――for(x:range)

C++为 for 提供 for range 的用法。

 #include  #include  #include  using namespace std; int main(int argc, char *argv[]) { string str = "china";    //!字符数组 for(auto ch: str) { cout< vs = {"abc","xyz","mnq"};   vector::iterator itr = vs.begin();   for(; itr != vs.end(); itr++)   {     cout<<*itr< mis={{1,"c++"},{2,"java"},{3,"python"}};   map::iterator itr = mis.begin();   for(; itr != mis.end(); ++itr)   {     cout<<(*itr).first<<"\t"<second<

新的初始化的方式――Initializer List

1)常规方法――normal init

 #include  #include  #include  #include  using namespace std; int main(int argc, char *argv[]) { #if 0   vector vi(5);   cout< vi2(5,10);   for(auto i: vi2){     cout< vi3;   for(int i=0; i<10; i++){     vi3.push_back(i);   }   for(auto i: vi3){     cout< li(5);   cout< li2(5,10);   cout< li3;   for(int i=0; i<10; i++)   {     li3.push_back(i);   }   cout< mis;   mis.insert(pair(1,"c++"));   mis.insert(pair(2,"java"));   mis.insert(pair(3,"python"));   mis.insert(map::value_type(4,"c"));   mis.insert(map::value_type(5,"php"));   for(auto is: mis)   {   cout<

2)初始化列表――Initializer List

 #include  #include  #include  #include  using namespace std; int main(int argc, char *argv[]) { vector iv = {1,2,3,4,5}; list li = {1,2,3,4,5}; map mis = {{1,"c"},{2,"c++"}, {3,"java"},{4,"scala"}, {5,"python"}}; mis.insert({6,"ruby"}); // map::iterator itr = mis.begin(); // for(; itr != mis.end(); ++itr) // { // cout<first<< itr->second<

3)initializer_list(作入参)

 #include  #include  using namespace std; template  class MyArray { private: vector m_Array; public: MyArray() { } MyArray(const initializer_list& il) { for (auto x : il) m_Array.push_back(x); } }; int main() { MyArray foo = { 3, 4, 6, 9 }; return 0; }

统一的初始化风格(Uniform initialization)

C++中的初始化风格,大体有如下形式:

 int a = 2; //"赋值风格"的初始化 int aa [] = { 2, 3 }; //用初始化列表进行的赋值风格的初始化 complex z(1, 2); //"函数风格"的初始化

C++ 11 中,允许通过以花括号的形式来调用构造函数。这样多种对象构造方式便可以统一起来了:

 int a = { 2 }; int aa [] = { 2, 3 }; complex z = { 1, 2 };
 #include  using namespace std; class complex { public: complex(int x, int y) :_x(x),_y(y){} private: int _x; int _y; }; complex func(const complex & com) { return {1,2}; } int main(int argc, char *argv[]) { int a = 10; int aa[] = {1,2,3}; complex com(1,2); //--------------------------- int a_ = {1}; int aa_[] = {1,2,3}; complex com_ = {1,2}; func({1,2}); return 0; } 

auto自动类型推导

1)引入

 #include  using namespace std; int func() { return 8; } int main(int argc, char *argv[]) { auto i = 5; auto &ri = i; auto rf = func(); const auto *p = &ri; static auto si = 100; return 0; }

2)语法

auto 能够实现类型的自我推导,并不代表一个实际的类型声明。auto 只是一个类型声明的占位符。
auto 声明的变量,必须马上初始化,以让编译器推断出它的实际类型,并在编译时将 auto 占位符替换为真正的类型。

3)用法

  • 不用于函数参数
 #include  #include  using namespace std; //void foo(auto i) //{ // cout< vi; auto ivcp = vi; // vector va = vi; return 0; }
  • 常用于STL

如迭代器的初始化,容器拷贝等。

decltype-类型指示器

1)获取表达式类型

auto 类型,作为占位符的存在来修饰变量,必须初始化,编译器通过初始化来确定 auto 所代表的类型。即必须定义变量。

如果,我仅希望得到类型,而不是具体的变量产生关系,该如何作到呢?decltype(expr); expr 代表被推导的表达式。由decltype推导所声明难过的变量,可初始化,也可不初始化。 

 #include  using namespace std; int func() { return 1; } int main(int argc, char *argv[]) { int a = 10; cout<

2)推导规则

decltype(expr); 所推导出来的类型,完全与 expr 类型一致。同 auto 一样,在编译期间完成,并不会真正计算表达式的值。
应用

3)decltype与typedef联合应用

 #include  #include  #include  using namespace std; int main(int argc, char *argv[]) { vector vi = {1,2,3,4,5,0}; typedef decltype(vi.begin()) Itr; for(Itr itr = vi.begin(); itr != vi.end(); ++itr) { cout<<*itr< mis; mis.insert(map::value_type(1,"abc")); mis.insert(decltype(mis)::value_type(2,"java")); typedef decltype(map::value_type()) Int2String;    mis.insert(Int2String(3,"c++")); for(auto& is:mis) { cout<

4)decltype +auto

C++11 增了返回类型后置(trailing-return-type,或跟踪返回类型),将 decltype 和 auto结合起来完成返回类型的推导。 

 #include  using namespace std; template R add(T a, U b) { return a+b; } template auto add2(T a, U b)->decltype(a+b) { return a+b; } int main(int argc, char *argv[]) { int a = 1; float b = 1.1; auto ret = add(a,b); cout<(a,b); cout<

仿函数(functor)

1)语法

重载了 operator()的类的对象,在使用中,语法类型于函数。故称其为仿函数。此种用法优于常见的函数回调。 

 class Add { public: int operator()(int x, int y) { return x+y; } }; 

2)应用

 #include  using namespace std; class Add { public: int operator()(int x, int y) { return x+y; } }; int main(int argc, char *argv[]) { int a = 1 , b = 2; Add add; cout<

3)提高(带状态的functor)

相对于函数,仿函数,可以拥用初始状态,一般通过 class 定义私有成员,并在声明对象的时候,进行初始化。私有成员的状态,就成了仿函数的初始状态。而由于声明一个仿函数对象可以拥有多个不同初始状态的实例。 

 #include  using namespace std; class Tax { public: Tax(float r, float b):_rate(r),_base(b){} float operator()(float money) { return (money-_base)*_rate; } private: float _rate; float _base; }; int main(int argc, char *argv[]) { Tax high(0.40,30000); Tax middle(0.25,20000); Tax low(0.12,10000); cout<<"大于 3w 的税:"<

以上就是C++11/14的新特性(更简洁)的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » C语言