0%

C++ Primer学习笔记(五)

结构体

  • 定义一个结构体

    1
    2
    3
    4
    5
    struct Sales_data { 
    std::string bookNo;
    unsigned units_sold = 0;
    double revenue = 0.0;
    };
  • 定义结构对象

    1
    2
    3
    4
    struct Sales_data { /* ... */ } accum, trans, *salesptr; 
    // equivalent, but better way to define these objects
    struct Sales_data { /* ... */ };
    Sales_data accum, trans, *salesptr;

    定义结构体以分号结束

#include

预处理器是一个在编译器之前运行的程序,并且会改变我们程序的源码。

当预处理看到一个#include,会将指定头文件的内容替换掉#include

1
2
3
4
5
6
7
8
9
#ifndef SALES_DATA_H
#define SALES_DATA_H
#include <string>
struct Sales_data {
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
#endif

使用#ifndef来防止头文件被多次引用

using declaration

通过使用using形式的定义,来简化写法,比如:using namespace::name

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
// using declaration; when we use the name cin, we get the one from the namespace std
return 0;
}

using std::cin;
int main()
{
int i;
cin >> i; // ok: cin is a synonym for std::cin
cout << i; // error: no using declaration; we must use the full name
std::cout << i; // ok: explicitly use cout from namepsace std

由于定义了using std::cin,我们可以在调用cin函数时,不添加std::namespace前缀。

String

  • 在使用string之前需要引入string头文件

    1
    2
    #include <string>
    using std::string

定义和初始化

1
2
3
4
string s1; // default initialization; s1 is the empty string 
string s2 = s1; // s2 isacopyof s1
string s3 = "hiya"; // s3 is a copy of the string literal
string s4(10, 'c'); // s4 is cccccccccc

复制初始化(copy initialize)和直接初始化(direct initialization)

当使用=时,编译器是复制初始化,而当忽略=时,我们使用的是直接初始化,比如:

1
2
3
4
5
6
7
8
string s5 = "hiya"; // copy initialization
string s6("hiya"); // direct initialization
string s7(10, 'c'); // direct initialization; s7 is cccccccccc

string s8 = string(10, 'c'); // copy initialization; s8 is cccccccccc

string temp(10, 'c'); // temp is cccccccccc
string s8 = temp; // copy temp into s8

读写字符串

1
2
3
4
5
6
7
// Note: #include and using declarations must be added to compile this code int main()
{
string s; // empty string
cin >> s; // read a whitespace-separated string into s
cout << s << endl; // write s to the output
return 0;
}

获取字符串的长度

1
auto len = line.size(); // len has type string::size_type

拼接字符串

1
2
3
string s1 = "hello, ", s2 = "world\n";
string s3 = s1 + s2; // s3 is hello, world\n
s1+=s2; // equivalentto s1=s1+s2
1
2
string s1 = "hello", s2 = "world"; // no punctuation in s1 or s2
string s3 = s1 + ", " + s2 + '\n';

注意+符号两边至少要有一个string类型,不能是字符字面量相加

1
2
3
4
string s4 = s1 + ", "; // ok: adding a string and a literal string 
s5 = "hello" + ", "; // error: no string operand string
s6 = s1 + ", " + "world"; // ok: each + has a string operand string
s7 = "hello" + ", " + s2; // error: can't add string literals
1
string s6 = (s1 + ", ") + "world";

相当于:

1
2
string tmp = s1 + ", "; // ok: + has a string operand
s6 = tmp + "world"; // ok: + has a string operand
1
string s7 = ("hello" + ", ") + s2; // error: can't add string literals

(“hello” + “,”) 符号两边都是常量

Exercise 3.3: Explain how whitespace characters are handled in the string input operator and in the getline function.

对于string类的输入函数,它会自动忽略开头的空白(空格、制表符、换行等等),从第一个真正的字符开始直到下一个空白。

对于getline()函数,它会保存字符串中的空白符,它读入数据,直到遇到换行符位置。

1
2
3
4
5
6
7
8
int main()
{
string line;
// read input a line at a time until end-of-file
while (getline(cin, line))
cout << line << endl;
return 0;
}

答案来自☞C++Primer第五版 第三章习题答案(1~10)