C++笔记4
string类简介
ISO/ANSI C++通过添加 string 类 扩展了C++库
因此现在可以使用string 类型的变量而不是字符数组来储存字符串了。
你将会看到,string类使用起来将会比数组简单,同时提供了字符串作为一种数据类型的表示方法。
要使用 string 类,必须在程序中包含头文件 string, string 类位于名称空间 std,因此还需要 用一条 using 编译指令,或者使用 std::string来使用他,string 类定义隐藏了字符串的数组性质,让你能够像使用普通变量一样来使用字符串。
4.7 strtypel .cpp // strtypel.cpp using the C++ string class #include #include estring> int main C) using namespace std; char charrl [201 ; char charr2 [201 = “jaguar” ; string strl; string str2 = “panther”; // make string class available // create an empty array // create an initialized array // create an empty string object // create an initialized string cout “Enter a kind Of feline: ; cin charrl; cout “Enter another kind Of fel ine: cin strl; // use cin for input cout Cout cout cout 0Here are some felines; charrl str1 endl ; charr2 str2 // use cout for Output “The third letter in charr2 charr2 [2] endl; “The third letter in str2 str2 [2] endl; return O ; // use array notation 在这个例子中我们可以发现,许多使用 string 类的方法和使用 字符数组其实是相同的。
不同在于,我们可以将 string 类 声明 为简单变量,而不是数组。
类设计让程序能够自动处理 srting 的大小
这让使用 string 类对象变得更加方便,也更加安全
C++11字符串初始化:
C++11也允许 列表初始化 用于C风格的字符串和 string 对象
= Chapon Dodu”); char first date ( “The Elegant plate”}; char second_date [l string third date (“The Bread Bowl”); ( “Hank’s Fine Eats”}; string fourth_date
赋值 拼接 附加:在使用 string 类的时候,某些操作比使用数组更加简单,例如,不能将一个数组赋值给另外一个数组,但是可以将一个 string对象赋值给另外 一个 string 对象
并且 string 类简化了字符串合并操作,可以使用 运算符来讲俩个 string 类对象合并起来
这使得 string 类看起来就像是 变量一般
// strtype2.cpp assigning,
include < iostream>
#include int main ( ) using namespace std; string sl = “penguin” ; string s2, 53; adding, and appending // make string class available cout cout cout cout cout cout cout cout sl; “You can assign one string Object to another: s2 sl\n” ; s2: “ s2 endli “You can assign a C-style string to a string object. \nn; \ “buzzard\ ; “buzzard” ; “32: “ 92 endl; “You can concatenate strings: 33 endl; “You can append strings. \n”; 33 sl s2\n”; cout 32 yields SI sl endl; “ for a day” ; cout N” for a day" yields s2 = B2 endl; return O ; strtype2 30 • CD main() E cpp “main” l/ / use string assign add and appending “pch. h” (string> std using namespace main ( ) sl s2 penguin” cout cout cout cout cout cout cout cout you can assign so “the sl is sl endl. is so endl your can assign C—STYLE str to string “sl is \ “ dog \ endl now the sl is sl endl “you can add sl and so” endl now s2 is s2 endl “your can appending the C—STYLE strings” std::ostream &(_cdecl * Pfn)(std::ostream &)) endl s2 4— cout the s2 is s2 endl return
String 类的其他操作
在 c++新增的 string 类之前,程序员也需要完成诸如给字符串赋值等工作,对于C风格的字符串,程序员使用C语言库中的函数来完成这些任务。头文件 cstring 提供了这些函数,例如:
可以使用 strcpy ()将字符串复制到字符数组中,使用 strcat() 来讲字符串附加到字符数组末尾。
4.9 strtype3.cpp // strtype3. cpp more string class features #include #include #include int main() using namespace std; char charrl [20] ; // make string class available // C-style string library char charr2 [201 = n jaguar” ; string strl; string str2 “panther”; // assignment for string Objects and character arrays Stri str2; strcpy(charrl, charr2) ; // copy str2 to Stri // Copy charr2 to charrl // appending for string objects and character arrays Stri “ paste”; Strcat (charrl, juice”) ; // add paste to end of strl // add juice to end Of charri // finding the length Of a string object and a C-style string int lenl strl.size() ; int len2 strlen (charrl) ; // obtain length of str1 // Obtain length of charrl cout “The len1 cout “The return O; string str1 contains “ characters . \nn; string charri contains characters , 这个程序我们可以看到 string 类的方便支持
首先是 string 类可以自动的调节自己的大小,并且在使用的时候,是当作一个对象,但是我们操作 字符数组是后, strlen(charr1)的时候是将 charr1当作了参数。
String 类 I/O:
正如你知道的,可以使用 cin和运算符 »来储存到 string对象中,使用 cout «来显示 string 对象。