-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpp_3_19_chapter6.cpp
More file actions
136 lines (112 loc) · 2.93 KB
/
cpp_3_19_chapter6.cpp
File metadata and controls
136 lines (112 loc) · 2.93 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
#include <iostream>
#include <initializer_list>
#include <string>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
void exercise6_10(int *pA, int *pB)
{ // 指针交换两个int的值
int nTemp = *pA;
*pA = *pB;
*pB = nTemp;
}
void exercise6_12(int &nA, int &nB)
{ // 引用交换两个int的值
int nTemp = nA;
nA = nB;
nB = nTemp;
}
void exercise6_27(std::initializer_list<int> il)
{ // 使用 initializer_list计算列表的元素和
int nResult{0};
for(auto beg = il.begin(); beg != il.end(); ++beg)
{
nResult += *beg;
}
cout << nResult << endl;
}
void test1(std::initializer_list<string> sl)
{ // 查看elem的类型
for(const auto &elem : sl)
cout << elem << endl;
}
int exercise6_33(vector<int>::iterator &itBegin, vector<int>::iterator &itEnd)
{ // 递归输出vector对象
if(itBegin != itEnd)
{
cout << *itBegin++ << endl;
exercise6_33(itBegin, itEnd);
}
return 0;
}
int exercise6_33(const vector<int> &vnTest)
{ // 递归输出vector对象
static auto viBegin = vnTest.begin();
if(viBegin != vnTest.end())
{
cout << *viBegin++ << endl;
exercise6_33(vnTest);
}
return 0;
}
void test2()
{ // 测试,当vector赋值时,begin和end都变化,用第一个vector的迭代器根据第二个vector的值比对,会出现错误,实际上时迭代器会直接++到第二个迭代器的对应值
int k = 0;
vector<int> i{1, 2, 3, 4};
vector<int> j{i};
auto p = i.begin();
auto q = j.end();
while(p != q)
{
cout << k++ << "\t" << *p << endl;
p++;
}
}
string (&exercise6_36(void))[10]
{ // 返回数组的引用
static string sTest[10]{"a", "b"};
return sTest;
}
using strarr = string[10];
strarr &exercise6_37_1(void)
{ // 类型别名
static string sTest[10]{"a", "b"};
return sTest;
}
auto exercise6_37_2(void) -> string(&)[10]
{ // 后置返回类型
static string sTest[10]{"a", "b"};
return sTest;
}
string nArr[10];
decltype(nArr) &exercise6_37_3(void)
{ // decltype关键字返回
static string sTest[10]{"a", "b"};
return sTest;
}
int main(int argc, char const *argv[])
{
// int nA = 1, nB = 2;
// exercise6_10(&nA, &nB);
// cout << nA << "\t" << nB << endl;
// exercise6_12(nA, nB);
// cout << nA << "\t" << nB << endl;
// exercise6_27({1, 2, 3, 4, 5});
// test1({"a", "b", "cd", "efg"});
// vector<int> vnTest{1, 2, 3, 4, 5, 6, 7};
// auto Begin = vnTest.begin();
// auto End = vnTest.end();
// exercise6_33(Begin, End);
// exercise6_33(vnTest);
// test2();
// string (&sReturn)[10] = exercise6_36();
// for(auto i : sReturn)
// cout << i << endl;
// string (&sReturn)[10] = exercise6_37_1();
// string (&sReturn)[10] = exercise6_37_2();
// string (&sReturn)[10] = exercise6_37_3();
return EXIT_SUCCESS;
}