-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathExer13_48.cpp
More file actions
68 lines (66 loc) · 1.68 KB
/
Exer13_48.cpp
File metadata and controls
68 lines (66 loc) · 1.68 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
#include <iostream>
#include <vector>
#include "Exer13_44_47_49_String.h"
using std::cout;
using std::endl;
using std::vector;
void foo(String x){}
void bar(const String& x){}
String baz()
{
String ret("world");
return ret;
}
int main()
{
cout << "general test1:" << endl;
String s0;
String s1("hello");
String s2(s0);
String s3 = s1;
s2 = s1;
cout << "general test2:" << endl;
foo(s1);
bar(s1);
foo("temporary");
bar("temporary");
String s4 = baz();
cout << "vector test1:" << endl;
vector<String> svec;
svec.push_back(s0);
svec.push_back(s1);
svec.push_back(baz());
svec.push_back("good job");
cout << "vector test2:" << endl;
vector<String> v;
for(int i = 0; i != 10; ++i)
v.push_back("string");
v.clear();
if(v.empty())
cout << "v is empty" << endl;
return 0;
}
// ******results******
// general test1:
// String(const String&) is called
// String(const String&) is called
// String& operator=(const String&) is called
// general test2:
// String(const String&) is called
// vector test1:
// String(const String&) is called
// String(const String&) is called
// String(const String&) is called
// String(const String&) is called
// String(const String&) is called
// String(const String&) is called
// String(const String&) is called
// vector test2:
// String(const String&) is called
// ...(25 times in total)
// Note #1: part of the tests come from
// https://github.com/Mooophy/Cpp-Primer/blob/master/ch13/ex13_48.cpp
// which is quoted from
// http://coolshell.cn/articles/10478.html
// Note #2: the result shows that copy constructor is called more times than
// push_back is called.