字符串实际上就是在内存中在连续位置上保存的字符,字符串结尾以'\0' null字符表示结束。
String在C++中有两种表现形式。1. 用C style; 2. 用String class.
char dog[8] = { 'b', 'e', 'a', 'u', 'x', ' ', 'I', 'I'}; // not a string!, continuous stored but not ended with '\0' char cat[8] = {'f', 'a', 't', 'e', 's', 's', 'a', '\0'}; // a string!
char val[] = {"this is also a string"}; //a string! '\0' is added implicitly for double quotation""
cout <<sizeof(wchar_t) <<endl; cout<<sizeof(char32_t) <<endl; cout<<sizeof(char16_t) <<endl; wchar_t wchar[] = {L" a 4-Byte long string"}; char32_t char32[] = {"a 4-Byte long string"}; char16_t char16[] = {"a 2-Byte long string"};Result is:4
4
2
char bird[11] = "Mr. Cheeps"; // the \0 is understood char fish[] = "Bubbles"; // let the compiler count
char shirt_size = 'S'; // this is fine char shirt_size = ”S“; // this is wrong
#include <iostream> #include "Chapter4.h" using namespace std; int main() { using namespace std; const int ArSize = 20; char name[ArSize]; char dessert[ArSize]; cout << "Enter your name:\n"; cin >> name; cout << "Enter your favorite dessert:\n"; cin >> dessert; cout << "I have some delicious " << dessert; cout << " for you, " << name << ".\n"; return 0; }
Enter your name: Alistair Dreeb Enter your favorite dessert: I have some delicious Dreeb for you, Alistair.
#include <string> using namespace std; string str1; string str2 = "string";
string str3 = {"also a string"};
string str4 {"string too!"};
strcpy(dest, src); //copy src char arry to dest char array. strncpy(dest, src); //copy "n" src char arry to dest char array. strlen(chararray1); //calulate the length of chars in chararray1 strcat(chararray1, chararray2); //concate chararray2 to the end of chararray1
char char1[] = {'1','2','3'}; char char2[3]; char2 = char1; //wrong, char copy need to use strcpy().
#include <iostream> #include "Chapter4.h" #include <string> #include <cstring> //C-style string library using namespace std; int main() { using namespace std; const int ArSize = 20; char name[ArSize] = "bbai"; char dessert[ArSize]; string namestr = "bbai"; //strcpy name to dessert strcpy(dessert, name); cout <<" copy from " << name << " to "<<dessert<<endl; int n = 2; char dessert2[ArSize]; //strncpy n characters from name to dessert2 strncpy(dessert2, name, n); cout <<" copy first "<< n << "characters from " << name << " to "<<dessert2<<endl; //strcat strcat(dessert2, name); //add name to the end of dessert2 //strlen cout<< "length of name is " << strlen(name) << " length of dessert2 is " << strlen(dessert2) <<" and lenght of string is " << namestr.size()<<endl; return 0; }
copy from bbai to bbai copy first 2characters from bbai to bb length of name is 4 length of dessert2 is 6 and lenght of string is 4
raw strings use "( and )" as delimiters, and they use anR prefix to identify them as rawstrings:
cout << R"(Jim "King" Tutt uses "\n" instead of endl.)" << '\n';
This would display the following:
Jim "King" Tutt uses \n instead of endl.
The standard string literal equivalent would be this:
cout << "Jim \"King\" Tutt uses \" \\n\" instead of endl." << '\n';