C++ Primer Ch3习题代码

3.16 编写一段程序,把练习3.13中vector对象的容量和具体内容输出出来。检验你之前的回答是否正确,如果不对,重新学习3.3.1节直到弄明白错在何处。

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

using std::vector;
using namespace std;

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

    cout << "v1: " << "size = " << v1.size() << ", ";
    for(auto c : v1)
        cout << c << " ";
    cout << endl;

    cout << "v2: " << "size = " << v2.size() << ", ";
    for(auto c : v2)
        cout << c << " ";
    cout << endl;

    cout << "v3: " << "size = " << v3.size() << ", ";
    for(auto c : v3)
        cout << c << " ";
    cout << endl;

    cout << "v4: " << "size = " << v4.size() << ", ";
    for(auto c : v4)
        cout << c << " ";
    cout << endl;

    cout << "v5: " << "size = " << v5.size() << ", ";
    for(auto c : v5)
        cout << c << " ";
    cout << endl;

    cout << "v6: " << "size = " << v6.size() << ", ";
    for(auto c : v6)
        cout << c << " ";
    cout << endl;

    cout << "v7: " << "size = " << v7.size() << ", ";
    for(auto c : v7)
        cout << c << " ";
    cout << endl;

    return 0;
}

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

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

using std::vector;
using namespace std;

int main()
{
    vector<string> vec;
    string word;
    int i = 0;
    int n = 0;

    cout << "请输入要输入词的个数:";
    cin >> n;

    while(cin >> word)
    {
        vec.push_back(word);
        if(++i == n)
            break;
    }

    for(auto &c : vec)
        for(auto &d : c)
            d = toupper(d);

    for(auto c : vec)
        cout << c << endl;

    return 0;
}

3.19 如果想定义一个含有10个元素的vector对象,所有元素的值是42,请列举出3种不同的实现方法。哪种方法更好呢?为什么?

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

using std::vector;
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 = {42, 42, 42, 42, 42, 42, 42, 42, 42, 42};
    vector<int> vec4;
    for(int i = 0; i < 10; ++i)
        vec4.push_back(42);
    vector<int> vec5(10);
    for(auto &c : vec5)
        c = 42;

    return 0;
}

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

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

using std::vector;
using namespace std;

// 相邻元素的和
int main()
{
    vector<int> vec;
    int n;

    while(cin >> n)
        vec.push_back(n);

    if(vec.size() == 0)
    {
        cout << "没有任何元素" << endl;
        return -1;
    }

    for(decltype(vec.size()) i = 0; i < vec.size() - 1; i+=2)
        cout << vec[i] + vec[i+1] << " ";

    if(vec.size() % 2)
        cout << vec[vec.size() - 1];
        
    cout << endl;

    return 0;
}

// 第1个和倒数第1个的和
#include <iostream>
#include <vector>
#include <string>

using std::vector;
using namespace std;

int main()
{
    vector<int> vec;
    int n;

    while(cin >> n)
        vec.push_back(n);

    if(vec.size() == 0)
    {
        cout << "没有任何元素" << endl;
        return -1;
    }

    for(decltype(vec.size()) i = 0; i < vec.size() / 2; ++i)
        cout << vec[i] + vec[vec.size() - 1 - i] << " ";
    if(vec.size() % 2)
        cout << vec[vec.size() / 2];

    cout << endl;

    return 0;
}

3.21 请使用迭代器重做3.3.3节的第一个练习

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

using std::vector;
using namespace std;

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

    cout << "v1: " << "size = " << v1.size() << ", ";
    for(auto it = v1.cbegin(); it != v1.cend(); ++it)
        cout << *it << " ";
    cout << endl;

    cout << "v2: " << "size = " << v2.size() << ", ";
    for(auto it = v2.cbegin(); it != v2.cend(); ++it)
        cout << *it << " ";
    cout << endl;

    cout << "v3: " << "size = " << v3.size() << ", ";
    for(auto it = v3.cbegin(); it != v3.cend(); ++it)
        cout << *it << " ";
    cout << endl;

    cout << "v4: " << "size = " << v4.size() << ", ";
    for(auto it = v4.cbegin(); it != v4.cend(); ++it)
        cout << *it << " ";
    cout << endl;

    cout << "v5: " << "size = " << v5.size() << ", ";
    for(auto it = v5.cbegin(); it != v5.cend(); ++it)
        cout << *it << " ";
    cout << endl;

    cout << "v6: " << "size = " << v6.size() << ", ";
    for(auto it = v6.cbegin(); it != v6.cend(); ++it)
        cout << *it << " ";
    cout << endl;

    cout << "v7: " << "size = " << v7.size() << ", ";
    for(auto it = v7.cbegin(); it != v7.cend(); ++it)
        cout << *it << " ";
    cout << endl;
    
    return 0;
}

