-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12.list_initialization.cpp
More file actions
198 lines (136 loc) · 3.74 KB
/
Copy path12.list_initialization.cpp
File metadata and controls
198 lines (136 loc) · 3.74 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
//
// Created by satellite on 2023-04-18.
//
//列表初始化
//#define initializer list test
//#define use initializer list
//#define use_initializer_list_inFuncLisat
//#define initializer_list_ret
#define initializer_list_type_narrow
#ifdef initializer list test
#include <vector>
#include <map>
using namespace std;
int a[]={1,3,5};//C++98通过,C++11通过
int b[]{2,4,6};//C++98失败,C++11通过
vector<int> c{1,3,5};//C++98失败,C++11通过
map<int,float>d=
{{1,1.0f},{2,2.0f},{5,3.2f}};//C++98失败,C++11通过
#endif
/*
自动变量和全局变量的初始化在c++11中背丰富了:
等号“=”加上赋值表达式(assignment-expression),比如int a=3+4。
等号“=”加上花括号式的初始化列表,比如int a={3+4}。
圆括号式的表达式列表(expression-list),比如int a(3+4)。
花括号式的初始化列表,比如int a{3+4}。
最后两种也可以用于获取都内存new操作符中:
int *i = new int(1);
double *d = new double(1.2f);
*/
/*
标准模板库中容器对初始化列表
的支持源自<initializer_list>这个头文件中initialize_list类模板的支
持。程序员只要#include了<initializer_list>头文件,并且声明一个以
initialize_list<T>模板类为参数的构造函数,同样可以使得自定义的
类使用列表初始化。
*/
#ifdef use_initializer_list_inConstructor
#include <vector>
#include <string>
using namespace std;
//自定义类使用初始化列表初始化
enum Gender{boy,girl};
class People{
public:
People(initializer_list<pair<string,Gender>>l) //提供了以initialize_list为参数的购构造函数
{
auto i=l.begin();
for(;i!=l.end();++i)
data.push_back(*i);
}
private:
vector<pair<string,Gender>>data;
};
People ship2012={{"Garfield",boy},{"HelloKitty",girl}};
#endif
#ifdef use_initializer_list_inFuncLisat
//函数参数列表中使用初始化列表
#include <initializer_list>
#include "vector"
#include "iostream"
using namespace std;
void Fun(initializer_list<int> iv) {
for(auto i = iv.begin();i!=iv.end();++i)
{
cout<<*i<<' ';
}
}
int main1() {
Fun({1, 2});
Fun({});//空列表
}
class Mydata{
public:
Mydata& operator[](initializer_list<int> l)
{
for(auto i=l.begin();i!=l.end();++i)
idx.push_back(*i);
return*this;
}
Mydata& operator=(int v)
{
if(idx.empty()!=true){
for(auto i=idx.begin();i!=idx.end();++i){
d.resize((*i>d.size())?*i:d.size());
d[*i-1]=v;
}
idx.clear();
}
return*this;
}
void Print(){
for(auto i=d.begin();i!=d.end();++i)
cout<<*i<<"";
cout<<endl;
}
private:
vector<int> idx;//辅助数组,用于记录index
vector<int> d;
};
int main(){
Mydata d;
d[{2,3,5}]=7;
d[{1,4,5,8}]=4;
d.Print();//4 7 7 4 4 0 0 4
}
#endif
#ifdef initializer_list_ret
#include "vector"
#include "deque"
using namespace std;
//初始化列表做函数返回
//返回一个初始化列表,通常会导致构造一个临时变量
vector<int> Func() {return {1,2}; }
//使用初始化列表构造一个vector<int>的临时对象并返回
//如果返回值是一个引用类型,则会返回一个临时对象的引用,注意加const或使用右值引用
const vector<int>& Func1() {return {1,2};}
vector<int>&& Func2() {return {1,2};}
auto ini = {1,2,3,5};
#endif
#ifdef initializer_list_type_narrow
const int x=1024;
const int y=10;
char a=x;//收窄,但可以通过编译
char*b=new char(1024);//收窄,但可以通过编译
//采用的是赋值表达符及圆括号
//式的表达式初始化,所以虽然它们的数据类型明显收窄(char通常取
//值范围为-128到127),却不会引发编译失败
char c={x};//收窄,无法通过编译
char d={y};//可以通过编译
unsigned char e{-1};//收窄,无法通过编译
float f{7};//可以通过编译,7在float类型中可以被表示
int g{2.0f};//收窄,无法通过编译
float*h=new float{1e48};//收窄,无法通过编译
float i=1.2l;//可以通过编译,1.2在float类型中可以被表示
//在C++11中,列表初始化是唯一一种可以防止类型收窄的初始化方式
#endif