C++ Primer 从入门到放弃 之 第三章 练习题

3.1 节练习

练习 3.1

#include <iostream>
using namespace std;

int main(){
    cout << "using namesapce std" << endl;
    string inPut;
    cin >> inPut;
    cout << "Your input is: " << inPut << endl;
    return 0;
}

3.2

3.2.1

3.2.2 节练习

#include <iostream>
#include <string>
using namespace std;

int main(){

    // 3.2
    string input;
    while(getline(cin, input)){
        cout << "Your input is: " + input << endl; 
        if (input == "break"){
            break;
        }
    }
    while(cin >> input){
        cout << "The word You input is: " + input << endl; 
        if (input == "break") break;
    }

    /*
    3.3
    输入运算符会忽略开头的空白,直到遇到第一个字符开始,直到读取到第一个空格为止。
    getline(cin,s)会读取一行输入的所有内容,直到读取到换行符为止。
    */

    // 3.4
    string a, b;
    while(cin >> a >> b){
        if (a == b){
            cout << a + " is equal to " + b << endl;
        }else
        {
            cout << a + " is not equal to " + b << endl;
            if (a.size() > b.size()){
                cout << a + " is longer than " + b << endl;
            }else{
                cout << b + " is longer than " + a << endl;
            }
        }
        if (a == "break" || b == "break") break;
    }

    // 3.5
    string res;
    while (cin >> input){
        if (res == "") res = input;
        else res = res + " " + input;
    }
    cout << res << endl;
    
    return 0;
}

3.2.3 节练习

#include <iostream>
#include <string>
using namespace std;

int main(){
    // 3.6
    string s;
    cin >> s;
    for(auto &c: s){
        c = 'X';
    }
    cout << s << endl;

    // 3.7 
    // s do not change
    cin >> s;
    for(auto c: s){
        c = 'X';
    }
    cout << s << endl;

    // 3.8
    cin >> s;
    decltype(s.size()) index = 0;
    if (!s.empty()){
        while (index < s.size()){
            s[index] = 'X';
            ++index;
        }
    }
    cout << s << endl;
    cin >> s;
    for (decltype(s.size()) i = 0; i < s.size(); ++i){
        s[i] = 'X';
    }
    cout << s << endl;

    /*
    3.9
    不合法,s是空字符串,没有index 0。
    */

    // 3.10
    cout << "remove punc" << endl;
    cin >> s;
    string res;
    for(auto c : s){
        if (!ispunct(c)) res += c;
    }
    cout << res << endl;

    /*
    3.11
    合法,因为字符串s是常量,对常量的引用是合法的,但不可以对引用赋值,c的类型是const char &
    */
    const string tmp("qwe");
    for (auto &c : tmp) cout << c << endl;

    return 0;
}

3.3

3.3.1 节练习

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int main(){
    // 3.12
    vector<vector<int>> ivec;
    // int vector 不可以拷贝给 string vector
    vector<string> s{10, "hi"};

    /* 3.13
    a 没有元素
    b 10个0
    c 10个42
    d 1个10
    e 10和42
    f 10个空字符串
    g 10个hi
    */

    return 0;
}

3.3.2 节练习

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(){
    string input;
    vector<string> strs;
    while (cin >> input){
        if (input == "break") break;
        strs.push_back(input);
    }
    cout << strs.size() << endl;

    int val;
    vector<int> vals;
    while(cin >> input) vals.push_back(val);
    cout << vals.size() << endl;

    return 0;
}

3.3.3 节练习

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(){

    // 3.17
    string input;
    vector<string> strs;
    while (cin >> input){
        if (input == "break") break;
        for (auto &c : input) c = toupper(c);
        strs.push_back(input);
    }
    for (auto &s : strs) cout << s << endl;

    // 3.18
    // 不合法,vector<int> ivec(1);

    // 3.19 v1更好
    vector<int> v1(10, 42);
    vector<int> v2{42,42,42,42,42,42,42,42,42,42};
    vector<int> v3;
    for (decltype(v3.size()) i = 0; i != 10; ++i){
        v3.push_back(42);
    }
    for (auto i:v1) cout << i;
    cout << endl;
    for (auto i:v2) cout << i;
    cout << endl;
    for (auto i:v3) cout << i;
    cout << endl;

    // 3.20
    int val;
    vector<int> vals;
    while (cin >> val){
        if (val == -1) break; 
        vals.push_back(val);
    }
    for (decltype(vals.size()) i = 0; i < vals.size(); i += 2){
        if (i + 1 < vals.size())
            cout << vals[i] + vals[i+1] << endl;
        else
            cout << vals[i] << endl;
    }

    vector<int> vals2;
    while (cin >> val){
        vals2.push_back(val);
    }
    for (decltype(vals2.size()) i = 0; i < vals2.size(); ++i){
        cout << vals2[i] + vals2[vals2.size()-1-i] << endl;
    }

    return 0;
}

3.4

3.4.1 节练习

#include <iostream>
#include <string>
#include <vector>
using namespace std;

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

    for (auto it = v1.cbegin(); it != v1.cend(); ++it) cout << *it;
    cout << endl;
    for (auto it = v2.cbegin(); it != v2.cend(); ++it) cout << *it;
    cout << endl;
    for (auto it = v3.cbegin(); it != v3.cend(); ++it) cout << *it;
    cout << endl;
    for (auto it = v4.cbegin(); it != v4.cend(); ++it) cout << *it;
    cout << endl;
    for (auto it = v5.cbegin(); it != v5.cend(); ++it) cout << *it;
    cout << endl;
    for (auto it = v6.cbegin(); it != v6.cend(); ++it) cout << *it;
    cout << endl;
    for (auto it = v7.cbegin(); it != v7.cend(); ++it) cout << *it;
    cout << endl;

    // 3.22
    vector<string> text{"qwe", "", "asd", "", "zxc"};
    for (auto it = text.cbegin(); it != text.cend() && !it->empty(); ++it)
        cout << *it << endl;
    
    // 3.23
    vector<int> vals;
    for(int i = 0; i != 10; ++i) vals.push_back(i);
    for(auto it = vals.begin(); it != vals.end(); ++it) *it = 2 * *it;
    for(auto val:vals) cout << val << " ";
    cout<<endl;

    return 0;
}

3.4.2 节练习

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(){
    // 3.24
    vector<int> vals;
    for(int i=0; i != 10; ++i) vals.push_back(i);
    for(auto begin = vals.begin(), end = vals.end() - 1; begin != vals.end(); ++begin, --end){
        cout << *begin + *end << " ";
    }
    cout << endl;

    for(auto begin = vals.begin(); begin < vals.end(); begin += 2){
        if ((begin + 1) < vals.end())
            cout << *begin + *(begin + 1) << " ";
        else
            cout << *begin << " ";
    }
    cout << endl;

    // 3.25
    vector<unsigned> scores(11);
    unsigned input;
    while (cin >> input){
        if (input <= 100){
            auto it = scores.begin() + input / 10;
            *it += 1; 
        }
    }
    for (auto i:scores) cout << i << " ";
    cout << endl;

    // 3.26 会溢出。因为迭代器要么指向score中的一个位置,
    //      要么指向score最后一个位置的下一个位置。
    // 若begin为第五个位置,end为最后一个位置的下一个位置,
    // begin + end不会指向end后五个位置。
    vector<unsigned> scores(11);
    auto b = scores.begin(), e = scores.end();
    auto mid = b + (e - b) / 2;

    return 0;
}

3.5

3.5.1 节练习

#include <iostream>
#include <string>
using namespace std;

string sa[10];
int ia[10];
int main(){
    /* 3.27
    a 不合法,buf_size应为常量
    b 合法
    c 不合法,应返回const int
    d 不合法,应给 \0 留空间
    */
    const unsigned bf = 10;
    int ary1[bf];
    int ary2[7 * 7 - 9];
    char a[10] = "123456789";

    // 3.28 sa和sa2是空字符串,ia是0,ia2是随机初始化
    string sa2[10];
    int ia2[10];
    for (auto i:sa) cout << i << " ";
    cout << endl;
    for (auto i:ia) cout << i << " ";
    cout << endl;
    for (auto i:sa2) cout << i << " ";
    cout << endl;
    for (auto i:ia2) cout << i << " ";
    cout << endl;

    // 3.29 不能改变长度

    return 0;
}

3.5.2 节练习

#include <iostream>
#include <vector>
using namespace std;

