C ++ Primer (第五版) 第三章习题练习

学习笔记所用
本文部分内容参考了:C++Primer第五版——习题答案+详解(完整版)

3.1

//注意,位于头文件的代码一般来说不适用using声明

3.2

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

int main()
{
    string s1;
    getline(cin, s1);//读入一整行
    cout << s1 << endl;
    cin >> s1;//读入一个词
    cout << s1;
    return 1;
}

3.3
cin:会忽略开头的空白( 空格符,换行符,制表符 ),直到遇见下一处空白为止
getline:从输入流中读取所有字符,直到遇到换行符为止
总结:如果想输入包括空格符的字符串就使用getline,否则用cin

3.4

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

int main()
{
    string s1, s2;
    cin >> s1 >> s2;
    //输出较大的
    if (s1 != s2) {
        if (s2 > s1) {
            cout << s2 << endl;
        }
        else cout << s1 << endl;
    }
    //输出较长的
    if (s1.size() != s2.size()) {
        if (s2.size() > s1.size()) {
            cout << s2 << endl;
        }
        else cout << s1 << endl;
    }
    return 1;
}

3.5

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

int main()
{
    string s1, sum1,sum2;//sum1保存连接成的大字符串,sum2用空格隔开
    while (cin >> s1) {
        sum1 += s1;
        sum2 = sum2 + ' ' + s1;
    }
    cout << sum1 << endl << sum2;
    return 1;
}

3.6

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

int main()
{
    string s = "dadad";
    for (auto &x : s) {
        x = 'X';
    }
    cout << s;
    return 1;
}

3.7

//结果一样

3.8

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

int main()
{
    string s = "dadad";
    decltype(s.size()) i;
    for (i = 0; i < s.size(); i++) {
        s[i] = 'X';
    }
    cout << s << endl;
    i = 0;
    s = "dadad";
    while (i < s.size()) {
        s[i++] = 'X';
    }    
    cout << s;
    return 1;
}

3.9

不合法,可能会产生不可预知的后果,因为s是空字符串。

3.10

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

int main()
{
    string s = "da,d,!a.d";
    string::size_type i,j;
    string s1;
    int L = 0;
    for (i = 0,j = 0; i < s.size(); i++) {
        if (!ispunct(s[i])) {
            s1 += s[i];
        }
    }
    s = s1;
    cout << s;
    return 1;
}

3.11

合法,类型是const char

3.12

合法,对象是vector<int>
不合法,类型不对应
合法,初始化10"null"

3.13

a:0个
b:100
c:1042
d:110
e:1042
f:10个默认初始化的元素
g:10"hi"

3.14

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

int main()
{
    vector<int> s;
    int x;
    while(cin >> x) {
        s.push_back(x);
    }
    cout << s.size();
    return 1;
}

3.15

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

int main()
{
    vector<string> s;
    string x;
    for (; cin >> x;) {
        s.push_back(x);
    }
    cout << s.size();
    return 1;
}

3.17

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

int main()
{
    vector<string> s;
    string x;
    while (cin >> x) {
        s.push_back(x);
    }
    for (auto i : s) {
        for (auto &j : i) {
            j = toupper(j);
        }
        cout << i << endl;
    }
    return 1;
}

3.18

//不合法,应改为:
vector<int> ivec(10);
ivec[0] = 42;

3.19

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

int main()
{
    //1
    vector<int> s1(10, 42);
    //2
    vector<int> s2;
    for (auto i = 0; i < 10; i++) {
        s2.push_back(42);
    }
    //3
    vector<int> s3 = { 42,42, 42, 42,42, 42, 42, 42, 42, 42 };
    return 1;
}

3.20

#include <iostream>
#include <string>
#include <cctype>
#include <vector>
using namespace std;
//1 2 3 4 5 6 7 8 9
int main()
{
    //1
    vector<int> s1;
    int x;
    while (cin >> x) {
        s1.push_back(x);
    }
    //3.20.1 输出相邻整数的和
    for (int i = 0; i < s1.size()-1; i++)
    {
        cout << s1[i] + s1[i + 1] << " " << endl;
    }
    //3.20.2 输出头尾之和
    for (int i = 0, j = s1.size()-1; i <= j ; i++, j--)
    {
        cout << s1[i] + s1[j] << " " << endl;
    }
    return 1;
}

3.22

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

int main()
{
    vector<string> text;
    string x;
    //初始化text
    while (getline(cin, x)) {
        text.push_back(x);
    }
    for (auto i = text.begin(); i != text.end() && !i->empty(); i++) {
        for (auto& j : *i) {
            j = toupper(j);
        }
        cout << *i << endl;
    }
    return 1;
}

3.23

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

int main()
{
    vector<int> text;
    int x;
    while (cin >> x) {
        text.push_back(x);
    }
    for (auto i = text.begin(); i != text.end(); i++) {
        *i *= 2;
        cout << *i << endl;
    }    
    return 1;
}

