天天看点

C++ Primer(第五版) 第三章练习答案C++ Primer(第五版) 第三章练习答案

C++ Primer(第五版) 第三章练习答案

目录

  • C++ Primer(第五版) 第三章练习答案
    • 3.1
    • 3.2
    • 3.3
    • 3.4
    • 3.5
    • 3.6
    • 3.7
    • 3.8
    • 3.9
    • 3.10
    • 3.11
    • 3.12
    • 3.13
    • 3.14
    • 3.15
    • 3.16
    • 3.17
    • 3.18
    • 3.19
    • 3.20
    • 3.21
    • 3.22
    • 3.23
    • 3.24
    • 3.25
    • 3.26
    • 3.27
    • 3.28
    • 3.29
    • 3.30
    • 3.31
    • 3.32
    • 3.33
    • 3.34
    • 3.35
    • 3.36
    • 3.37
    • 3.38
    • 3.39
    • 3.40
    • 3.41
    • 3.42
    • 3.43
    • 3.44
    • 3.45

3.1

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#if 0
/**
 * 1_9
 */
int main()
{
    int sum = 0;

    int i = 50;
    while (i <= 100)
        sum += i++;

    cout << sum << endl;

    return 0;
}
#elif 0
/**
 * 1_10
 */
int main()
{
    for (int i = 10; i >= 0; --i)
        cout << i << endl;

    return 0;
}
#elif 1
/**
 * 1_11
 */
int main()
{
    int i, j;
    cin >> i >> j;

    if (i < j)
        for (; i <= j; ++i)
            cout << i << endl;
    else if (j < i)
        for (; j <= i; ++j)
            cout << j << endl;
    else 
        cout << i << endl;

    return 0;
}
/**
 * 2_41or42
 */
#endif
           

3.2

#include <string>
#include <iostream>

using namespace std;

int main()
{
    string words;

    // 读入一行
    while (getline(cin, words))
        cout << words << endl;

    // 读入一个词
    while (cin >> words)
        cout << words << endl;

    return 0;
}
           

3.3

/**
 * 跳过空白符
 * 读取空白符
 */
           

3.4

#include <string>
#include <iostream>

using namespace std;

int main()
{
    string str1, str2;

    cin >> str1 >> str2;

    // 输出相等或较大的string
    if (str1 == str2)
        cout << str1 << endl;
    else if (str1 > str2)
        cout << str1 << endl;
    else
        cout << str2 << endl;
    
    // 输出长度相等或较长的string
    if (str1.size() == str2.size())
        cout << str1 << endl;
    else if (str1.size() > str2.size())
        cout << str1 << endl;
    else
        cout << str2 << endl;

    return 0;
}
           

3.5

#include <string>
#include <iostream>

using namespace std;

int main()
{
    string sum, word;

    // 输出连接string
    while (cin >> word)
        sum += word;
    cout << sum << endl;

    // 重置流和string
    cin.clear();
    sum = string();

    // 输出带空格string
    while (cin >> word)
    {
        word += " ";
        sum += word;
    }
    cout << sum << endl;

    return 0;
}
           

3.6

#include <string>
#include <iostream>

using namespace std;

int main()
{
    string str;

    // 输入一行
    while (getline(cin, str))
    {
        for (auto &i : str)
            i = 'X';
        cout << str << endl;
    }

    return 0;
}
           

3.7

#include <string>
#include <iostream>

using namespace std;

int main()
{
    string str;

    // 输入一行
    while (getline(cin, str))
    {
        // 啥都没发生
        for (char &i : str)
            i = 'X';
        cout << str << endl;
    }

    return 0;
}
           

3.8

#include <string>
#include <iostream>

using namespace std;

int main()
{
    string str;

    // 输入一行
    while (getline(cin, str))
    {
        // while
        int i = 0;
        while (i < str.size())
            str[i++] = 'X';

        cout << str << endl;
    }
    
    // 输入一行
    while (getline(cin, str))
    {
        // for
        for (int i = 0; i < str.size(); ++i)
            str[i] = 'X';

        cout << str << endl;
    }

    return 0;
}
           

3.9

#include <string>
#include <iostream>

using namespace std;

int main()
{
    string s;

    // 输出一个字符
    // 下标越界
    cout << s.size() << endl;
    cout << s[0] << endl;

    return 0;
}
           

3.10

#include <string>
#include <iostream>

using namespace std;

int main()
{
    string words;

    while (getline(cin, words))
    {
        for (auto &i : words)
        {
            // 是标点符合跳过
            if (ispunct(i))
                continue;
            cout << i;
        }
        cout << endl;
    }

    return 0;
}
           

