C++ Notes

I got to know something about C++ when learning CS106X-Stanford and Object-Oriented Programming-Zhejiang University . However, I haven’t learned C++ specially. In this summer vacation, I will dig into C++ and keep notes here.

Arrays

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
/* Built-in Array (fixed size) */
// **Initialization**
// the dimension of must be a constant expression,
// or be omitted when using list initialization
int a1[5]; // if defined in a function, it will have undefined values
int a2[] = {1, 2, 3}; // dimension is 3
int a3[5] = {1, 2, 3}; // equivalent to a3[] = {1, 2, 3, 0, 0}
// **Size**
int size = sizeof(a) / sizeof(*a);
// **Iteration**
for (int i = 0; i < size; ++i) { // using subscript
// a[i] ...
}
for (auto element : a) { // traverse the entire array
// element ...
}
for (int *p = begin(a); p != end(a); ++p) { // using pointer
// *p ...
}
// **Sort**
sort(a, a + size);
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
/* vector */
// **Initialization**
vector<int> v0; // empty vector
vector<int> v1(5, 1); // 5 elements with value 1
vector<int> v2(5); // 5 elements with default value 0
vector<int> v3{1, 2, 3}; // equivalent = vector<int> v3 = {1, 2, 3};
// **Copy**
vector<int> v4(v0);
vector<int> v5 = v4;
// **Size**
v.size();
// **Iteration**
for (int i = 0; i < v.size(); ++i) {
// v[i] ...
}
for (auto ele : v) {
// ele ...
}
for (auto it = v.begin(); it != v.end(); ++it) {
// *it ...
}
// **Sort**
sort(v.begin(), v.end());

string

Ways to initialize a string

string s1; // Default initialization; s1 is the empty string.

string s2(s1); // s2 is a copy of s1.

string s2 = s1; // Equivalent to s2(s1), s2 is a copy of s1.

string s3("value"); // s3 is a copy of the string literal, not including the null.

string s3 = "value"; // Equivalent to s3("value"), s3 is a copy ofo the string literal.

string s4(n, 'c'); // Initialize s4 with n copies of the character 'c'.

copy initialize: initialize a variable using =

direct initialize: omit the =

Input operator

The string input operator (is >> s) reads and discards any leading whitespace (e.g., spaces, newlines, tabs). It then reads characters until the next whitespace character is encountered.

getline(is, s) reads a line of input from is into s. This function reads the given stream up to and including the first newline and stores what it read - not including the newline - in its string argument.

Size

size returns a string::size_type value. It is an unsigned type. Try to avoid problems due to conversion between unsigned and int by not using ints in expressions that use size().

Adding Literals and strings

For historical reasons, and for compatibility with C, string literals are not standard library strings. It is important to remember that these types differ when you use string literals and library strings.

When we mix strings and string or character literals, at least one operand to each + operator must be of string type. Thus, it is illegal to add two string literals. (error: invalid operands to binary expression ('const char *' and 'const char *'))

Generic Programming

Use != rather than < in for loops.

Use iterators rather than subscripts.