C++学习(九)入门篇——String类

2018-06-17 22:10:22来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

可以用string类而不是字符数组来存储字符串,string更加简单

要是用string类,则要在程序中包含头文件string,且位于std名称空间中,string类隐藏了字符串的数组性质,可以像处理普通变量那样处理字符串

程序清单4.7  strtype1.cpp
//strtypel.cpp - - using the C++ string class
#include<iostream>
#include<string>
int main()
{
    using namespace std;
    char charr1[20];
    char charr2[20] = "jaguar";
    string str1;
    string str2 = "panther";

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

}

输出结果:

string对象声明为简单变量

char数组为一组用于存储字符串的char存储单元,string类为一个存储字符串的实体

除了数组初始化可用列表初始化,也适用于字符串初始化

char first_date[ ] ={"Le Chapon Dodu"};

string second_date={"Le Chapon Dodu"};

char third_date[ ] {"Le Chapon Dodu"};

string forth_date {"Le Chapon Dodu"};

 

不能将一个数组赋给另一个数组,但可以将一个string对象赋给另一个string对象

char charr1[20];
char charr2[20] = "jaguar";
string str1;
string str2 = "panther";
charr1=charr2;                      //不允许这么做
str1=str2;                             //可以

string类简化了字符串合并操作

还可以将两个string对象合并起来

string str3;
str3 = str1+str2;
str1+=str2;
程序清单4.8 strtype2.cpp
//strtype2.cpp - - assigning,adding,and appending
#include<iostream>
#include<string>
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 = s2 + s1\n";
    s3 = s2 + s1;
    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();
}

结果:

在C++新增string类前,还是要完成给字符串赋值,用头文件cstring中的函数来完成,用函数strcpy()将字符串复制到字符数组中,用strcat()将字符串附加到字符数组末尾

strcpy(charr1,charr2);    //copy charr2 to charr1
strcat(charr1,charr2);    //append contents of charr2 to charr1
程序清单4.9  strtype3.cpp
//strtype3.cpp - - more string class features
#include<iostream>
#include<string>
#include<cstring>
int main()
{
    using namespace std;
    char charr1[20];
    char charr2[20] = "jaguar";
    string str1;
    string str2 = "panther";

    str1 = str2;
    strcpy_s(charr1,charr2);

    str1 += " paste";
    strcat_s(charr1, " juice");

    int len1 = str1.size();
    int len2 = strlen(charr1);

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

输出:

函数strlen()是一个常规函数,返回该字符串包含的字符数。

str1是一个对象,而size()是一个类对象,所以这样调用str1.size()

 

string类I/O

//strtype4.cpp - - line input
#include<iostream>
#include<string>
#include<cstring>
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);
    cout << "You entered: " << charr << endl;
    cout << "Enter another line of text:\n";
    getline(cin, str);
    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();
}

输出:

数组charr字符串长度为31

说明未被初始化的数据,第一个空字符出现的位置是随机的,在数组末尾几个字节后才遇到空字符

getline(cin,str);

将cin作为参数,指明去哪里查找输入,也没有指出字符串的长度,因为string对象根据字符串的长度自动调整自己的大小

getline()是istream的类方法,但在引入string类很久以前,C++就有istream类,所以没有处理string对象的类方法

 

其他形式的字符串字面值

wchar_t title[ ] =L"Chief Astrogator";
char16_t name[ ] =u"Felonia Ripova";
char32_t car[ ] =U"Humber Super Snipe";

C++分别使用前缀L、u、U来表示

 

还有可以使用原始字符串类型,在原始字符串中,字符表示的就是自己

cout<<R"(Jim "King" Tutt uses  "\n" instead of endl.)"<<'\n';

在这里\n不代表换行符,而是常规字符斜杠和n

“(   )”作为定界符,用前缀R来标识原始字符串

输入原始字符串时,按回车键不会移到下一行,而会在原始字符串中添加回车字符

 

如果要在原始字符串中包含)”,因为编译器见到第一个)”会到此结束,所以要重新定义边界符号

可以在开头“和(之间添加任意其他字符(不包括左括号、右括号、空格、斜杠和控制字符),那么结尾)‘’中间也要添加一样的其他字符

cout<<R"+*("(Who wouldn't?)",she whispered.)+*"<<endl;

可将R和其他字符串前缀结合,如Ru或者UR,来标识wchar_t等字符串

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:路径搜索——网络寻路

下一篇:HDU 6034---Balala Power!(搜索+贪心)