3.11

#include <string>
#include <iostream>

using namespace std;

int main()
{
    const string s = "Keep out!";
    // 合法
    // 常量的引用是常量引用 const char & 
    for (auto &c : s) { }

    return 0;
}
           

3.12

#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    // 空
    vector<vector<int>> ivec(2, vector<int>(5, 3));
    // 类型不同
    // vector<string> svec = ivec;
    // 10 个 null
    vector<string> svec(10, "null");

    for (auto &i : ivec)
        for (auto &j : i)
            cout << j << endl;

    return 0;
}
           

3.13

#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    // 空
    vector<int> v1;
    // 10 个 0
    vector<int> v2(10);
    // 10 个 42
    vector<int> v3(10, 42);
    // 10
    vector<int> v4{10};
    // 10 42
    vector<int> v5{10, 42};
    // 10 个 空
    vector<string> v6{10};
    // 10 个 hi
    vector<string> v7{10, "hi"};

    return 0;
}
           

3.14

#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    vector<int> ivec;
    int i;

    while (cin >> i)
        ivec.push_back(i);
    
    for (auto &i : ivec)
        cout << i << endl;

    return 0;
}
           

3.15

#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    vector<string> svec;
    string str;

    while (cin >> str)
        svec.push_back(str);
    
    for (auto &i : svec)
        cout << i << endl;

    return 0;
}
           

3.16

#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    // 空
    vector<int> v1;
    // 10 个 0
    vector<int> v2(10);
    // 10 个 42
    vector<int> v3(10, 42);
    // 10
    vector<int> v4{10};
    // 10 42
    vector<int> v5{10, 42};
    // 10 个 空
    vector<string> v6{10};
    // 10 个 hi
    vector<string> v7{10, "hi"};

    cout << v1.size() << endl;
    cout << v2.size() << endl;
    cout << v3.size() << endl;
    cout << v4.size() << endl;
    cout << v5.size() << endl;
    cout << v6.size() << endl;
    cout << v7.size() << endl;

    return 0;
}
           

3.17

#include <vector>
#include <string>
#include <iostream>
#include <cctype>

using namespace std;

int main()
{
    string word;
    vector<string> svec;

    while (cin >> word)
        svec.push_back(word);
    
    // 转换成大写
    for (auto &i : svec)
        for (auto &j : i)
            j = toupper(j);
    
    for (auto &i : svec)
        cout << i << endl;

    return 0;
}
           

3.18

#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    vector<int> ivec;
    // ivec[0] = 42;
    ivec.push_back(42);

    return 0;
}
           

3.19

#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    // 更好,代码少
    vector<int> vec1(10, 42);

    vector<int> vec2{42, 42, 42, 42, 42, 42, 42, 42, 42, 42};

    vector<int> vec3;
    for (int i = 0; i < 10; ++i)
        vec3.push_back(42);

    return 0;
}
           

3.20

#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    int i;
    vector<int> ivec;

    while (cin >> i)
        ivec.push_back(i);
    
    // 输出相邻的和
    for (decltype(ivec.size()) i = 1; i != ivec.size(); ++i)
    {
        cout << ivec[i - 1] + ivec[i] << endl;
    }

    putchar('\n');

    // 输出第一个和最后一个的和
    for (vector<int>::size_type i = 0; i != ivec.size() / 2; ++i)
    {
        cout << ivec[i] + ivec[ivec.size() - 1 - i] << endl;
    }

    return 0;
}
           

3.21

#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    // 空
    vector<int> v1;
    // 10 个 0
    vector<int> v2(10);
    // 10 个 42
    vector<int> v3(10, 42);
    // 10
    vector<int> v4{10};
    // 10 42
    vector<int> v5{10, 42};
    // 10 个 空
    vector<string> v6{10};
    // 10 个 hi
    vector<string> v7{10, "hi"};

    for (vector<int>::const_iterator beg = v1.cbegin(); beg != v1.cend(); ++beg)
        cout << *beg << " ";
    cout << endl;

    for (auto beg = v2.cbegin(); beg != v2.cend(); ++beg)
        cout << *beg << " ";
    cout << endl;

    for (auto beg = v3.cbegin(); beg != v3.cend(); ++beg)
        cout << *beg << " ";
    cout << endl;

    for (auto beg = v3.cbegin(); beg != v3.cend(); ++beg)
        cout << *beg << " ";
    cout << endl;

    for (auto beg = v4.cbegin(); beg != v4.cend(); ++beg)
        cout << *beg << " ";
    cout << endl;

    for (auto beg = v5.cbegin(); beg != v5.cend(); ++beg)
        cout << *beg << " ";
    cout << endl;

    for (auto beg = v6.cbegin(); beg != v6.cend(); ++beg)
        cout << *beg << " ";
    cout << endl;

    for (auto beg = v7.cbegin(); beg != v7.cend(); ++beg)
        cout << *beg << " ";
    cout << endl;

    return 0;
}
           

