Accelerated C++ 学习笔记:Chapter1, Work with strings
1.1, New concept:
1.) variable
2.) object
3.) definition
4.) local variable
5.) destroys
6.) interface
7.) initialize
8.) empty
9.) null
10.) whitespace (space, tab, backspace, or the end of the line)
11.) buffer
12.) flushes
13.) convert
14.) concatenate
15.) overloaded
16.) const
17.) construct
18.) member function
19.) character literal
20.) char
1.2, Sample code:
1 // ask for a person's name, and generate a framed greeting 2 #include <iostream> 3 #include <string> 4 5 int main() 6 { 7 std::cout << "Please enter your first name: "; 8 std::string name; 9 std::cin >> name; 10 11 // build the message that we intend to write 12 const std::string greeting = "Hello, " + name + "!"; 13 14 // build the second and fourth lines of the output 15 const std::string spaces(greeting.size(), ' '); 16 const std::string second = "* " + spaces + " *"; 17 18 // build the first and fifth lines of the output 19 const std::string first(second.size(), '*'); 20 21 // write it all 22 std::cout << std::endl; 23 std::cout << first << std::endl; 24 std::cout << second << std::endl; 25 std::cout << "* " << greeting << " *" << std::endl; 26 std::cout << second << std::endl; 27 std::cout << first << std::endl; 28 29 return 0; 30 }
1.3, Important notes:
1.) In general, the input-output library saves its output in an internal data structure called a buffer, which it uses to optimize output operations.
There are three events that cause the system to flush the buffer:
First, the buffer might be full, in which case the library will flush it automatically.
Second, the library might be asked to read from the standard input stream. In that case, the library immediately flushes the output buffer without waiting for the buffer to become full.
The third occasion for flushing the buffer is when we explicitly say to do so.(Writing the value of std::endl ends the line of output, and then flushes the buffer, which forces the system to write to the output stream immediately.)
2.) ( to be added...)
1.4, Details
Types:
char: Built-in type that holds ordinary characters as defined by the implementation.
wchar_t: Built-in type intended to hold "wide characters," which are big enough to hold characters for languages such as Japanese.
The string type is defined in the standard header <string>;. An object of type string contains a sequence of zero or more characters. If n is an integer, c is a char, is is an input stream, and os is an output stream, then the string operations include
std::string s;
Defines s as a variable of type std::string that is initially empty.
std::string t = s;
Defines t as a variable of type std::string that initially contains a copy of the characters in s, where s can be either a string or a string literal.
std::string z(n, c);
Defines z as a variable of type std::string that initially contains n copies of the character c. Here, c must be a char, not a string or a string literal.
os << s
Writes the characters contained in s, without any formatting changes, on the output stream denoted by os. The result of the expression is os.
is >> s
Reads and discards characters from the stream denoted by is until encountering a character that is not whitespace. Then reads successive characters
from is into s, overwriting whatever value s might have had, until the next character read would be whitespace. The result is is.
s + t
The result of this expression is an std::string that contains a copy of the characters in s followed by a copy of the characters in t. Either s or t, but
not both, may be a string literal or a value of type char.
s.size()
The number of characters in s.
Variables can be defined in one of three ways:
std::string hello = "Hello"; // define the variable with an explicit initial value
std::string stars(100, '*'); // construct the variable
// according to its type and the given expressions
std::string name; // define the variable with an implicit initialization,
// which depends on its type
Variables defined inside a pair of curly braces are local variables/which exist only while executing the part of the program within the braces. When the implementation reaches the }, it destroys the variables, and returns any memory that they occupied to the system. Defining a variable as const promises that the variable's value, will not change during its lifetime. Such a variable must be initialized as part of its definition, because there is no way to do so later.
Input: Executing std::cin >> v discards any whitespace characters in the standard input stream, then reads from the standard input into variable v. It returns std::cin, which has type istream, in order to allow chained input operations.