L3F.WIN

Github及Hexo的使用

0%

C++ primer 5 代码

第1章 开始

初识输入输出

1
2
3
4
5
6
7
8
9
10
#include <iostream> //载入iostream库

int main() {
std::cout << "Enter two numbers:" << std::endl; //cout输出的ostream类型的对象 <<为输出运算符 endl操纵符结束当前行 标准库定义的所有名字都在命名空间std中
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2; //cin输入的ostream类的对象
std::cout << "The sum of " << v1 << " and " << v2
<< " is " << v1 + v2 << std::endl;
return 0;
}

练习题

  1. 打印”Hello World”

    1
    2
    3
    4
    5
    6
    #include <iostream>

    int main() {
    std::cout << "Hello, World" << std::endl;
    return 0;
    }
  2. 编写乘法运算

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #include <iostream>

    int main() {
    std::cout << "Enter two numbers:" << std::endl;
    int v1 = 0, v2 = 0;
    std::cin >> v1 >> v2;
    std::cout << "The multiplication of " << v1 << " and " << v2 << " is " << v1 * v2 << std::endl;
    return 0;
    }

控制流

while语句

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main() {
int sum = 0, val = 1;
while (val <= 10) {
sum += val;
++val;
}
std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
return 0;
}

练习题

  1. 50到100的整数相加

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #include <iostream>

    int main() {
    int sum = 0, val = 50;
    while (val <= 100) {
    sum += val;
    ++val;
    }
    std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl;
    return 0;
    }
  2. 打印10到0之间的整数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #include <iostream>

    int main() {
    int number = 0, val = 10;
    while (val >= 0) {
    number = val;
    std::cout << number << " " << std::endl;
    --val;
    }
    return 0;
    }
  3. 打印指定范围内的所有整数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    #include <iostream>

    int main() {
    std::cout << "Enter two number:" << std::endl;
    int num1 = 0, num2 = 0;
    std::cin >> num1 >> num2;
    if (num1 < num2) {
    while (num1 <= num2) {
    std::cout << num1 << std::endl;
    ++num1;
    }
    }
    else {
    while (num1 >= num2) {
    std::cout << num1 << std::endl;
    --num1;
    }
    }
    return 0;
    }

for语句

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int main() {
int sum = 0;
for (int val = 0; val <= 10; ++val) {
sum += val;
}

std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
return 0;
}

读取数量不定的输入数据

1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main() {
int sum = 0, val = 0;
while (std::cin >> val) {
sum += val;
}
std::cout << "Sum is: " << sum << std::endl;
return 0;
}

if语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main() {
int currVal = 0, val = 0;
if (std::cin >> currVal) {
int cnt = 1;
while (std::cin >> val) {
if (val == currVal) {
++cnt;
}
else {
std::cout << currVal << " occurs "
<< cnt << " times" << std::endl;
currVal = val;
cnt = 1;
}
}

std::cout << currVal << " occurs" << cnt << " times" << std::endl;
}
return 0;
}

不够完善

类简介

1
2
3
4
5
6
7
8
9
#include <iostream>
#include "Sales_item.h" //不属于标准库的使用""包围

int main() {
Sales_item book;
std::cin >> book;
std::cout << book << std::endl;
return 0;
}

书店程序

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
#include <iostream>
#include "Sales_item.h"

int main() {
Sales_item total; //表村下一条交易记录的变量
//读入第一条交易记录,并确保有数据可以处理
if (std::cin >> total) {
Sales_item trans; //保存和的变量
//读入并处理剩余交易记录
while (std::cin >> trans) {
//如果我们处理相同的书
if (total.isbn() == trans.isbn()) {
total += trans; //更新总销售额
}
else {
//打印前一本书的结果
std::cout << total << std::endl;
total = trans; //total现在表示下一本书的销售额
}
}
std::cout << total << std::endl;
}
else {
//没有输入,警告
std::cout << "No data?!" << std::endl;
return -1;
}
return 0;
}

第2章 变量和基本类型

基本内置类型

类型转换

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main() {
unsigned u = 10, u2 = 42;
std::cout << u2 - u << std::endl; //32
std::cout << u - u2 << std::endl; //4294967264 无符号不能有负数结果
int i = 10, i2 = 42;
std::cout << i2 - i << std::endl; //32
std::cout << i - i2 << std::endl; //-32
std::cout << i - u << std::endl; //0
std::cout << u - i << std::endl; //0
}

练习题

  1. 变量赋值是否正确
    1
    2
    3
    4
    5
    6
    7
    8
    #include <iostream>

    int main() {
    std::cin >> int input_value; //意外的类型
    int i = { 3.14 }; //从double转换到int需要收缩转换
    double salary = wage = 999.99; //wage为声明的标识符
    int i = 3.14; //3
    }

变量

作用域

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

int reused = 42;

int main() {
int unique = 0;
std::cout << reused << " " << unique << std::endl; // 42 0
int reused = 0;
std::cout << reused << " " << unique << std::endl; //0 0
std::cout << ::reused << " " << unique << std::endl; // 42 0
}

练习题

  1. 求j的值
    1
    2
    3
    4
    5
    6
    7
    #include <iostream>
    int i = 42;
    int main() {
    int i = 100;
    int j = i;
    std::cout << "i = " << i << " j = " << j << std::endl; //100
    }

复合类型

引用

练习题

求输出结果

1
2
3
4
5
6
7
#include <iostream>

int main() {
int i, &ri = i;
i = 5; ri = 10;
std::cout << i << " " << ri << std::endl; //10 10
}

指针