3.22

#include <vector>
#include <string>
#include <iostream>
#include <cctype>

using namespace std;

int main()
{
    vector<string> text;
    string str;

    // 输入一行
    while (getline(cin, str))
        text.push_back(str);
    
    // 没到结尾并且不是空行
    for (auto beg = text.begin(); beg != text.end() && !beg->empty(); ++beg)
    {
        // 转换成大写
        for (auto &i : *beg)
            i = toupper(i);

        cout << *beg << endl;
    }

    return 0;
}
           

3.23

#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    int i;
    vector<int> ivec;

    while (cin >> i && ivec.size() != 10)
        ivec.push_back(i);

    // 清理多余输入
    while (getchar() != '\n')
        ;
    
    for (auto beg = ivec.begin(); beg != ivec.end(); ++beg)
        *beg *= 2;
    
    for (auto &i : ivec)
        cout << i << " ";
    cout << endl;

    return 0;
}
           

3.24

#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    int i;
    vector<int> ivec;

    while (cin >> i)
        ivec.push_back(i);
    
    // 输出相邻的和
    for (auto beg = ivec.begin() + 1; beg != ivec.end(); ++beg)
    {
        cout << *(beg - 1) + *beg << " ";
    }
    cout << endl;


    // 输出第一个和最后一个的和                           用小于
    for (auto beg = ivec.begin(), end = ivec.end() - 1; beg < end; ++beg, --end)
    {   
        cout << *beg + *end << " ";
    }
    cout << endl;

    return 0;
}
           

3.25

#include <vector>
#include <string>
#include <iostream>

using namespace std;

int main()
{   
    vector<unsigned int> scores(11, 0);
    unsigned grade;

    while (cin >> grade)
    {
        if (grade <= 100)
            ++*(scores.begin() + grade / 10);
    }

    for (auto &i : scores)
        cout << i << " ";
    cout << endl;

    return 0;
}
           

3.26

#include <vector>
#include <string>
#include <iostream>

using namespace std;

int BinarySearch(vector<int> vec, int key);


int main()
{
    vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int key = -2;

    cout << BinarySearch(vec, key) << endl;

    return 0;
}

int BinarySearch(vector<int> vec, int key)
{
    auto beg = vec.begin();
    auto end = vec.end() + 1;

    while (beg <= end)
    {
        auto mid = beg + (end - beg) / 2;
        if      (key < *mid)   end = mid - 1;
        else if (key > *mid)   beg = mid + 1;
        else    return *mid;
    }
    return -1;
}
           

3.27

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    unsigned buf_size = 1024;
    
    // 不是常量表达式
    int ia[buf_size];
    
    int ia[4 * 7 - 14];

    // 不是常量表达式
    int ia[txt_size()];

    // const char [12]" 类型的值不能用于初始化 "char [11]" 类型的实体
    char st[11] = "fundamental";

    return 0;
}
           

3.28

#include <iostream>
#include <string>

using namespace std;

// 空
string sa[10];

// 0
int ia[10];

int main()
{
    // 空
    string sa2[10];
    // 未定义值
    int ia2[10];

    for (auto &i : ia2)
        cout << "=" << i << "=" << endl;

    return 0;
}
           

3.29

