⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 习题3.1 .txt

📁 c++ primer 答案(第4版) 自己整理的 只有1-5章
💻 TXT
字号:
习题3.1 

用适当的using声明,而不用std::前缀,访问标准库中的名字,重新编写2.3节的程序,计算一给定数的给定次幂的结果。

【解答】

#include <iostream>

using std::cin;

using std::cout; 

int main()

{

    // 局部对象

    int base, exponent;

    long result=1;

    // 读入底数和指数

    cout << "Enter base and exponent:" << endl;

    cin >> base >> exponent;

    if (exponent < 0) {

        cout << "Exponent can't be smaller than 0" << endl;

        return -1;

    }

    if (exponent > 0) {

        // 计算底数的指数次方

        for (int cnt = 1; cnt <= exponent; ++cnt)

            result *= base;

    }

    cout << base 

         << " raised to the power of "

         << exponent << ": "

         << result << endl;

    return 0;

}

习题3.2 

什么是默认构造函数?

【解答】

默认构造函数(default constructor)就是在没有显式提供初始化式时调用的构造函数。它由不带参数的构造函数,或者为所有形参提供默认实参的构造函数定义。如果定义某个类的变量时没有提供初始化式,就会使用默认构造函数。

如果用户定义的类中没有显式定义任何构造函数,编译器就会自动为该类生成默认构造函数,称为合成的默认构造函数(synthesized default constructor)。

习题3.3 

列举出三种初始化string对象的方法。

【解答】

(1) 不带初始化式,使用默认构造函数初始化string对象。

(2) 使用一个已存在的string对象作为初始化式,将新创建的string对象初始化为已存在对象的副本。

(3) 使用字符串字面值作为初始化式,将新创建的string对象初始化为字符串字面值的副本。

习题3.4 

s和s2的值分别是什么?

string s;

int main() {

string s2;

}

【解答】

s和s2的值均为空字符串。

习题3.5 

编写程序实现从标准输入每次读入一行文本。然后改写程序,每次读入一个单词。

【解答】

//从标准输入每次读入一行文本

#include <iostream>

#include <string>

using namespace std;

int main()

{

    string line;

    // 一次读入一行,直至遇见文件结束符

    while (getline(cin, line))

        cout << line << endl;    // 输出相应行以进行验证

    return 0;

}

修改后程序如下:

//从标准输入每次读入一个单词

#include <iostream>

#include <string>

using namespace std;

int main()

{

    string word;

    // 一次读入一个单词,直至遇见文件结束符

    while (cin >> word)

        cout << word << endl; // 输出相应单词以进行验证

    return 0;

}

注意,一般而言,应该尽量避免使用using指示而使用using声明(参见17.2.4节),因为如果应用程序中使用了多个库,使用using指示引入这些库中定义的名字空间,容易导致名字冲突。但本书中的程序都只使用了标准库,没有使用其他库。使用using指示引入名字空间std中定义的所有名字不会发生名字冲突。因此为了使得代码更为简洁以节省篇幅,本书的许多代码中都使用了using指示using namespace std;来引入名字空间std。另外,本题中并未要求输出,加入输出是为了更清楚地表示读入的结果。本书后面部分有些地方与此类似处理,不再赘述。

习题3.6 

解释string类型的输入操作符和getline函数分别如何处理空白字符。

【解答】

string类型的输入操作符对空白字符的处理:读取并忽略有效字符(非空白字符)之前所有的空白字符,然后读取字符直至再次遇到空白字符,读取终止(该空白字符仍留在输入流中)。

getline函数对空白字符的处理:不忽略行开头的空白字符,读取字符直至遇到换行符,读取终止并丢弃换行符(换行符从输入流中去掉但并不存储在string对象中)。

习题3.7 

编一个程序读入两个string对象,测试它们是否相等。若不相等,则指出两个中哪个较大。接着,改写程序测试它们的长度是否相等,若不相等,则指出两个中哪个较长。

【解答】

测试两个string对象是否相等的程序:

#include <iostream>

#include <string>

using namespace std;

int main()

{

    string s1, s2;

    // 读入两个string对象

    cout << "Enter two strings:" << endl;

    cin >> s1 >> s2;

    // 测试两个string对象是否相等

    if (s1 == s2)

        cout << "They are equal." << endl;

    else if (s1 > s2)

        cout << "\"" << s1 << "\" is bigger than"

             << " \"" << s2 << "\"" << endl;

    else

        cout << "\"" << s2 << "\" is bigger than"

             << " \"" << s1 << "\"" << endl;

    return 0;

}

测试两个string对象的长度是否相等的程序:

#include <iostream>

#include <string>

using namespace std;

int main()

{

    string s1, s2;

    // 读入两个string对象

    cout << "Enter two strings:" << endl;

    cin >> s1 >> s2;

    // 比较两个string对象的长度

    string::size_type len1, len2;

    len1 = s1.size();

    len2 = s2.size();

    if (len1 == len2)

        cout << "They have same length." << endl;

    else if (len1 > len2)

        cout << "\"" << s1 << "\" is longer than"

             << " \"" << s2 << "\"" << endl;

    else

        cout << "\"" << s2 << "\" is longer than"

             << " \"" << s1 << "\"" << endl;

    return 0;

}

习题3.8 

编一个程序,从标准输入读取多个string对象,把它们连接起来存放到一个更大的string对象中,并输出连接后的string对象。接着,改写程序,将连接后相邻string对象以空格隔开。

【解答】

#include <iostream>

#include <string>

using namespace std;

int main()

