C++ primer 5版第三章

本文提供了一系列C++编程练习题解答,包括字符串操作、向量容器使用、迭代器应用等核心内容,适合初学者实践和巩固所学知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

练习3.6


//用范围for语句将字符串内所有字符用X代替
#include <iostream>
using namespace std;
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;

int main() {
    string str("some thing");

    for (auto &c:str){
        c = 'X';//这里开始把 X写成"X"了..
    }
    cout<<str<<endl;
    return 0;
}

练习3.10

//读入一个包含标点符号的字符串,将标点符号去除后输出字符串剩余的部分

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

int main() {
    string s,s2;
    cin >>s;
    decltype(s.size()) cnt = 0;
    decltype(s.size()) n = 0;
    for( n = 0,cnt = 0;cnt!= s.size(); ++cnt){
        if (!ispunct(s[cnt]))
            cout<<s[cnt];//不是标点符号就输出
//这里,原本打算想办法在原字符串里面删掉标点,或者新建字符串保存非标点字符,但,未果,先不管,刷书》。
    }
}

练习3.17


//从cin读入一组词并把他们存入一个vector对象,然后设法把所有词都改写为大写形式。
//输出改编后的结果,每个词占一行。
#include <iostream>
using namespace std;
#include <string>
#include <vector>
using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::string;

int main() {
    vector<string> text;
    string str;
    while(cin >> str){
        text.push_back(str);
    }
    for (string s: text){
        for(auto &c: s)
            c = toupper(c);
        cout<<s<<endl;
    }
}

练习3.20

//读入一组整数并把他们存入一个vector对象,将每对相邻整数的和输出出来
#include <iostream>
using namespace std;
#include <string>
#include <vector>
using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::string;

int main() {
    vector<int> num;
    int n;
    while(cin >> n)
        num.push_back(n);
    if(num.size() > 1){
        for(decltype(num.size()) i = 0; i+1< num.size();i++)
            cout<<num[i]+num[i+1]<<" ";
    }
    cout<<endl;
}

//改:先输出第1个和最后1个的和,再第2和倒数第2,类推
#include <iostream>
using namespace std;
#include <string>
#include <vector>
using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::string;

int main() {
    vector<int> num;
    int n;
    while(cin >> n)
        num.push_back(n);
    if(num.size() > 1){
        decltype(num.size()) begin = 0, end = num.size()-1;
        for(; begin<=end;++begin, --end)
            cout<<num[begin]+num[end]<<" ";
    }
    cout<<endl;
}

练习3.22

//把text第一段全都改成大写形式,然后再输出它
#include <iostream>
using namespace std;
#include <string>
#include <vector>
using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::string;

int main() {
    vector<string> text{"hellow"," ", "world"};
//这里,试过string text,用getline,然后结果是没有empty成员,暂时不知道怎么回事
    for (auto it = text.begin();
        it != text.end() && !it->empty(); ++it){
        for(auto &c : *it)
            c = toupper(c);
        cout<<*it;
    }
}

练习3.23

//创建一个含有10个整数的vector对象,然后使用迭代器将所有元素的值都变成原来的两倍
#include <iostream>
using namespace std;
#include <string>
#include <vector>
using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::string;

int main() {
    vector<int> num;
//这里原来写的num(10),但经测试,会出现前10个初始化为0,输入的数字从第11个开始插,最后有10个0和10个数...所以
//vector最好是不限定大小(是这么个说法吗
    int temp;
    for (int i = 0; i< 10; i++){
        cin >> temp;
        num.push_back(temp);
    }
    for (auto it = num.begin();
        it != num.end(); ++it){
        *it = (*it) + (*it);
        cout<<*it<<" ";
        cout<< num.size()<<endl;
    }
}

练习3.25


//p93的划分分数段,用迭代器改写该程序
#include <iostream>
using namespace std;
#include <string>
#include <vector>
using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::string;

int main() {
    vector<unsigned> scores(11,0);
    unsigned grade;
    auto beg = scores.begin(), end = scores.end();
    while(cin >> grade){
        if (grade <= 100){
            grade = grade/10;// 成绩的十位数字
            while(grade != 0){//beg移动到scores的对应位置
                ++beg;
                --grade;
            }
            ++(*beg);//计数
            beg = scores.begin();//beg恢复原状
        }

    }
    while(beg != end){//逐个输出scores的值
        cout<<*beg<<" ";
        beg++;
    }
    //若添加cout<<*end<<endl,会输出12个值,最后一个固定是0,也就是说
    //end是最后一个元素的后一个位置
}

练习3.42


//将含有整数元素的vector对象拷贝给一个整型数组
#include <iostream>
using namespace std;
#include <string>
#include <vector>
using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::string;


int main() {
    vector<int> a{1,2,3,4,5};
    int b[5];
    auto beg = a.begin(), end = a.end();
    int i = 0;
    while (beg != end){
        b[i++] = *(beg++);
    }
    for (i = 0; i < 5; i++)
        cout<< b[i]<< " "<< endl;
}

练习3.43


//输出ia的元素
#include <iostream>
using namespace std;
#include <string>
#include <vector>
using std::vector;
using std::cin;
using std::cout;
using std::endl;
using std::string;

//版本1:范围for管理迭代过程
int main() {
    int ia[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
    for (int (&row)[4] : ia)//题目中要求不得使用auto,注意&row
        for (int &col : row)
            cout<< col <<" ";
    cout<< endl;
}

//版本2:下标运算符
int main() {
    int ia[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
    int row,col;
    for (row = 0; row < 3;++row)
        for (col = 0; col < 4; ++col)
            cout << ia[row][col]<< " ";
    cout<< endl;
}

//版本3:指针
int main() {
    int ia[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
    for (int (*p)[4] = ia; p != ia + 3; ++p)
        for( int *q = *p; q != *p + 4; ++q)
    //注意这个q,是指向一个整数,而p指向一个4元素的数组,*q = *p是q指向4元数组的首位
            cout<< *q <<" ";
    cout<< endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值