《C++ primer》第五版习题答案整理——第三章 字符串、向量组和数组

 

更新中

P75

练习3.1

3.1.1

#include

using std::cout;

using std::endl;

int main()

{

    int sum = 0, val = 50;

    while (val <= 100)

    {

         sum += val;

         ++val;

    }

    cout << "Sum of 50 to 100 inclusive is " << sum << endl;

    return 0;

}

3.1.2

#include

using std::cout;

using std::endl;

int main()

{

    int sum = 0;

    for (int val = 10; val >= 0; --val)

    {

         sum += val;

    }

    cout << "Sum of 10 to 0 inclusive is " << sum << endl;

    return 0;

}

3.1.3

#include

using std::cout;

using std::cin;

using std::endl;

int main()

{

    cout << "请输入两个整数: " << endl;

    int i = 0, j = 0;

    cin >> i >> j;

    if (i <= j)

    {

         for (i; i <= j; i++)

             cout << i << endl;

    }

    else

    {

         for (i; i >= j; i--)

             cout << i << stendl;

    }

    return 0;

}

3.1.4
 

#include 

#include 

#include "../ch02/ex2_42.h"


using std::cin;

using std::cout;

using std::endl;

using std::cerr;


int main()

{

    Sales_data data1, data2;


    // code to read into data1 and data2

    double price = 0;  // price per book, used to calculate total revenue


    // read the first transactions: ISBN, number of books sold, price per book

    cin >> data1.bookNo >> data1.units_sold >> price;

    // calculate total revenue from price and units_sold

    data1.revenue = data1.units_sold * price;


    // read the second transaction

    cin >> data2.bookNo >> data2.units_sold >> price;

    data2.revenue = data2.units_sold * price;


    // code to check whether data1 and data2 have the same ISBN

    //        and if so print the sum of data1 and data2

    if (data1.bookNo == data2.bookNo)

    {

         unsigned totalCnt = data1.units_sold + data2.units_sold;

         double totalRevenue = data1.revenue + data2.revenue;


         // print: ISBN, total sold, total revenue, average price per book

         cout << data1.bookNo << " " << totalCnt

             << " " << totalRevenue << " ";

         if (totalCnt != 0)

             cout << totalRevenue / totalCnt << endl;

         else

             cout << "(no sales)" << endl;


         return 0;  // indicate success

    }

    else

    {  // transactions weren't for the same ISBN

         cerr << "Data must refer to the same ISBN" << endl;

         return -1; // indicate failure

    }

}

不再一一列举;

知识点:using声明命名空间。注意的是头文件中不应该包含using声明,因为在引用头文件时,会将头文件中的内容拷贝到文件中来,如果头文件中包含了using声明,那么可能每一个头文件中都会包含using声明,可能会造成名字的冲突。

常加:using namespace  std ;(将标准库声明)

 

P81

练习3.2

一整行

#include 

#include

using namespace std;

int main()

{

    string line;

    while (getline(cin, line))

             cout << line << endl;

    return 0;

}

一个词
 

#include 

#include

using namespace std;

int main()

{

    string line;

    while(cin>>line)

    cout << line << endl;

    return 0;

}

练习3.3

对于string类的输入函数,它会自动忽略开头的空白(空格、制表符、换行等等),从第一个真正的字符开始直到下一个空白。

对于getline()函数,它会保存字符串中的空白符,它读入数据,直到遇到换行符位置。

 

练习3.4

字符串比较

#include 

#include

using namespace std;

int main()

{

    string i,j;

    cout << "请输入两个字符串" << endl;

    cin >> i >> j;

    if (i == j)

         cout << "两字符串相等" << endl;

    if (i != j)

    {

         if (i > j)

             cout << "两字符串不相等,较大的是: " << i << endl;

         else

             cout << "两字符串不相等,较大的是: " << j << endl;

    }

    return 0;

}

字符串长度比较

#include 

#include

using namespace std;

int main()

{

    string i,j;

    cout << "请输入两个字符串" << endl;

    cin >> i >> j;

    if (i.size() == j.size())

         cout << "两字符串长度相等" << endl;

    if (i.size()!= j.size())

    {

         if (i.size ()> j.size())

             cout << "两字符串长度不相等,较大的是: " << i << endl;

         else

             cout << "两字符串长度不相等,较大的是: " << j << endl;

    }

    return 0;

}

 