练习题

  1. 更改指针的值及所指对象的值

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #include <iostream>

    int main() {
    int val1 = 15, val2 = 16;
    int *p1 = &val1;
    p1 = &val2;
    val2 = 17;
    std::cout << "val1 = " << val1 << " p1 = " << *p1 << " val2 = " << val2 << std::endl; // 由符号*得到指针p1所指的对象 15 17 17
    }
  2. 代码结果

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #include <iostream>

    int main() {
    int i = 42;
    int *p1 = &i;
    *p1 = *p1 * *p1; // 1764

    std::cout << *p1 << std::endl;
    }

const

练习题

初始化是否合法

1
2
3
4
5
6
7
int &r = -1,  i= 0;  //int &r错误无法从int 转换为 &r
int *const p2 = &i2; //错误 i2 为未声明的标识符
const int *const p3 = &i2; //错误i2未声明标识符
const int *p = &i2; //错误i2未声明标识符
const int &const r2; //必须初始化
const int i2 = i, &r = i; //i未声明标识符
const int i = -1, &r = 0; //正确

处理类型

练习题

  1. 计算值

    1
    2
    3
    4
    5
    6
    7
    8
    #include <iostream>

    int main() {
    int a = 3, b = 4;
    decltype(a) c = a;
    decltype((b)) d = a;
    std::cout << ++c << " " << ++d << std::endl; // 4 4
    }
  2. 计算值

    1
    2
    3
    4
    5
    6
    7
    8
    #include <iostream>

    int main() {
    int a = 3, b = 4;
    decltype(a) c = a;
    decltype((a = b)) d = a;
    std::cout << c << " " << d << std::endl; // 3 3
    }

自定义数据类型

练习题

  1. 利用自定义数据类型重写bookshop
    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
    #include <iostream>
    #include <string>

    struct Sales_data {
    std::string bookNo;
    unsigned units_sold = 0;
    double revenue = 0.0;
    };

    int main() {
    double price = 0.0;
    Sales_data item1, item2;
    std::cin >> item1.bookNo >> item1.units_sold >> price;
    item1.revenue = item1.units_sold * price;
    std::cin >> item2.bookNo >> item2.units_sold >> price;
    item2.revenue = item2.units_sold * price;

    if (item1.bookNo == item2.bookNo) {
    unsigned totalCnt = item1.units_sold + item2.units_sold;
    std::cout << "图书" << item1.bookNo << ": 总共销售" << totalCnt << "本,收入" << item1.revenue + item2.revenue << "元" << std::endl;
    }
    else {
    std::cout << "图书" << item1.bookNo << ": 总共销售" << item1.units_sold << "本,收入" << item1.revenue << "元" << std::endl;
    std::cout << "图书" << item2.bookNo << ": 总共销售" << item2.units_sold << "本,收入" << item2.revenue << "元" << std::endl;
    }
    }

第3章 字符串,向量和数组

命名空间的using声明

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

int main() {
int val1;
cin >> val1;
cout << "刚才输入:" << val1 << endl;
return 0;
}

string对象上的操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::endl;
using std::string;

int main() {
string s, s1;

while (cin >> s) {
s1 += s;

}
cout << s1 << endl;
return 0;
}

For处理范围内的字符

1
2
3
4
5
6
7
8
9
10
11
12
string s("Hello World!!!");
//punct_cnt 的类型和s.size的返回类型一样
decltype(s.size()) punct_cnt = 0;
for (auto c : s) {
if (ispunct(c)) {
++punct_cnt;
}
}
cout << punct_cnt
<< " punctuation characters in " << s << endl;

return 0;

练习题

  1. 替换所有字符为X

    1
    2
    3
    4
    5
    6
    7
    8
    9
    string s("Hello World!!!"), x;

    for (auto c : s) {
    c = 'X';
    x = x + c;
    }
    cout << " punctuation characters in " << x << endl;

    return 0;
  2. 删除字符串中的标点符号

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    string s("Hello World!!!"), shuchu;

    for (auto c : s) {
    if (!ispunct(c)) {
    shuchu = shuchu + c;
    }

    }

    cout << "删除符号最后结果 " << shuchu << endl;

    return 0;

vector 对象

练习题

  1. cin读入一组整数并把它们保存到vector对象

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    int val;
    vector<int> number;
    while (cin >> val) {
    number.push_back(val);
    }

    for (int i = 0; i < number.size(); i++) {
    cout << number[i] << endl;
    }

    return 0;
  2. 读入一组字符串

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    string word;
    vector<string> chars;
    while (cin >> word) {
    chars.push_back(word);
    }

    for (int i = 0; i < chars.size(); i++) {
    cout << chars[i] << endl;
    }

    return 0;

迭代器介绍

迭代控制字符串第一个文字大小写

1
2
3
4
5
6
7
8
9
int main() {
string s("some string");
if (s.begin() != s.end()) { //判断字符串是否为空
auto it = s.begin();
*it = toupper(*it); //将当前字符改成大写并返回所指元素的引用
}
cout << s;
return 0;
}

迭代器修改全部字符串

1
2
3
4
5
6
7
8
int main() {
string s("some string");
for (auto it = s.begin(); it != s.end(); ++it) { //循环控制迭代中的字符串 使用isspace()判断是否有空格
*it = toupper(*it);
}
cout << s; //SOME STRING
return 0;
}

第4章 表达式

基础

1
2
3
4
int main() {
cout << 12 / 3 * 4 + 5 * 15 + 24 % 4 / 2 << endl; //91
return 0;
}

练习题判断奇数偶数

1
2
3
4
5
6
7
8
9
10
11
int main() {
int num = 0;
cin >> num;
if (num % 2 == 0) {
cout << num << "偶数" << endl;
}
else {
cout << num << "奇数" << endl;
}
return 0;
}