[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:

4

4

2



除了显式的指定'\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.

C-style
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
	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;
}

Result:
 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;



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值