练习3.5

连在一起

#include 

#include

using namespace std;

int main()

{

    string i,j;

    cout<<"请输入多个字符串"<> j)

    {

         i += j;

    }

    cout << i << endl;

    return 0;

}

空格分隔开
 

#include 

#include

using namespace std;

int main()

{

    string i,j;

    cout<<"请输入多个字符串"<> j)

    {

         i += j+=" ";

    }

    cout << i << endl;

    return 0;

}

P86

练习3.6

#include 

#include

using namespace std;

int main()

{

    string str;

    cin >> str;

    for (auto &c : str)//切记用引用

    c='X';

    cout << str << endl;

    return 0;

}

练习3.7

不会发生变化,因为每一个元素都是char类型的。

#include 

#include

using namespace std;

int main()

{

    string str;

    cin >> str;

    for (char &c : str)//切记用引用

    c='X';

    cout << str << endl;

    return 0;

}

练习3.8

个人感觉用哪个都差不多(所以到底哪个好???)。

传统for循环

#include 

#include

using namespace std;

int main()

{

    string str;

    cin >> str;

    int num = str.size();

    for(int i=0;i

while循环
 

#include 

#include

using namespace std;

int main()

{

    string str;

    cin >> str;

    int num = str.size();

    int i=0;

    while (i< num)

    {

         str[i] = 'X';

         i++;

    }

    cout << str << endl;

    return 0;

}

练习3.9

不合法,因为字符串S是一个空字符串,第一个元素是未知的,引用非法。

 

练习3.10

#include 

#include

using namespace std;

int main()

{

    string str,putout;

    cout << "请输入一段带标点符号的字符串:" << endl;

    cin >> str;

    for (auto c : str)

         if (!ispunct(c))

            putout= putout + c;

    cout << putout << endl;

    return 0;

}

 

3.11

合法,c的类型为对const char的引用

 

P90

3.12

  1. 正确,包含0个元素;
  2. 错误,vector元素类型不一致;
  3. 正确,包含10个元素为“null”的vector对象。

 

3.13

  1. 空vector;
  2. 10个初始值为0的int对象;
  3. 10个初始值为42的int对象;
  4. 一个初始值为10的int对象;
  5. 2个int对象,初始值分别为10  42;
  6. 10个空字符串;
  7. 10个初始化为”hi”的字符串。

 

P91

3.14

#include 

#include

#include

using namespace std;

int main()

{

    int i;

    vectorv{ 0 };

    while (cin >> i)

         v.push_back(i);

    return 0;

}

 

3.15

#include 

#include

#include

using namespace std;

int main()

{

    string i;

    vectorv;

    while (cin >> i)

         v.push_back(i);

    return 0;

}

 

P94

练习3.16

#include 

#include

#include

using namespace std;

int main()

{

    vectorv1, v2(10), v3(10, 42), v4{ 10 }, v5{10,42};

    vectorv6{ 10 }, v7{ 10,"hi" };

    cout << v1.size()<

练习3.17

#include 

#include

#include

using namespace std;

int main()

{

    vector str;

    string s;

    cout << "请输入一组词:" << endl;

    while (cin >> s)

    {

         str.push_back(s);

    }

    for (auto &c1 : str)

    {

         for(auto &c2:c1)

         c2 = toupper(c2);

    }

    for (auto c : str)

    {

         cout << c << endl;

    }

    return 0;

}

 

练习3.18

不合法,空vector,给第一个元素赋值是错误的。

第二行改为:   

int str = 42;

ivec.push_back(str);

 

练习3.19   

    vectorv1(10,42);//此法最好,简洁!

    vectorv2{ 42,42,42,42,42,42,42,42,42,42 };

    vectorv3 = { 42,42,42,42,42,42,42,42,42,42 };

    vectorv4 = v1;

    vectorv5(v1);

练习3.20

1.相邻整数相加

#include 

#include

#include

using namespace std;

int main()

{

    int in;

    vectorin_v,sum1;

    cout << "请输入一组整数" << endl;

    while (cin >> in)

    {

         in_v.push_back(in);

    }

    for (int i = 0; i < in_v.size()-1; i++)

    {

         sum1.push_back(in_v[i] + in_v[i + 1]);

    }

    for (auto c : sum1)

    {

         cout << c << ' ';

    }

    cout << endl;

}

2.“头尾“整数相加

#include 

#include

#include

using namespace std;

int main()

{

    int in;

    vectorin_v,sum2;

    cout << "请输入一组整数" << endl;//分两种情况

    while (cin >> in)

    {

         in_v.push_back(in);

    }

    if (in_v.size() % 2 == 0)//判断整数个数为双数

    {

         for (int i = 0; i < in_v.size() / 2 + 1; i++)

         {

             sum2.push_back(in_v[i] + in_v[in_v.size() - i - 1]);

         }

         for (auto c : sum2)

         {

             cout << c << ' ';

         }

         cout << endl;

    }

    else//整数个数为单数

    {

         for (int i = 0; i < in_v.size() / 2 + 1; i++)

         {

             sum2.push_back(in_v[i] + in_v[in_v.size() - i - 1]);

         }

         sum2.push_back(in_v[in_v.size() / 2]);

         for (auto c : sum2)

         {

             cout << c << ' ';

         }

         cout << endl;

    }

}

 

P99

练习3.21

#include 

#include

#include

using namespace std;

int main()

{

    vectorv1, v2(10), v3(10, 42), v4{ 10 }, v5{ 10,42 };

    vectorv6{ 10 }, v7{ 10,"hi" };

    cout << v1.size() << endl;

    for (auto it=v1.begin();it!=v1.end();++it)

    {

         cout << *it<< ' ';

    }

    cout << endl;

    cout << v2.size() << endl;

    for (auto it = v2.begin(); it != v2.end(); ++it)

    {

         cout <<*it << ' ';

    }

    cout << endl;

    cout << v3.size() << endl;

    for (auto it = v3.begin(); it != v3.end(); ++it)

    {

         cout << *it << ' ';

    }

    cout << endl;

    cout << v4.size() << endl;

    for (auto it = v4.begin(); it != v4.end(); ++it)

    {

         cout << *it << ' ';

    }

    cout << endl;

    cout << v5.size() << endl;

    for (auto it = v5.begin(); it != v5.end(); ++it)

    {

         cout <<*it<< ' ';

    }

    cout << endl;

    cout << v6.size() << endl;

    for (auto it = v6.begin(); it != v6.end(); ++it)

    {

         cout <<*it << ' ';

    }

    cout << endl;

    cout << v7.size() << endl;

    for (auto it = v7.begin(); it != v7.end(); ++it)

    {

         cout <<*it<< ' ';

    }

    cout << endl;

    return 0;

}

 

练习3.22

#include 

#include

#include

using namespace std;

int main()

{

    vector text{ "some string"," ","1asafd" };

    for (auto it = text.begin(); it != text.end() && !it->empty(); ++it)

    {

         for (auto& c : *it)

             c = toupper(c);

         cout << *it << endl;

    }

    return 0;

}

练习3.23

#include 

#include

#include

using namespace std;

int main()

{

    vector num(10,6);

    for (auto it = num.begin(); it != num.end(); ++it)

    {

         *it *= 2;

         cout << *it << endl;

    }

    return 0;

}

 

P101

练习3.24

相邻相加

#include 

#include

#include

using namespace std;

int main()

{

    int in;

    vectorin_v, sum;

    cout << "请输入一组整数" << endl;

    while (cin >> in)

    {

         in_v.push_back(in);

    }

    for (auto it = in_v.begin(); it + 1 != in_v.end(); ++it)

    {

         sum.push_back(*it + *(it + 1));

    }

    for (auto c : sum)

    {

         cout << c << " ";

    }

    cout << endl;

    return 0;

}

头尾相加

#include 

#include

#include

using namespace std;

int main()

{

    int in;

    vectorin_v, sum;

    cout << "请输入一组整数" << endl;//分两种情况

    while (cin >> in)

    {

         in_v.push_back(in);

    }

    if (in_v.size() % 2 == 0)//判断整数个数为双数

    {

         for (auto it_beg=in_v.begin(),it_end=in_v.end()-1;it_beg!=it_end;++it_beg,--it_end)

         {

             sum.push_back(*it_beg+*it_end);

         }

         for (auto c : sum)

         {

             cout << c << ' ';

         }

         cout << endl;

    }

    else//整数个数为单数

    {

         for (auto it_beg = in_v.begin(), it_end = in_v.end() - 1; it_beg != it_end; ++it_beg, --it_end)

         {

             sum.push_back(*it_beg + *it_end);

         }

         sum.push_back(in_v[in_v.size() / 2]);

         for (auto c : sum)

         {

             cout << c << ' ';

         }

         cout << endl;

    }

}

 

练习3.25

#include

#include

#include

using namespace std;

int main()

{

    vectorscores(11, 0);

    int grade;

    while (cin >> grade)

    {

         auto it = scores.begin();

         int n = grade / 10;

         it = it + n;

         ++(*it);

    }

    for (auto c:scores)

    {

         cout <

练习3.26

因为两个迭代器之间没有相加的操作!

两迭代器相减的结果是他们之间的距离。

迭代器+n或-n分别为向前或向后移动若干元素。

 

P103

练习3.27

  1. 非法,维度必须为常量;
  2. 合法;
  3. 非法;
  4. 非法,没有空间存放空字符。

 

练习3.28

sa数组为空;ia数组含有10个整数,皆为0;sa2数组为空;ia2数组含有10个整数,在函数体内,值不确定

 

练习3.29

数组的缺点:数组的大小是确定不变的,不可以向数组中添加元素,丧失了vectoer的灵活性。

 

P104

练习3.30

数组大小为10,那么索引应该是0~9,数组越界ix < array_size

 

练习3.31

#include

using namespace std;

int main()

{

    int ia[10];

    for (int i = 0; i <= 9; i++)

         ia[i] = i;

    for (auto i : ia)

         cout << i << " ";

    cout << endl;

    return 0;

}

 

练习3.32

数组

#include

using namespace std;

int main()

{

    int ia[10];

    int b[10];

    for (int i = 0; i <= 9; i++)

    {

         ia[i] = i;

         b[i] = ia[i];

    }

    for (auto i : ia)

         cout << i << " " ;

    cout << endl;

    for (auto i : b)

         cout << i << " ";

    cout << endl;

    return 0;

}

vector

#include

#include

using namespace std;

int main()

{

    vectoriv1;

    for (int j = 0; j < 10; j++)

         iv1.push_back(j);

    for (auto c : iv1)

         cout << c << " ";

    cout << endl;

    vectoriv2(iv1);

    for (auto c : iv2)

         cout << c << " ";

    cout << endl;

    return 0;

}

练习3.33

数组的值为不确定的.

 

P108

练习3.34

将p1移动到p2位置,即将p1移动(p2-p1)个位置;p1或p2是非法的,该程序就是非法的。

 

练习3.35

#include

#include

using namespace std;

int main()

{

    int ia[] = { 0,1,2,3,4,5 };

    for (auto c : ia)

         cout << c << ' ';

    cout << endl;

    int* p = ia;

    for (int i = 0; i < 6; i++)

    {

         *p = 0;

         ++p;

    }

    for (auto c : ia)

    {

         cout << c << ' ';

    }

    cout << endl;

    return 0;

}

 

练习3.36

数组比较

#include

#include

#include

using namespace std;

int main()

{

    int a[10], b[10];

    int* numa = a, * numb = b;

    int temp=0;

    for (int i = 0; i < 10; i++)

         {

             a[i] = i;

             b[i] = 10-i;

         }

    if (sizeof(a) == sizeof(b))

    {

        

         for (int j = 0; j < 10; j++)

         {

             if (*(numa + j) == *(numb + j))

             {

                  temp ++;

             }

         }

         if (temp == 10)

         {

             cout << "相等" << endl;

         }

         else

             cout << "不相等" << endl;

    }

    else

         cout << "不相等" << endl;

    return 0;

}

vector比较

#include

#include

#include

using namespace std;

int main()

{

    int a[10], b[10];

    for (int i = 0; i < 10; i++)

         {

             a[i] = i;

             b[i] = 10-i;

         }

    vector v1(a, a + 10);//一种新的用数组初始化vector的方法,

    vector v2(b, b + 10);//   其元素与数组中元素相同。

    if (v1 == v2)

         cout << "相等" << endl;

    else

         cout << "不相等" << endl;

    return 0;

}

 

P110

练习3.37

输出如下

《C++ primer》第五版习题答案整理——第三章 字符串、向量组和数组_第1张图片

 

练习3.38

指针指代的是对象的地址,相加是地址相加,难以区分到底什么意义。

 

练习3.39

#include

#include

#include

using namespace std;

int main()

{

    string a = "abcd";

    string b = "efgh";

    if (a == b)

         cout << "equal" << endl;

    else

         cout << "not equal" << endl;

    char ca1[] = { 'a','b','c','\0' };

    char ca2[] = "abc";

    if (strcmp(ca1, ca2) == 0)

         cout << "equal" << endl;

    else

         cout << "not equal";

    return 0;

}

 

练习3.40

#include

#include

#include

#pragma warning(disable:4996)//VS2019编译错误,必须加上此行;

                             //原因是strcpy与strcat函数被新版本判定为不安全,准备弃用。

using namespace std;

int main()

{

    char ca1[] = "abc";

    char ca2[] = { 'd','e','f' ,'\0'};

    char ca3[100];

    strcpy(ca3, ca1);

    strcat(ca3, ca2);

    cout << ca3 << endl;

    return 0;

}

P112

练习3.41

#include

#include

#include

using namespace std;

int main()

{

    int a[] = { 1,2,3 };

    vector b(begin(a), end(a));

    for (auto c : b)

         cout << c << ' ';

    cout << endl;

    return 0;

}

 

练习3.42

#include

#include

#include

using namespace std;

int main()

{

    int a[10];

    vector v(10,1);

    for (int i=0;i<=9;i++)

    {

         a[i] = v[i];

         cout << a[i] << ' ';

    }

    cout << endl;

    return 0;

}

P116

练习3.43

#include

#include

#include

using namespace std;

int main()

{

    int ia[3][4] = {

         {0,1,2,3},

        {4,5,6,7},

        {8,9,10,11}

    };

    for (const int(&i)[4] : ia)

    {

         for (int j : i)

             cout << j << " ";

         cout << endl;

    }

    cout << endl;

    for (size_t i = 0; i < 3; i++)

    {

         for (size_t j = 0; j < 4; j++)

             cout << ia[i][j] << " ";

         cout << endl;

    }

    cout << endl;

    for (int(*i)[4] = begin(ia); i != end(ia); i++)

    {

         for (int* j = begin(*i); j != end(*i); j++)

             cout << *j << ' ';

         cout << endl;

    }

    return 0;

}

练习3.44
 

#include

#include

#include

using namespace std;

int main()

{

    using int_array=int[4];

    int ia[3][4] = {

         {0,1,2,3},

        {4,5,6,7},

        {8,9,10,11}

    };

    for (const int_array &i: ia)

    {

         for (int j : i)

             cout << j << " ";

         cout << endl;

    }

    cout << endl;

    for (size_t i = 0; i < 3; i++)

    {

         for (size_t j = 0; j < 4; j++)

             cout << ia[i][j] << " ";

         cout << endl;

    }

    cout << endl;

    for (int_array *i = begin(ia); i != end(ia); i++)

    {

         for (int* j = begin(*i); j != end(*i); j++)

             cout << *j << ' ';

         cout << endl;

    }

    return 0;

}

练习3.45
 

#include

#include

#include

using namespace std;

int main()

{

    int ia[3][4] = {

         {0,1,2,3},

        {4,5,6,7},

        {8,9,10,11}

    };

    for (auto &i: ia)

    {

         for (auto j : i)

             cout << j << " ";

         cout << endl;

    }

    cout << endl;

    for (auto i = 0; i < 3; i++)

    {

         for (auto j = 0; j < 4; j++)

             cout << ia[i][j] << " ";

         cout << endl;

    }

    cout << endl;

    for (auto *i = begin(ia); i != end(ia); i++)

    {

         for (auto * j = begin(*i); j != end(*i); j++)

             cout << *j << ' ';

         cout << endl;

    }

    return 0;

}

 

你可能感兴趣的:(C++)