{

    string result_str, str;

    // 读入多个string对象并进行连接

    cout << "Enter strings(Ctrl+Z to end):" << endl;

    while (cin>>str) 

        result_str = result_str + str;

    // 输出连接后的string对象

    cout << "String equal to the concatenation of these strings is:" 

         << endl << result_str << endl;

    return 0;

}

改写后的程序:

#include <iostream>

#include <string>

using namespace std;

int main()

{

    string result_str, str;

    // 读入多个string对象并进行连接

    cout << "Enter strings(Ctrl+Z to end):" << endl;

    cin >> result_str;//读入第一个string对象,放到结果对象中

    while (cin>>str) 

        result_str = result_str + ' ' + str;

    // 输出连接后的string对象

    cout << "String equal to the concatenation of these strings is:" 

         << endl << result_str << endl;

    return 0;

}

习题3.9 

下列程序实现什么功能?实现合法吗?如果不合法,说明理由。

string s;

cout << s[0] << endl;

【解答】

该程序段输出string对象s所对应字符串的第一个字符。

实现不合法。因为s是一个空字符串,其长度为0,因此s[0]是无效的。

注意,在一些编译器(如Microsoft Visual C++ .NET 2003)的实现中,该程序段并不出现编译错误。

习题3.10 

编一个程序,从string 对象中去掉标点符号。要求输入到程序的字符串必须含有标点符号,输出结果则是去掉标点符号后的string对象。

【解答】

#include <iostream>

#include <string>

#include <cctype>

using namespace std;

int main()

{

    string s, result_str;

    bool has_punct = false;//用于标记字符串中有无标点

    char ch;

    //输入字符串

    cout << "Enter a string:" << endl;

    getline(cin, s);

    //处理字符串:去掉其中的标点

    for (string::size_type index = 0; index != s.size(); ++index)

    {

        ch = s[index];

        if (ispunct(ch))

            has_punct = true;

        else

            result_str += ch;

    }

    if (has_punct)

        cout << "Result:" << endl << result_str <<endl;

    else {

        cout << "No punctuation character in the string?!" << endl;

        return -1;

    }

    return 0;

}

习题3.11 

下面哪些vector定义不正确?

(a) vector< vector<int> > ivec;

(b) vector<string> svec = ivec ;

(c) vector<string> svec(10,"null");

【解答】

(b)不正确。因为svec定义为保存string对象的vector对象,而ivec是保存vector <int>对象的vector对象(即ivec是vector的vector),二者的元素类型不同,所以不能用ivec来初始化svec。

习题3.12 

下列每个vector对象中元素个数是多少?各元素的值是什么?

(a) vector<int> ivec1;

(b) vector<int> ivec2(10);

(c) vector<int> ivec3(10,42);

(d) vector<string> svec1;

(e) vector<string> svec2(10);

(f) vector<string> svec3(10,"hello");

【解答】

(a) 元素个数为0。

(b) 元素个数为10,各元素的值均为0。

(c) 元素个数为10,各元素的值均为42。 

(d) 元素个数为0。 

(e) 元素个数为10,各元素的值均为空字符串。 

(f) 元素个数为10,各元素的值均为"hello"。

习题3.13 

读一组整数到vector对象,计算并输出每对相邻元素的和。如果读入元素个数为奇数,则提示用户最后一个元素没有求和,并输出其值。然后修改程序:头尾元素两两配对(第一个和最后一个,第二个和倒数第二个,以此类推),计算每对元素的和,并输出。

【解答】

//读一组整数到vector对象,计算并输出每对相邻元素的和

#include <iostream>

#include <vector>

using namespace std;

int main()

{

    vector<int> ivec;

    int ival;

    // 读入数据到vector对象

    cout << "Enter numbers(Ctrl+Z to end):" << endl;

    while (cin>>ival)

        ivec.push_back(ival);

    // 计算相邻元素的和并输出

    if (ivec.size() == 0) {

        cout << "No element?!" << endl;

        return -1;

    }

    cout << "Sum of each pair of adjacent elements in the vector:" 

         << endl;

    for (vector<int>::size_type ix = 0; ix < ivec.size()-1; 

                              ix = ix + 2) {

        cout << ivec[ix] + ivec[ix+1] << "\t";

        if ( (ix+1) % 6 == 0) // 每行输出6个和

            cout << endl;

    }

    if (ivec.size() % 2 != 0) // 提示最后一个元素没有求和

        cout << endl 

             << "The last element is not been summed " 

             << "and its value is " 

             << ivec[ivec.size()-1] << endl;

    return 0;

}

修改后的程序:

//读一组整数到vector对象,计算首尾配对元素的和并输出

#include <iostream>

#include <vector>

using namespace std;

int main()

{

    vector<int> ivec;

    int ival;

    //读入数据到vector对象

    cout << "Enter numbers:" << endl;

    while (cin>>ival)

        ivec.push_back(ival);

    //计算首尾配对元素的和并输出

    if (ivec.size() == 0) {

        cout << "No element?!" << endl;

        return -1;

    }

    cout << "Sum of each pair of counterpart elements in the vector:" 

         << endl;

    vector<int>::size_type cnt = 0;

    for (vector<int>::size_type first = 0, last = ivec.size() - 1;

         first < last; ++first, --last) {

        cout << ivec[first] + ivec[last] << "\t";

        ++cnt;

        if ( cnt % 6 == 0) //每行输出6个和

            cout << endl;

    }

    if (first == last) //提示居中元素没有求和

        cout << endl 

             << "The center element is not been summed " 

             << "and its value is " 

             << ivec[first] << endl;

    return 0;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -