c++标准库类型string使用全解(参考书C++primer第五版)

初始化string对象

#include <string>
using namespace::std;
int main()
{
	string s1;//默认初始化,s1是一个空字符
	string s2 = s1;//s2是s1是副本
	string s2(s1);//等价于上句
	string s3 = "hiya";//s3是该字符串字面值的副本//拷贝初始化
	string s3("hiya");//等价于上句//直接初始化
	string s4(10, 'c');//s4的内容是cccccccccc//直接初始化

}

string对象上的操作

#include <string>
#include <iostream>
using namespace::std;
int main()
{
	//os<<s 将s写到输出流os中,返回os
	//is>>s 从is中读取字符串赋给s,字符串以空白分割,返回is
	string s;
	cin >> s;
	cout << s << endl;
	//同样可以多个输入和输出
	string s1, s2;
	cin >> s1 >> s2;
	cout << s1 << s2 << endl;
	//读取未知数量的string对象
	string word;
	while (cin >> word)//反复读取,直到到达文件末尾
		cout << word << endl;
	//使用getline读取一整行,希望可以保留输入时的空白符,读取到换行符时结束把除换行符外的内容存入string对象
	//getline(is, s) 从is中读取一行赋给s,返回is
	string line;
	while (getline(cin, line))
	//每次读入一行,输出其中超过80个字符的行
		if(line.size() > 80)
			cout << line << endl;
	//s.empty() s为空返回true,否则返回false
	//s.size() 返回s中字符的个数,返回值类型是string::size_type可以存放任何string对象的大小,是无符号整型数
	//当然,可以使用auto或decltype来推断
	

	
	
	//比较string对象
	//如果两个string对象在某些对应位置上不一致,则string对象比较的结果实际是第一对相异字符的比较结果
	string slang1 = "hello";
	string slang2 = "hiya";
	if (slang1 < slang2);//true
	//如果两个string对象的长度不同而且较短的string对象的每个字符都与较长的string对象对应位置上的字符相同,就说较短的string对象小于较长的string对象
	string str1 = "hey";
	string str2 = "heyyyy";
	if (str1 < str2);//true

	//为string对象赋值
	//s1 = s2 用s2的副本代替s1中原来的字符
	string st1(10, 'c'), st2;
	st1 = st2;//此时st1和st2都是空字符串

	//两个string对象相加
	//s1 + s2 返回s1和s2连接后的结果
	string s1 = "hello, ", s2 = "world.\n";
	string s3 = s1 + s2;//hello, world.\n
	s1 += s2;//hello, world.\n
	//字面值和string对象相加
	//注意不能将两个字面值相加,必须要有一个string对象
	string s4 = "hello " + "," + s2;//错误,等价于string temp = "hello " + ",";  string s4 = s2 + temp
	//为了与C兼容,string对象和字符串字面量不是同种类型


	//s[n] 返回s中第n个字符的引用,位置n从0计起
	

	return 0;
}

练习

#include <string>
#include <iostream>
using namespace::std;
//练习3.2
//从标准输入中每次读入一行
void a() {
	string str;
	while(1)
		getline(cin, str);
}
//修改上面的程序使其每次读入一个词
void b() {
	string str;
	while (1)
		cin >> str;
}

//练习3.4
//读入两个字符串,比较是否相等,如果不相等,输出较大的字符串
void c() {
	string str1, str2;
	if (str1 == str2)
		cout << " str1 equal to str2" << endl;
	else {
		cout << "no equal" << endl;
		if (str1 > str2)
			cout << str1 << endl;
		else
			cout << str2 << endl;
	}
}
//改写为判断两字符串是否等长
void d() {
	string str1, str2;
	if (str1.size() == str2.size())
		cout << "str1和str2等长" << endl;
	else {
		if (str1.size() > str2.size())
			cout << str1 << endl;
		else
			cout << str2 << endl;
	}
}

//练习3.5
//读入多个字符串并将它们连接成一个大字符串并输出
void e() {
	string word, phrase;
	while (cin >> word) {
		phrase += word;
	}
	cout << phrase << endl;
}
//修改程序,用空格把输入的多个字符串分隔开
void f() {
	string word, phrase;
	while (cin >> word) {
		cout << word << " " << endl;
	}
}

处理string对象中的字符

