新的字符串表示方式——原生字符串(Raw String Literals)
C/C++中提供了字符串,字符串的轉(zhuǎn)義序列,給輸出帶來了很多不變,如果需要原生義的時候,需要反轉(zhuǎn)義,比較麻煩。
C++提供了,原生字符串,即字符串中無轉(zhuǎn)義,亦無需再反義。詳細(xì)規(guī)則見帶碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <iostream> 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<<path<<endl; cout<<path2<<endl; cout<<path3<<endl; cout<<path4<<endl; return 0; } |
新的for循環(huán)——for(x:range)
C++為 for 提供 for range 的用法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#include <iostream> #include <vector> #include <map> using namespace std; int main( int argc, char *argv[]) { string str = "china" ; //!字符數(shù)組 for (auto ch: str) { cout<<ch<<endl; } int arr[] = {1,2,3,4}; //!普通數(shù)組 for (auto i: arr) { cout<<i<<endl; } vector<string> vs = { "abc" , "xyz" , "mnq" }; vector<string>::iterator itr = vs.begin(); for (; itr != vs.end(); itr++) { cout<<*itr<<endl; } //!vector for (auto &s : vs) { cout<<s<<endl; } map< int ,string> mis={{1, "c++" },{2, "java" },{3, "python" }}; map< int ,string>::iterator itr = mis.begin(); for (; itr != mis.end(); ++itr) { cout<<(*itr).first<< "\t" <<itr->second<<endl; } //!map for (auto &pair: mis) { cout<<pair.first<< "\t" <<pair.second<<endl; } return 0; } |
新的初始化的方式——Initializer List
1)常規(guī)方法——normal init
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
#include <iostream> #include <vector> #include <list> #include <map> using namespace std; int main( int argc, char *argv[]) { #if 0 vector< int > vi(5); cout<<vi.size()<<vi.capacity()<<endl; vector< int > vi2(5,10); for (auto i: vi2){ cout<<i<<endl; } vector< int > vi3; for ( int i=0; i<10; i++){ vi3.push_back(i); } for (auto i: vi3){ cout<<i<<endl; } list< int > li(5); cout<<li.size()<<endl; for (auto &i:li){ cout<<i<<endl; } list< int > li2(5,10); cout<<li2.size()<<endl; for (auto &i:li2){ cout<<i<<endl; } list< int > li3; for ( int i=0; i<10; i++) { li3.push_back(i); } cout<<li3.size()<<endl; for (auto &i:li3){ cout<<i<<endl; } #endif map< int ,string> mis; mis.insert(pair< int ,string>(1, "c++" )); mis.insert(pair< int ,string>(2, "java" )); mis.insert(pair< int ,string>(3, "python" )); mis.insert(map< int ,string>::value_type(4, "c" )); mis.insert(map< int ,string>::value_type(5, "php" )); for (auto is: mis) { cout<<is.first<< "\t" <<is.second<<endl; } mis[6] = "scala" ; mis[7] = "basic" ; mis[8] = "ruby" ; for (auto &is: mis) { cout<<is.first<< "\t" <<is.second<<endl; } return 0; } |
2)初始化列表——Initializer List
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#include <iostream> #include <vector> #include <list> #include <map> using namespace std; int main( int argc, char *argv[]) { vector< int > iv = {1,2,3,4,5}; list< int > li = {1,2,3,4,5}; map< int ,string> mis = {{1, "c" },{2, "c++" }, {3, "java" },{4, "scala" }, {5, "python" }}; mis.insert({6, "ruby" }); // map<int,string>::iterator itr = mis.begin(); // for(; itr != mis.end(); ++itr) // { // cout<<itr->first<< itr->second<<endl; // } for (auto &is: mis) { cout<<is.first<<is.second<<endl; } return 0; } |
3)initializer_list<T>(作入?yún)ⅲ?/p>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#include <iostream> #include <vector> using namespace std; template < typename T> class MyArray { private : vector<T> m_Array; public : MyArray() { } MyArray( const initializer_list<T>& il) { for (auto x : il) m_Array.push_back(x); } }; int main() { MyArray< int > foo = { 3, 4, 6, 9 }; return 0; } |
統(tǒng)一的初始化風(fēng)格(Uniform initialization)
C++中的初始化風(fēng)格,大體有如下形式:
1
2
3
|
int a = 2; //"賦值風(fēng)格"的初始化 int aa [] = { 2, 3 }; //用初始化列表進(jìn)行的賦值風(fēng)格的初始化 complex z(1, 2); //"函數(shù)風(fēng)格"的初始化 |
C++ 11 中,允許通過以花括號的形式來調(diào)用構(gòu)造函數(shù)。這樣多種對象構(gòu)造方式便可以統(tǒng)一起來了:
1
2
3
|
int a = { 2 }; int aa [] = { 2, 3 }; complex z = { 1, 2 }; |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
#include <iostream> 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自動類型推導(dǎo)
1)引入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include <iostream> 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 能夠?qū)崿F(xiàn)類型的自我推導(dǎo),并不代表一個實(shí)際的類型聲明。auto 只是一個類型聲明的占位符。
auto 聲明的變量,必須馬上初始化,以讓編譯器推斷出它的實(shí)際類型,并在編譯時將 auto 占位符替換為真正的類型。
3)用法
- 不用于函數(shù)參數(shù)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
#include <iostream> #include <vector> using namespace std; //void foo(auto i) //{ // cout<<i<<endl; //} int main( int argc, char *argv[]) { int arr[10] = {0}; auto aa = arr; //!auto == const int * cout<< sizeof (aa)<< sizeof (aa)<<endl; // auto aaa[10] = arr; //!錯誤的用法:C/C++中數(shù)組不可以直接賦值的屬性是不可違背的。 vector< int > vi; auto ivcp = vi; // vector<auto> va = vi; return 0; } |
- 常用于STL
如迭代器的初始化,容器拷貝等。
decltype-類型指示器
1)獲取表達(dá)式類型
auto 類型,作為占位符的存在來修飾變量,必須初始化,編譯器通過初始化來確定 auto 所代表的類型。即必須定義變量。
如果,我僅希望得到類型,而不是具體的變量產(chǎn)生關(guān)系,該如何作到呢?decltype(expr); expr 代表被推導(dǎo)的表達(dá)式。由decltype推導(dǎo)所聲明難過的變量,可初始化,也可不初始化。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#include <iostream> using namespace std; int func() { return 1; } int main( int argc, char *argv[]) { int a = 10; cout<< sizeof (a)<<endl; decltype(a) b = 20; //!decltype(a) == int decltype(a+b) c = 30; cout<<a<<b<<c<<endl; const int & cira = a; decltype(cira) cirb = b; cout<<cira<<cirb<<endl; int *pa = &a; decltype(pa) pb = &b; cout<<&a<< "\t" <<pa<<endl; cout<<&b<< "\t" <<pb<<endl; decltype(func()) df; cout<< sizeof (df)<<endl; return 0; } |
2)推導(dǎo)規(guī)則
decltype(expr); 所推導(dǎo)出來的類型,完全與 expr 類型一致。同 auto 一樣,在編譯期間完成,并不會真正計算表達(dá)式的值。
應(yīng)用
3)decltype與typedef聯(lián)合應(yīng)用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
#include <iostream> #include <vector> #include <map> using namespace std; int main( int argc, char *argv[]) { vector< int > vi = {1,2,3,4,5,0}; typedef decltype(vi.begin()) Itr; for (Itr itr = vi.begin(); itr != vi.end(); ++itr) { cout<<*itr<<endl; } map< int ,string> mis; mis.insert(map< int ,string>::value_type(1, "abc" )); mis.insert(decltype(mis)::value_type(2, "java" )); typedef decltype(map< int ,string>::value_type()) Int2String; mis.insert(Int2String(3, "c++" )); for (auto& is:mis) { cout<<is.first<<is.second<<endl; } return 0; } |
4)decltype +auto
C++11 增了返回類型后置(trailing-return-type,或跟蹤返回類型),將 decltype 和 auto結(jié)合起來完成返回類型的推導(dǎo)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#include <iostream> using namespace std; template < typename R, typename T, typename U> R add(T a, U b) { return a+b; } template < typename R, typename T, typename U> 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<decltype(a+b), int , float >(a,b); cout<<ret<<endl; auto ret2 = add2<decltype(a+b)>(a,b); cout<<ret2<<endl; return 0; } |
仿函數(shù)(functor)
1)語法
重載了 operator()的類的對象,在使用中,語法類型于函數(shù)。故稱其為仿函數(shù)。此種用法優(yōu)于常見的函數(shù)回調(diào)。
1
2
3
4
5
6
7
8
|
class Add { public : int operator()( int x, int y) { return x+y; } }; |
2)應(yīng)用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <iostream> 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<<add(a,b)<<endl; return 0; } |
3)提高(帶狀態(tài)的functor)
相對于函數(shù),仿函數(shù),可以擁用初始狀態(tài),一般通過 class 定義私有成員,并在聲明對象的時候,進(jìn)行初始化。私有成員的狀態(tài),就成了仿函數(shù)的初始狀態(tài)。而由于聲明一個仿函數(shù)對象可以擁有多個不同初始狀態(tài)的實(shí)例。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#include <iostream> 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 的稅:" <<high(37500)<<endl; cout<< "大于 2w 的稅:" <<middle(27500)<<endl; return 0; } |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/wangkeqin/p/9285682.html