/**
 * 大小固定
 * 没有成员函数
 *

           

3.30

#include <string>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    constexpr size_t array_size = 10;
    int ia[array_size];

    // 下标 >= 0  &&  下标 < array_size
    for (size_t ix = 1; ix <= array_size; ++ix)
        ia[ix] = ix;

    return 0;
}
           

3.31

#include <string>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int arr[10];
    for (int i = 0; i < 10; ++i)
        arr[i] = i;

    return 0;
}
           

3.32

#include <string>
#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

int main()
{
    int arr[10];
    for (int i = 0; i < 10; ++i)
        arr[i] = i;

    int arr2[10];

    memcpy(arr2, arr, sizeof(arr));

    for (auto &i : arr2)
        printf("%d\n", i);
    


    vector<int> vec(10);
    for (int i = 0; i < vec.size(); ++i)
        vec[i] = i;
    
    vector<int> vec2(10);

    vec2 = vec;

    for (auto &i : vec2)
        cout << i << endl;

    return 0;
}
           

3.33

/**
 * 未定义值
 *

           

3.34

#include <string>
#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

int main()
{
    // p2 处于尾后时非法
    p1 += p2 - p1;

    return 0;
}
           

3.35

#include <string>
#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

int main()
{
    int arr[10];

    auto p = begin(arr);

    while (p != end(arr))
        *p++ = 0;

    for (auto &i : arr)
        cout << i << endl;

    return 0;
}
           

3.36

#include <string>
#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

bool eq(int arr[], int arr2[], int size);

int main()
{
    int a[4] = {1, 2, 3, };
    int b[4] = {1, 2, 3, 4};
    
    if (eq(a, b, sizeof(a) / sizeof(int)))
        puts("true");
    else
        puts("false");
    
    vector<int> v = {1, 2, 3, };
    vector<int> v2 = {1, 2, 3, 4};

    if (v == v2)
        puts("true");
    else
        puts("false");

    return 0;
}

bool eq(int arr[], int arr2[], int size)
{
    for (int i = 0; i < size; ++i)
        if (arr[i] != arr2[i])
            return false;

    return true;
}
           

3.37

#include <string>
#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

int main()
{
    const char ca[] = {'h', 'e', 'l', 'l', 'o'};
    const char *cp = ca;
    
    // 没有空字符  循环遇到空字符停止
    while (*cp) {
        cout << *cp << endl;
        ++cp;
    }

    return 0;
}
           

3.38

/**
 * 你家地址 + 我家地址 有什么意义
 *

           

3.39

#include <string>
#include <iostream>
#include <vector>
#include <iterator>
#include <cstring>

using namespace std;

int main()
{
    string str1("LGDSNX"), str2("LGDSNX");
    const char *cstr1 = "LGDSNX", *cstr2 = "LGDSNX";
    
    if (str1 == str2)
        puts("true");
    
    if (strcmp(cstr1, cstr2) == 0)
        puts("true");

    return 0;
}
           

3.40

#include <string>
#include <iostream>
#include <vector>
#include <iterator>
#include <cstring>

using namespace std;

int main()
{
    char cstr1[] = "xiao", cstr2[] = "xiao";
    const auto len = strlen(cstr1) + strlen(cstr2) + 1;

    char *new_str = new char[len];

    strcpy(new_str, cstr1);
    strcat(new_str, cstr2);

    puts(new_str);

    delete[] new_str;

    return 0;
}
           

3.41

#include <string>
#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

int main()
{
    int a[] = {1, 2, 3, 4};

    vector<int> vec(begin(a), end(a));

    return 0;
}
           

3.42

#include <string>
#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

int main()
{
    vector<int> vec(10, 1);

    int arr[10];

    for (int i = 0; i < vec.size(); ++i)
        arr[i] = vec[i];

    return 0;
}
           

3.43

#include <string>
#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

constexpr int row = 3, col = 4;

int main()
{
    int ia[row][col] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };

    for (int (&i)[col] : ia)
    {
        for (int j : i)
            cout << j << " ";
        cout << endl;
    }

    for (int i = 0; i < row; ++i)
    {
        for (int j = 0; j < col; ++j)
            cout << ia[i][j] << " ";
        cout << endl;
    }

    for (int (*i)[col] = begin(ia); i != end(ia); ++i)
    {
        for (int *j = begin(*i); j != end(*i); ++j)
            cout << *j << " ";
        cout << endl;
    }

    return 0;
}
           

3.44

#include <string>
#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

constexpr int row = 3, col = 4;

typedef int arr_4[col];

int main()
{
    int ia[row][col] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };

    for (arr_4 &i : ia)
    {
        for (int j : i)
            cout << j << " ";
        cout << endl;
    }

    for (int i = 0; i < row; ++i)
    {
        for (int j = 0; j < col; ++j)
            cout << ia[i][j] << " ";
        cout << endl;
    }

    for (arr_4 *i = begin(ia); i != end(ia); ++i)
    {
        for (int *j = begin(*i); j != end(*i); ++j)
            cout << *j << " ";
        cout << endl;
    }

    return 0;
}
           

3.45

#include <string>
#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

constexpr int row = 3, col = 4;

int main()
{
    int ia[row][col] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}
    };

    for (auto &i : ia)
    {
        for (int j : i)
            cout << j << " ";
        cout << endl;
    }

    for (int i = 0; i < row; ++i)
    {
        for (int j = 0; j < col; ++j)
            cout << ia[i][j] << " ";
        cout << endl;
    }

    for (auto i = begin(ia); i != end(ia); ++i)
    {
        for (auto j = begin(*i); j != end(*i); ++j)
            cout << *j << " ";
        cout << endl;
    }

    return 0;
}
           

继续阅读