C++ Primer Plus 代码学习解析(第四章 4.7-4.10)

4.7 strtype1.cpp

#include <iostream>
#include <string>               // make string class available
int main()
{
    using namespace std;
    char charr1[20];            // create an empty array
    char charr2[20] = "jaguar"; // create an initialized array
    string str1;                // create an empty string object
    string str2 = "panther";    // create an initialized string

    cout << "Enter a kind of feline: ";
    cin >> charr1;
    cout << "Enter another kind of feline: ";
    cin >> str1;                // use cin for input
    cout << "Here are some felines:\n";
    cout << charr1 << " " << charr2 << " "
        << str1 << " " << str2 // use cout for output
        << endl;
    cout << "The third letter in " << charr2 << " is "
        << charr2[2] << endl;
    cout << "The third letter in " << str2 << " is "
        << str2[2] << endl;    // use array notation
    // cin.get();

    return 0;
}
  1. string类型的变量是c++的一种全新的形式,可以用来储存字符串,比数组更简单
  2. 若使用string类,则需要包含头文件string
  3. string类与数组应用基本相同,主要区别是可以将string对象声明为简单变量,而非数组
  4. 类设计可以让程序自动处理string的大小,可自动调整其长度,因此使用string对象更方便
  5. 从理论上来讲,可以将char数组视为一组用于储存一个字符串的char储存单元,而string类变量是一个表示字符串的实体

4.8 strtype2.cpp

#include <iostream>
#include <string>               // make string class available
int main()
{
    using namespace std;
    string s1 = "penguin";
    string s2, s3;

    cout << "You can assign one string object to another: s2 = s1\n";
    s2 = s1;
    cout << "s1: " << s1 << ", s2: " << s2 << endl;
    cout << "You can assign a C-style string to a string object.\n";
    cout << "s2 = \"buzzard\"\n";
    s2 = "buzzard";
    cout << "s2: " << s2 << endl;
    cout << "You can concatenate strings: s3 = s1 + s2\n";
    s3 = s1 + s2;
    cout << "s3: " << s3 << endl;
    cout << "You can append strings.\n";
    s1 += s2;
    cout << "s1 += s2 yields s1 = " << s1 << endl;
    s2 += " for a day";
    cout << "s2 += \" for a day\" yields s2 = " << s2 << endl;

    //cin.get();
    return 0;
}
  1. 该代码探究了string类的一些应用
  2. 首先是相较于数组而言,一个string对象可以直接赋值给另一个string对象,以上就是s1赋值给s2
  3. 之后第二个就是s1与s2两个string类的相加,可以将s2内容加到s1的后面
  4. 既然可以相加,自然也可以用+=这一特性,而后就演示了这一特性

4.9 strtype3.cpp

#include <iostream>
#include <string>               // make string class available
#include <cstring>              // C-style string library
int main()
{
    using namespace std;
    char charr1[20];
    char charr2[20] = "jaguar";
    string str1;
    string str2 = "panther";

    str1 = str2;                // copy str2 to str1
    strcpy(charr1, charr2);     // copy charr2 to charr1

    // appending for string objects and character arrays
    str1 += " paste";           // add paste to end of str1
    strcat(charr1, " juice");   // add juice to end of charr1

    // finding the length of a string object and a C-style string
    int len1 = str1.size();     // obtain length of str1
    int len2 = strlen(charr1);  // obtain length of charr1

    cout << "The string " << str1 << " contains "
        << len1 << " characters.\n";
    cout << "The string " << charr1 << " contains "
        << len2 << " characters.\n";
    // cin.get();

    return 0;
}
  1. 该代码探究了数组与string类的一些特性
  2. 首先对于c风格的字符串,需要用到c语言库的函数,因此需要使用头文件string.h,即cstring
  3. 对两者加以对比,string类的赋值操作str1=str2就相当于strcpy(str1,str2)
  4. 之后是将一个字符串加到另一个后面,string类只需相加即可,而字符串则需要strcat()函数
  5. 最后则是对两个字符串计算长度,需要注意的是在这里,str1是一个string对象,size()是一string类的一个方法,这点与strlen()函数有本质的区别

4.10 strtype4.cpp

#include <iostream>
#include <string>               // make string class available
#include <cstring>              // C-style string library
int main()
{
    using namespace std;
    char charr[20];
    string str;

    cout << "Length of string in charr before input: "
        << strlen(charr) << endl;
    cout << "Length of string in str before input: "
        << str.size() << endl;
    cout << "Enter a line of text:\n";
    cin.getline(charr, 20);     // indicate maximum length
    cout << "You entered: " << charr << endl;
    cout << "Enter another line of text:\n";
    getline(cin, str);          // cin now an argument; no length specifier
    cout << "You entered: " << str << endl;
    cout << "Length of string in charr after input: "
        << strlen(charr) << endl;
    cout << "Length of string in str after input: "
        << str.size() << endl;
    // cin.get();

    return 0;
}
  1. 该代码探究了string对象的输入储存与输出
  2. 首先是在输入之前计算了数组与string类两者的长度,首先对于数组类,其大小是不固定的(碰到\0结束),而string对象却是0
  3. 之后分别向数组和string类输入数据,需要注意有两点,首先是数组使用之前提到过得cin.getline()方法获取数据,而string则是直接使用getline(cin,str),将输入的数据传递到cin中,而且其大小为str,代表其是可以变化的
  4. 上面这种处理方法成为友元函数,之后会对其进行详解

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值