《C++ Primer》5th 课后练习 第三章 字符串、向量和数组 11~20

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_40758751/article/details/102733510

练习3.11 下面的范围for语句合法吗?如果合法,c的类型是什么?

const string s = "Keep out!";
for(auto &c : s){ /* ... */ }

如果for循环体中不对c做修改,则是合法的,c为const string &型。

练习3.12 下列vector对象的定义有不正确的吗?如果有,请指出来。对于正确的,描述其执行结果;对于不正确的,说明其错误的原因。

vector<vector<int>> ivec;         // 正确(C++11中),创建了一个类型为vector<int>的vector对象ivec
vector<string> svec = ivec;       // 错误,类型不一样
vector<string> svec(10, "null");  // 正确,创建了内容为10个"null"对象的对象svec

知识点:vector的初始化:

1:引用不可以成为vector的元素,因为其不是对象。

2:可以用花括号初始化每一个值。

3:可以用括号指定元素个数或相同的元素值。

4:只能使用直接初始化,不可以使用拷贝初始化vector之间的拷贝是可行的,但要保证类型相同)

练习3.13 下列的vector对象各包含多少个元素?这些元素的值分别是多少?

vector<int> v1;                // size:0,  no values.
vector<int> v2(10);            // size:10, value:0
vector<int> v3(10, 42);         // size:10, value:42
vector<int> v4{ 10 };            // size:1,  value:10
vector<int> v5{ 10, 42 };         // size:2,  value:10, 42
vector<string> v6{ 10 };            // size:10, value:""
vector<string> v7{ 10, "hi" };     // size:10, value:"hi"

练习3.14 编写一段程序,用cin读入一组整数并把它们存入一个vector对象。

#include<iostream>
#include<vector>

using namespace std;
int main()
{
    int temp;
    vector<int> v;
    while(cin >> temp)
    {
        v.push_back(temp);
    }
    for (auto i : v)
        cout << i << " ";
    return 0;
}

练习3.15 改写上题程序,不过这次读入的是字符串。

#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
    string temp;
    vector<string> v;
    while(cin >> temp)
    {
        v.push_back(temp);
    }
    for (auto i : v)
        cout << i << " ";
    return 0;
}

练习3.16 编写一段程序,把练习3.13中vector对象的容量和具体内容输出出来。

#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
    vector<int> v1;                // size:0,  no values.
    vector<int> v2(10);            // size:10, value:0
    vector<int> v3(10, 42);         // size:10, value:42
    vector<int> v4{ 10 };            // size:1,  value:10
    vector<int> v5{ 10, 42 };         // size:2,  value:10, 42
    vector<string> v6{ 10 };            // size:10, value:""
    vector<string> v7{ 10, "hi" };     // size:10, value:"hi"
    cout << "v1: " << v1.capacity() << endl;
    for (int i : v1)
        cout << i << " ";
    cout << endl;
    cout << "v2: " << v2.capacity() << endl;
    for (int i : v2)
        cout << i << " ";
    cout << endl;
    cout << "v3: " << v3.capacity() << endl;
    for (int i : v3)
        cout << i << " ";
    cout << endl;
    cout << "v4: " << v4.capacity() << endl;
    for (int i : v4)
        cout << i << " ";
    cout << endl;
    cout << "v5: " << v5.capacity() << endl;
    for (int i : v5)
        cout << i << " ";
    cout << endl;
    cout << "v6: " << v6.capacity() << endl;
    for (string i : v6)
        cout << i << " ";
    cout << endl;
    cout << "v7: " << v7.capacity() << endl;
    for (string i : v7)
        cout << i << " ";
    cout << endl;
    return 0;
}

练习3.17 从cin读入一组词并把它们存入一个vector对象,然后设法把所有词都改为大写形式。输出改变后的结果,每个词占一行。

#include<iostream>
#include<vector>
#include<string>
#include<cctype>
using namespace std;
int main()
{
    string temp;
    vector<string> v;
    while(cin >> temp)
    {
        for (auto &c : temp)
            c = toupper(c);
        v.push_back(temp);
    }
    for (auto i : v)
        cout << i << " ";
    cout << endl;
    return 0;
}

练习3.18 下面的程序合法吗?如果不合法,你准备如何修改?

vector<int> ivec;
ivec[0] = 42;

不正确,应改为

ivec.push_back(42);

练习3.19 如果想定义一个含有10个元素的vector对象,所有元素的值都是42,请例举三种不同的实现方法,哪种方式更好呢?

vector<int> ivec(10, 42); //最好?
vector<int> ivec;
for(int i = 0; i < 10; i++)
    ivec.push_back(42);
vector<int> ivec{42,42,42,42,42,42,42,42,42,42};

练习3.20 读入一组整数并把他们存入一个vector对象,将每对相邻整数的和输出出来。改写你的程序,这次要求先输出第一个和最后一个元素的和,接着输入第二个和倒数第二个元素的和,以此类推。

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    int temp;
    vector<int> v;
    while (cin >> temp)
    {
        v.push_back(temp);
    }
    for (int i = 1; i < v.size(); i++)
        cout << v[i - 1] + v[i] << " ";
    cout << endl;
    return 0;
}
#include<iostream>
#include<vector>
using namespace std;
int main()
{
    int temp;
    vector<int> v;
    while (cin >> temp)
    {
        v.push_back(temp);
    }
    for (int i = 0; i < v.size(); i++)
        cout << v[i] + v[v.size()-i-1] << " ";
    cout << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40758751/article/details/102733510
今日推荐