一、字符串字面值
字符串字面值是一串常量字符,字符串字面值常量用雙引號括起來的零個或多個字符表示,為兼容C語言,C++中所有的字符串字面值都由編譯器自動在末尾添加一個空字符。
字符串沒有變量名字,自身表示自身
"Hello World!" //simple string literal
"" //empty string literal
"/nCC/toptions/tfile.[cC]/n" //string literal using newlines and tabs
字符字面值: 'A' //single quote:character literal
字符串字面值: "A" //double quote:character string literal.包含字母A和空字符的字符串字符串字面值的連接:
std::out << "a multi-line "+
"string literal"+
" using concatenation"
<< std::endl;
輸出:a multi-line string literal using concatenation多行字面值:
std::out << "a multi-line /n
string literal/n
using a backslash"
<< std::endl;
輸出:a multi-line string literalusing a backslash=================================================
1. string literal:字符串直接量:
cout<<"hello"<<endl;
代碼中通過包含"hello"字符串自身來將其輸出,并未包含該字符串的變量。2. 字符串直接量可以賦值給變量,但是與字符串直接量相關聯的內存空間位于只讀部分,因此它是常量字符數組。
char* ptr = "hello";
ptr[1] = 'a';//crash! attemps to write to read-only memory.
因此,當引用字符串直接量的時候使用指向const的字符數組:
const char* ptr = "hello";
ptr[1] = 'a';//bug! attempts to write to read-only memory.
3. 當將字符串直接量賦值給字符數組的初始值的時候。由于字符數組存放與棧中,不允許引用其他地方的內存,因此編譯器會將字符串直接量復制到站的數組內存中。因此,可以進行相應的修改。
char stackArray[] = "hello";
stackArray[1] = 'a';
二、C++風格字符串
C++風格字符串:使用C++風格字符串的時候,要將它當做是一個普通的類型,如int,這樣反而會避免將string作為一個類來理解所帶來的很多問題。1. 支持<cstring>中許多函數完成的同樣操作。
2. 字符串定義:
string myString = “hello”;
3. 操作符 = :復制字符串;比如:
string newone = original;
會將后者復制給前者,不會出現兩個變量同樣指向一個內存的情況。
4. 可以像int一樣使用 == 之類的操作符
5. 可以改變字符串中的某一個字符。 如
string myString = "hello"; mystring[0] = 'l';
這中操作是允許的。2.1 C風格字符串的使用
C++語言通過(const) char *類型的指針來操縱C風格字符串。
#include <cstring> // cstring是string.h頭文件中的C++版本,而string.h是C語言提供的標準庫
//操縱C風格字符串的標準庫函數(參數類型省略,都是char *類型):
strlen(s) // 返回s的長度,不包括字符串結束符null
strcmp(s1, s2) //當s1<s2時,返回值<0 ,當s1=s2時,返回值=0 ,當s1>s2時,返回值>0
strcat(s1, s2) // 將字符串s2連接到s1后,并返回s1
strcpy(s1, s2) // 將s2復制給s1,并返回s1
strncat(s1, s2, n) // 將s2的前n個字符連接到s1后面,并返回s1
strncpy(s1, s2, n) // 將s2的前n個字符復制給s1,并返回s1
if(cp1 < cp2) // compares address, not the values pointed to
const char *cp1 = "A string example";
const char *cp2 = "A different string";
int i=strcmp(cp1, cp2); // i is positive
i=strcmp(cp2, cp1); // i is negative
i=strcmp(cp1, cp1); // i is zero
2.3 永遠不要忘記字符串結束符null
char ca[]={'C', '+', '+'}; // not null-terminated
cout << strlen(ca) << endl; // disaster: ca isn't null-terminated
2.4 調用者必須確保目標字符串具有足夠的大小
// Dangerous:What happens if we miscalculate the size of largeStr?
char largeStr[16+18+2]; // will hold cp1 a space and cp2
strcpy(largeStr, cp1); // copies cp1 into largeStr
strcat(largeStr, " "); // adds a space at end of largeStr
strcat(largeStr, cp2); // concatenates cp2 to largeStr
// prints A string example A different string
cout << largeStr << endl;
2.5 使用strn函數處理C風格字符串
char largeStr[16+18+2] // to hold cp1 a space and cp2
strncpy(largeStr, cp1, 17); // size to copy includes the null
strncat(largeStr, " ", 2); // pedantic, but a good habit
strncat(largeStr, cp2, 19); // adds at most 18 characters, plus a null
2.6 盡可能使用標準庫類型string
string largeStr = cp1; // initialize largeStr as a copy of cp1
largeStr += " "; // add space at end of largeStr
largeStr += cp2; // concatenate cp2 onto end of largeStr
此時,標準庫負責處理所有的內在管理問題。三、C風格字符串
字符串字面值的類型實質是const char類型的數組。C++從C語言繼承下來的一種通用結構是C風格字符串,而字符串字面值就是該類型的實例。C風格字符串是以空字符null結束的字符數組:
char ca1[]={'C', '+', '+'}; // no null, not C-style string
char ca2[]={'C', '+', '+', '/0'}; // explicit null
char ca3[]="C++"; // null terminator added automatically
const char *cp="C++"; // null terminator added automatically
char *cp1=ca1; // points to first element of a array, but not C-style string
char *cp2=ca2; // points to first element of a null-terminated char array
ca1和cp1都不是C風格字符串:ca1是一個不帶結束符null的字符數組,而指針cp1指向ca1,因此,它指向的并不是以null結束的數組。字符串的連接:
1.c++中string可以替代c中的char數組且前者用起來更方便。連接兩個string對象只需用'+';c字符串是用char數組實現的。以下都稱c字符串為char數組
例如:
string s1="hello",s2="world";
string s3=s1+s2; //也可以s3=s1+"world"
cout<<s3<<endl;//結果為helloworld
當然還可以用+=連接。2.還可以這樣連接一個string對象和char數組。
例如:
string s1="hello";
char s2[]="world";
cout<<s1+s2<<endl;//輸出結果為helloworld
但不能這樣連接兩個char數組或字符字面值。例如:
string s1="hello";
string s2="world";
string s3=s1+"world";//正確,可以連接一個string對象和字符串字面值
string s4="hello"+"world";//錯誤,不能這樣連接連個字符串字面值
char s5[]="world";
string s6=s1+s5;//正確,可以連接一個string對象和char數組
char s7[]="hello";
stirng s8=s7+s5;//錯誤,不能這樣連接兩個char數組。
總而言之只能用+或+=連接兩個string對象或一個string對象和字符串字面值或一個string對象和char數組。連接一個string對象和字符串字面值或char數組或返回的都是string對象,所以可以連接一個string對象和字符串字面值(或char數組)后再連接一個字符串字面值(或char數組)。
例如:
string s;//初始化為空
char s1[]="hello";
char s2[]="world";
s=s+s1+s2;//正確