[C++]cpp小笔记3 --- C++ String and char

字符串实际上就是在内存中在连续位置上保存的字符,字符串结尾以'\0' null字符表示结束。

String在C++中有两种表现形式。1. 用C style; 2. 用String class.

1. C Style String

Store String in the form of an array of chars. The last bit of the char array MUST BE '\0' to indicate the end of the string.
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:




除了显式的指定'\0'的位置,还可以用"" 双引号来给char array初始化:
char bird[11] = "Mr. Cheeps";  // the \0 is understood
char fish[] = "Bubbles";   // let the compiler count

Quoted strings always include the terminating null character implicitly。要注意的一点是,正因为 " " 双引号会自动添加 \0, 所以"s"这样的一个字符实际上包含两个字符, 而's'近包含一个字符。
char shirt_size = 'S';   // this is fine
char shirt_size = ”S“;   // this is wrong

cin只能读取一个word, 任何空白符--space, tab, newline将结束cin读取,cin自动在读取的内容后添加'\0'并将内容保存在character array中。

#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.

2. C++ string class style string and its initialization

#include <string>

using namespace std;

string str1;
string str2 = "string";
string str3 = {"also a string"};
string str4 {"string too!"};

3. C-style and string style functions.

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

In C-style string, 
char char1[] = {'1','2','3'};
char char2[3];
char2 = char1;    //wrong, char copy need to use strcpy().

string ctyle functions:
string str = "xyz";
str.size();                          //size of str
#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(dessert2, name);  //add name to the end of dessert2

	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 string representation. PS, tired with R but got error...

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'; 

cin.get(), cin.get(char) and cin>>ch

cin>>ch 会自动过滤whitespaces, new lines, and tabs, 所以一遇到这些字符就结束输入;

cin.get() & cin.get(char)不会过滤,会全部返回;

cin.get()读到EOF return false;
cin.get(char)读到EOFreturn EOF;

文章标签: C++
个人分类: C++
上一篇[C++]cpp小笔记3 --- C++基础类型的大小和转换
下一篇[C++] gcc/g++ 命令的常用选项
想对作者说点什么? 我来说一句