#include <iostream>
#include <string>
#include <cctype>
#include <Windows.h>
using namespace::std;
int main()
{
	//cctype头文件中的函数
	//isalnum(c) 当c是字母或数字时为真
	//isalpha(c) 当c是字母时为真
	//iscntrl(c) 当c是控制符时为真
	//isdigit(c) 当c是数字时为真
	//isgraph(c) 当c不是空格且可以打印时为真
	//islower(c) 当c是小写字母时为真
	//isupper(c) 当c是大写字母时为真
	//isprint(c) 当c是可打印字符时为真(即c是空格或具有可视形式)
	//ispunct(c) 当c是标点符号时为真
	//isspace(c) 当c是空白是为真(空格、横向制表符、纵向制表符、回车符、换行符、进纸符)
	//isxdigit(c)当c是十六进制数字时为真
	//tolower(c) 如果c是大写字母,返回对应的小写字母;否则原样返回c
	//toupper(c) 如果c是小写字母,返回对应的大写字母;否则原样返回c

	//使用基于范围的for语句处理每个字符 C++11
	//for (declaration : expression)	statement
	//expression是一个对象表示序列
	//declaration负责定义一个变量,用于访问序列中的基础元素
	//每次迭代,declaration部分的变量会被初始化为expression部分的下一个元素值


	string str("some string");
	//每行输出str中的一个字符
	for (auto c : str) {
		Sleep(200);
		cout << c << endl;
	}
	cout << endl;
	
	
	//统计string对象中的大写字母个数
	string s("Hello ECNU Software Engineering Institute");
	for (auto c : s) {
		Sleep(200);
		cout << c << endl;
	}
	auto upper_cnt = 0;
	for (auto c : s)
		if (isupper(c))
			upper_cnt++;
	cout << upper_cnt << endl;

	//使用范围for语句改变字符串中的字符,把 循环变量定义为引用类型
	//把字符串中所有字母转换为大写
	for (auto &c : s) 
		c = toupper(c);//c是一个引用,改变的是s里的字符
	cout << s << endl;

	//只处理一部分字符
	//下标运算符[]接受的参数是string::size_type类型的值,这个参数表示要访问字符的位置,返回值是该位置上字符的引用
	//s[s.size()-1]是最后一个字符

	//将这个字符串的首字母变为大写
	string s2("hi ecnu ");
	if (!s2.empty())
		s[0] = toupper(s[0]);

	//使用下标进行迭代,把ecnu改为大写
	for (decltype(s2.size()) index = 3;
		index != s2.size() && !isspace(s[index]); ++index)
		s[index] = toupper(s[index]);

	//使用下标执行随机访问
	//把0到15之间的十进制数转换成对应的十六进制形式
	const string hexdigits = "0123456789ABCDEF";
	cout << "输入十进制数:";
	string result;//用于保存十六进制的字符串
	string::size_type n;//用于保存从输入流中读取的数
	while (cin >> n)
		if (n < hexdigits.size())//确保在十进制数0-15之间
			result += hexdigits[n] + " ";
	cout << "转换成十六进制数为:" << result << endl;


}

练习

#include<iostream>
#include<string>
#include<cctype>
using namespace::std;
int main()
{
	//练习3.6使用范围for语句将字符串内的所有字符用X代替
	string s1("Tomorrow is 520");
	for (auto &c : s1)
		c = 'X';
	cout << s1 << endl;

	//练习3.7将3.6的控制变量的类型设为char会发生什么
	for (char &c : s1)
		c = 'O';
	cout << s1 << endl;
	//正常,auto &c就是char &c

	auto i = 0;
	//练习3.8分别用while循环和for循环重写3.6
	for (i = 0; i < s1.size(); i++)
		s1[i] = 'X';
	i = 0;
	while (i < s1.size())
	{
		s1[i] = 'X';
		i++;
	}

	//3.9下面的程序有什么作用,合不合法?
	string s;
	if(!s.empty())//补充后合法
		cout << s[0] << endl;
	//作用是输出string对象s的第一个字符,如果该string对象为空,那么不合法

	//3.10读入一个包含标点符号的字符串,将标点符号去除后输出字符串剩余部分
	string ss1, ss2;
	getline(cin, ss1);
	for (auto c : ss1)
	{
		if (!ispunct(c))
			ss2 += c;
	}
	cout << ss2 << endl;

	//3.11下面的范围for语句合法吗,如果合法,c的类型是什么
	const string sss = "Keep out!";
	for (auto &c : s) {
		//合法,字符常量的引用类型,不通过c改变sss则合法
	}
	return 0;
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值