3.22修改之前那个输出text第一段的程序,首先把text的第一段全都改成大写形式,然后再输出它。

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

using std::vector;
using namespace std;

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

    while(getline(cin, s))
        text.push_back(s);
        text.push_back(s);

    for(auto it = text.begin(); it != text.end() && !it->empty(); ++it)
    {
        for(auto it2 = it->begin(); it2 != it->end(); ++it2)
            *it2 = toupper(*it2);
        cout << *it << endl;
    }

    return 0;
}

3.23编写一段程序,创建一个含有10个整数的vector对象,然后使用迭代器将所有元素的值都变成原来的两倍。输出vector对象的内容,检验程序是否正确。

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

    for(auto it = vec.begin(); it != vec.end(); ++it)
        *it = 2 * (*it);

    for(auto c : vec)
        cout << c << " ";

    cout << endl;

    return 0;
}

3.24请使用迭代器重做3.3.3节的最后一个练习。

// 相邻元素相加
#include <iostream>
#include <vector>
#include <string>

using std::vector;
using namespace std;

int main()
{
    vector<int> vec;
    int n;
    int num;

    cout << "输入要输入的整数个数:";
    cin >> n;

    while(n-- > 0 && cin >> num)
        vec.push_back(num);

    for(auto it = vec.cbegin(); it != vec.cend() - 1; it += 2)
        cout << *it + *(it+1) << " ";

    if(vec.size() % 2)
        cout << vec[vec.size() - 1] << endl;

    return 0;
}


// 头尾对应元素相加
#include <iostream>
#include <vector>
#include <string>

using std::vector;
using namespace std;

int main()
{
    vector<int> vec;
    int n;
    int num;

    cout << "输入要输入的整数个数:";
    cin >> n;

    while(n-- > 0 && cin >> num)
        vec.push_back(num);

    auto begin = vec.begin();
    auto end = --vec.end();
    for( ; begin < end; begin++, end--)
        cout << *begin + *end << " ";

    if(vec.size() % 2)
        cout << *begin << endl;

    return 0;
}

3.25 3.3.3节划分分数段的程序是使用下标运算符实现的,请利用迭代器改写该程序并实现完全相同的功能。

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

using std::vector;
using namespace std;

int main()
{
    vector<unsigned> vUS(11);
    auto it = vUS.begin();
    int iVal;
    
    cout << "请输入一组成绩(0~100):" << endl;
    while(cin >> iVal)
        if(iVal < 101)
            ++*(it + iVal / 10);
    
    cout << "各分数段的人数分布是(成绩从低到高):" << endl;
    // 利用迭代器遍历vUS的元素并逐个输出
    for(it = vUS.begin(); it != vUS.end(); it++)
        cout << *it << " ";
    cout << endl;
    
    return 0;
}

3.41 编写一段程序,用整型数组初始化一个vector对象。

int main()
{
    int a[] = {91, 92, 93, 94, 95, 96};
    vector<int> a1(begin(a), end(a));

    for(auto c : a1)
        cout << c << " ";
    cout << endl;
    
    return 0;
}

3.42 编写一段程序,将含有整数元素的vector对象拷贝给一个整型数组。

int main()
{
    const int sz = 10;
    vector<int> vInt;

    srand((unsigned) time(nullptr));
    cout << "vector: " << endl;
    for(int i = 0; i != sz; ++i)
    {
        vInt.push_back(rand() % 100);   // 生成有一个100以内的随机数
        cout << vInt[i] << " ";
    }
    cout << endl;

    auto it = vInt.cbegin();
    int a[vInt.size()];
    cout << "array: " << endl;
    for(auto &val : a)
    {
        val = *it;
        cout << val << " ";
        it++;
    }
    cout << endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值