栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

【C++ Primer】 第三章 练习题 题解

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【C++ Primer】 第三章 练习题 题解

部分比较简单 or 重复的题跳过

3.2


#include  
#include 
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main () {
    string s,line;
    getline(cin,line); //读一行  getline 直到 换行终止
    cout << line << endl;
    cin >> s ;
    cout << s << endl;//读一个单词   遇到空格截止
    return 0;
}
3.5

#include  
#include 
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main () {
    string s1,s2;
    while(cin >> s1) {
        s2 += s1;
        s2 += " ";
        //按CTRl + Z 退出
    }
    cout << s2 << endl;
    return 0;
}
3.6


#include  
#include 
using std::cin;
using std::cout;
using std::endl;
using std::string;

int main () {
    string s;
    getline(cin, s);
    for(auto &it : s) {
  //  for(char &it : s) { 咱俩句话等价  3.7 
        it = 'x';
    }
    cout << s << endl;
    return 0;
}

3.9

3.10

使用ispunct(s) 判断是否为标点

3.11


类型是常量引用 无法被修改 会保存

3.12

3.17

#include  
#include 
#include 
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int main () {
    string s;
    vectorvs;
    while(cin >> s) {
        string tmp;
        for(auto &it : s) {
            tmp += toupper(it);
        }
        vs.push_back(tmp);
    }
    for(auto it : vs) {
        cout << it << endl;
    }
    cout << endl;
    return 0;
}

3.18


3.26

3.28


#include  
#include 
#include 
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
string sa[10];
int ia[10];
int main () {
    string sa2[10];
    int ia2[10];
    for(auto it : sa) {
        cout << it << "  ";
    }cout << endl;
    for(auto it : sa2) {
        cout << it << "  ";
    }
    cout << endl;
    for(auto it : ia) {
        cout << it << "  ";
    }
    cout << endl;
    for(auto it : ia2) {
        cout << it << "  ";
    }cout << endl;
    return 0;
}

3.34

指向p2 如果类型不同则会报错

3.35


C++11 可以数组可以使用 begin 和 end函数

#include  
#include 
#include 
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::begin;
using std::end;

int main () {
    const int sz= 10;
    int a[sz];
    for(int i = 0; i < 10; i ++) {
        a[i] = i;
    }
    for(auto it : a) {
        cout << it << ' ';
    }
    cout << endl;
    int *p = begin(a);  //需要 using 
    while (p != end(a)) {
        *p = 0;
        p ++;
    }
    for(auto it : a) {
        cout << it << ' ';
    }
    return 0;
}
3.37


由于没有/0 所以会死循环
加上 /0 or 改为字符串 “hello”

3.41


int a[10]
vector v (begin(a),end(a));

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/982014.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号