int main(){
    // 3.30 下标是从0开始
    // for(size_t i = 0; i < array_size; ++i)

    // 3.31
    const size_t sz = 10;
    int array[sz];
    for (size_t i = 0; i < sz; ++i) array[i] = i;
    for (auto c:array) cout << c << " ";
    cout << endl;

    // 3.32
    int array_cp[sz];
    for (size_t i = 0; i < sz; ++i) array_cp[i] = array[i];
    for (auto c:array_cp) cout << c << " ";
    cout << endl;

    vector<int> vals;
    for(decltype(vals.size()) i = 0; i < 10; ++i) vals.push_back(i);
    for (auto c:vals) cout << c << " ";
    cout << endl;

    // 3.33 score的值未被定义,将被随机初始化

    return 0;
}

3.5.3 节练习

#include <iostream>
#include <string>
#include <vector>
//#include <iterator>
using namespace std;

int main(){

    // 3.34 将p1移动至p2的位置
    int a[10];
    int *p1 = a;
    int *p2 = end(a);
    p1 += p2 - p1;
    p2 += p1 - p2;

    // 3.35
    for (auto c:a) cout << c << " ";
    cout << endl;
    p1 = begin(a), p2 = end(a);
    for (p1; p1 != p2; ++p1) *p1 = 0;
    for (auto c:a) cout << c << " ";
    cout << endl;

    // 3.36
    vector<int> iv1(10,2), iv2(10,3);
    if (iv1 > iv2) cout << "iv1 > iv2" << endl;
    if (iv1 < iv2) cout << "iv1 < iv2" << endl;
    else cout << "iv1 = iv2" << endl;

    int a1[10] = {1,2,3,4,5,6,7,8,9,0}, a2[10] = {1,2,3,4,5,6,7,8,9,0};
    auto sz1 = end(a1) - begin(a1), sz2 = end(a2) - begin(a2);
    if (sz2 != sz1) cout << "a1 != a2" << endl;
    else{
        int *pa1 = a1, *pa2 = a2;
        while (pa1 != end(a1) && pa2 != end(a2)){
            if (*pa1 != *pa2){
                cout << "a1 != a2" << endl;
                break;
            }
            ++pa1, ++pa2;
        }
        if (pa1 == end(a1) && pa2 == end(a2)) cout << "a1 = a2" << endl;
    }

    return 0;
}

3.5.4 节练习

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
using namespace std;

int main(){

    // 3.37 显示定义ca时没有加 \0,直到遇到 \0 时输出才会结束
    const char ca[] = {'h', 'e', 'l', 'l', 'o', '\0'};
    const char *p = ca;
    while (*p){
        cout << *p << " ";
        ++p;
    }
    cout << endl;

    // 3.38 指针本身的值代表指针的地址,指针+指针等于地址+地址,新的地址没有意义

    // 3.39
    string s1 = "hello", s2 = "hell";
    cout << (s1 > s2) << endl;

    const char c1[] = {'h', 'e', 'l', 'l', 'o', '\0'}, c2[] = {'h', 'e', 'l', 'l', '\0'};
    cout << (strcmp(c1, c2)) << endl;

    // 3.40
    char c3[42];
    strcpy(c3, c1);
    strcat(c3, " ");
    strcat(c3, c2);
    for (char *p = c3; *p; ++p) cout << *p;
    cout << endl;

    return 0;
}

3.5.5 节练习

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main(){

    // 3.41
    int array[] = {1,2,3,4,5};
    vector<int> vi(begin(array), end(array));
    for(auto i : vi) cout << i << " ";
    cout << endl;

    // 3.42
    vector<int> vals(10,2);
    int arr[10];
    int *p = arr;
    for (decltype(vals.size()) i = 0; i < vals.size(); ++i){
        cout << vals[i] << " ";
        *p = vals[i];
        ++p;
    }
    cout << endl;
    for(p = begin(arr); p != end(arr); ++p) cout << *p << " ";
    cout << endl;

    return 0;
}

3.6 节练习

#include <iostream>
using namespace std;

int main(){
    constexpr int row = 3, col = 4;
    int mat[row][col] = {0};

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

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

    for (int (*p)[col] = mat; p != mat + row; ++p){
        for (int *q = *p; q != *p + col; ++q){
            cout << *q << " ";
        }
        cout << endl;
    }
    cout << endl;

    // 3.44
    using int_array = int[4];
    for (int_array *p = mat; p != mat + row; ++p){
        for (int *q = *p; q != *p + 4; ++q){
            cout << *q << " ";
        }
        cout << endl;
    }
    cout << endl;

    // 3.45
    for(auto &p: mat){
        for (auto q: p){
            cout << q << " ";
        }
        cout << endl;
    }
    cout << endl;


    return 0;
}

版权声明:本文为bestination原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。