五.C++字符串std::string(从C到C++)

内容参考于《21天学通C++》(第八版)

五.C++字符串std::string

1. 示例
#include <iostream>
#include <string>

using namespace std;
int main()
{
	// 创建有初始值的字符串
	string greetString("Hello std::string!");
	cout << greetString << endl;

	// 创建空字符串,并获取值
	cout << "Enter a line of text: " << endl;
	string firstLine;
	getline(cin, firstLine);

	cout << "Enter another: " << endl;
	string secondLine;
	getline(cin, secondLine);

	// 字符串拼接
	cout << "Result of concatenation: " << endl;
	string concatString = firstLine + " " + secondLine;
	cout << concatString << endl;

	// 字符串复杂
	cout << "Copy of concatenated string: " << endl;
	string aCopy;
	aCopy = concatString;
	cout << aCopy << endl;

	cout << "Length of concat string: " << concatString.length() << endl;

	return 0;
}

运行结果

Hello std::string!
Enter a line of text:
6666666666666666666666666666666666
Enter another:
5555555555555555555555555555555555
Result of concatenation:
6666666666666666666666666666666666 5555555555555555555555555555555555
Copy of concatenated string:
6666666666666666666666666666666666 5555555555555555555555555555555555
Length of concat string: 69
2. 作用

这里完全不用考虑字符串的大小,不用考虑内存溢出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值