3.24

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

int main()
{
    vector<int> text = {1,2,3,4,5,6,7,8,9,10};
    //1
    for (auto i = text.begin(); i != text.end()-1; i++) {
        cout << *i + *(i + 1) << endl;
    }
    //2
    for (auto j = text.begin(), k = text.end() - 1; j <= k; j++, k--) {
        cout << *j + *k << endl;
    }
    return 1;
}

3.25

#include <iostream>
#include <string>
#include <cctype>
#include <vector>
using namespace std;
//42 65 95 100 39 67 95 76 88 76 83 92 76 93

int main()
{
    vector<int> text;
    int x;
    vector<int> cj(11, 0);
    while (cin >> x) {
        text.push_back(x);
    }
    for (auto i = text.begin(); i != text.end(); i++) {
        int j = *i / 10;
        cj[j] ++;
    }
    for (auto i = cj.begin(); i != cj.end(); i++) {
        cout << *i << endl;
    }
    return 1;
}

3.26

bef和end都是迭代器,它们没有+运算,不能两个迭代器相加,编译器会报错

3.28

string不是内置的数据类型,int是

sa数组为空。

ia数组含有10个整数,皆为0。


sa2数组为空。

ia2数组含有10个整数,在函数体内,值不确定。

3.29

1:长度固定了。
2:没有一些有效的封装函数。

3.30

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

int main()
{
    int a[10];
    for (auto i = 0; i < 10; i++) {
        a[i] = i;
    }
    return 1;
}

3.32

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

int main()
{
	//int版本
    int a[10];
    for (auto i = 0; i < 10; i++) {
        a[i] = i;
    }
    int b[10];
    int j = 0;
    for (auto i : a) {
        b[j++] = i;
    }
    //vector版本
    vector<int> a;
    for (auto i = 0; i < 10; i++) {
        a.push_back(i);
    }
    vector<int>b = a;
    for (auto i : b) {
        cout << i << endl;
    }
    return 1;
}

3.34

式子等于:p1=p1+(p2-p1)
都合法,但是注意当p1和p2都是指针时,合法是因为p2-p1是个difference_type,时带符号整形数,指针可以加,
但是指针是不能加指针的。

3.35

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

int main()
{
    int a[5] = { 1,2,3,4,5 };
	for (auto i = 0; i < 5; i++)
	{
		*(a + i) = 0;
		cout << a[i] << endl;
	}
    return 1;
}

3.36
总计:数组比较要一个一个比,并且要大小相等。vector对象可以直接通过"=="来比较两个对象。

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

int main()
{
    //比较数组
    int a[5] = { 1,2,3,4,5 };
    int b[5] = { 1,2,3,4,5 };
    bool jg = true;
    //sa和sb存长度
    int sa = sizeof(a) / sizeof(a[0]), sb = sizeof(b) / sizeof(b[0]);
    if (sa != sb) {
        jg = false;
    }
    else {
        for (auto i = 0; i < sa; i++) {
            if (a[i] != b[i]) {
                jg = false; break;
            }
        }
    }

    //比较vector对象
    vector<int>c(10, 0);
    vector<int>d(10, 1);
    if (c == d) {
        cout << "2";
    }
    else {
        cout << "3";
    }
    return 1;
}

3.37

会一直输出,知道碰见第一个'\0';

3.38

两个指针相加相当于所指对象的地址相加,所以没有意义。

3.39

string对象比较直接通过比较运算符就可以。
C风格的字符串比较需要用到strcmp()函数。

3.40

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

int main()
{
    char s1[] = "hello", s2[] = "world";
    char s3[11];//注意时两个字符串长度之和加 1 并不是加 2
    strcpy_s(s3, s1);
    strcat_s(s3, s2);
    cout << s3;
    return 1;
}

3.41

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

int main()
{
    int a[7] = { 1,2,3,4,56,7 };
    vector<int> b( a,a + 3 );
    return 1;
}

3.42

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

int main()
{
    int a[7];
    vector<int> b{ 1,2,3,4,6,7 };
    for (auto i = 0; i < b.size(); i++) {
        a[i] = b[i];
        cout << a[i] << endl;
    }
    return 1;
}

3.43

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

int main()
{
    int ia[3][4] = { {0,1,2,3},{4,5,6,7},{8,9,10,11} };
    //1
    for (int(& i)[4] : ia) {
        for (int& j : i) {
            cout << j << " ";
        }
        cout << endl;
    }
    //2
    for (int i = 0; i != end(ia)-begin(ia); i++) {
        for (int j = 0; j != end(ia[i])-begin(ia[i]); j++) {
            cout << ia[i][j] << " ";
        }
        cout << endl;
    }
    //3
    for (int(*i)[4] = ia; i != ia + 3; i++) {
        for (int* j = *i; j != *i + 4; j++) {
            cout << *j << " ";
        }
        cout << endl;
    }
    return 1;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值