demo_string_class_1.cpp

来自「对于一个初涉VC++的人来书」· C++ 代码 · 共 53 行

CPP
53
字号

//***************************************************
// 标准C++库中的字符串string类:
// 字符串构造函数、字符串操作成员函数、字符串运算符
//***************************************************

# include <iostream>
# include <string>

using namespace std ;

void trueFalse(int x)
{
	cout<<(x?"True":"False")<<endl;
}

void main()
{
	string S1="DEF", S2="123";
	char CP1[]="ABC"; 
	char CP2[]="DEF";

	cout<<"S1 is: "<<S1<<endl;
	cout<<"S2 is: "<<S2<<endl;
	cout<<"length of S2: "<<S2.length()<<endl;

	cout<<"CP1 is: "<<CP1<<endl;
	cout<<"CP2 is: "<<CP2<<endl;

	cout<<"S1<=CP1 returned: ";
	trueFalse(S1<=CP1); 

	cout<<"CP2<=S1 returned: ";
	trueFalse(CP2<=S1); 

	S2+=S1;
	cout<<"S2=S2+S1: "<<S2<<endl;
	cout<<"length of S2: "<<S2.length()<<endl;

	return;
}

/*
S1 is: DEF
S2 is: 123
length of S2: 3
CP1 is: ABC
CP2 is: DEF
S1<=CP1 returned: False
CP2<=S1 returned: True
S2=S2+S1: 123DEF
length of S2: 6
*/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?