3.2 string - C++ primer 笔记

string

标准库类型string表示可变长的字符序列。

#include <string>
using std::string;

1 初始化

构造函数原型:

  • string(); //创建一个空的字符串 例如: string str;
    string(const char* s); //使用字符串s初始化
  • string(const string& str); //使用一个string对象初始化另一个string对象
  • string(int n, char c); //使用n个字符c初始化
string s1;			//默认构造,s1是空字符串
string s2(s1);		//s2是s1的副本
string s2 = s1;		//拷贝初始化,同s2(s1)
string s3("hi");	//s3是字面值“hi”的副本,除了字面值最后的那个空字符外
string s3 = "hi";	//拷贝初始化,等价s3("hi")
string s4(10, 'c')	//s4初始化为10个c组成的字符串

直接初始化和拷贝初始化

使用等号=则是拷贝初始化(copy initialization),反之不使用,则为直接初始化(direct initialization)。

2 string 对象操作

os<<s;			//将s写到输出流os中,返回os
is>>s;			//从is中读取字符串赋值给s,字符串串以空格分隔,返回is
getline(is, s);	//从is中读取一行赋值给s,返回is
s.empty();		//是否为空
s.size();		//尺寸
s[n];			//返回s中第n个字符的**引用**
s1+s2;			//返回s1 s2连接后的结果 
s1 = s2;		//用s2的副本代替s1中原来的值
s1 == s2;		//字符完全一样,则返回true;大小写敏感
s1 != s2;		//
<, <=, >, >=	//依照字符顺序
string str1 = "Hello";
string str2 = "Hello World";
string str3 = "Hiya";
//str1小于str2,str3大于str1、str2

2.1 赋值

  • 给string字符串进行赋值
    赋值的函数原型:

  • string& operator=(const char* s); //char*类型字符串 赋值给当前的字符串

  • string& operator=(const string &s); //把字符串s赋给当前的字符串

  • string& operator=(char c); //字符赋值给当前的字符串

  • string& assign(const char *s); //把字符串s赋给当前的字符串

  • string& assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串

  • string& assign(const string &s); //把字符串s赋给当前字符串

  • string& assign(int n, char c); //用n个字符c赋给当前字符串

2.2 拼接

实现在字符串末尾拼接字符串
函数原型:

  • string& operator+=(const char* str); //重载+=操作符
  • string& operator+=(const char c); //重载+=操作符
  • string& operator+=(const string& str); //重载+=操作符
  • string& append(const char *s); //把字符串s连接到当前字符串结尾
  • string& append(const char *s, int n); //把字符串s的前n个字符连接到当前字符串结尾
  • string& append(const string &s); //同operator+=(const string& str)
  • string& append(const string &s, int pos, int n);//字符串s中从pos开始的n个字符连接到字符串结尾

2.3 查找 替换

  • 查找:查找指定字符串是否存在

  • 替换:在指定的位置替换字符串
    函数原型:

  • int find(const string& str, int pos = 0) const; //查找str第一次出现位置,从pos开始查找

  • int find(const char* s, int pos = 0) const; //查找s第一次出现位置,从pos开始查找

  • int find(const char* s, int pos, int n) const; //从pos位置查找s的前n个字符第一次位置

  • int find(const char c, int pos = 0) const; //查找字符c第一次出现位置

  • int rfind(const string& str, int pos = npos) const; //查找str最后一次位置,从pos开始查找

  • int rfind(const char* s, int pos = npos) const; //查找s最后一次出现位置,从pos开始查找

  • int rfind(const char* s, int pos, int n) const; //从pos查找s的前n个字符最后一次位置

  • int rfind(const char c, int pos = 0) const; //查找字符c最后一次出现位置

  • string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str

  • string& replace(int pos, int n,const char* s); //替换从pos开始的n个字符为字符串s
    总结:

  • find查找是从左往后,rfind从右往左

  • find找到字符串后返回查找的第一个字符位置,找不到返回-1

  • replace在替换时,要指定从哪个位置起,多少个字符,替换成什么样的字符串

2.4 比较

字符串之间的比较

  • 字符串比较是按字符的ASCII码进行对比

= 返回 0

> 返回 1

< 返回 -1
函数原型:

  • int compare(const string &s) const; //与字符串s比较
  • int compare(const char *s) const; //与字符串s比较

2.5 字符存取

string中单个字符存取方式有两种

  • char& operator[](int n); //通过[]方式取字符
  • char& at(int n); //通过at方法获取字符

2.6 遍历元素

范围for语句

//统计字符串中的标点符号个数
string s("hello world!~@");
//count的类型与s.size()的类型一致
decltype(s.size()) count = 0;
for(auto c:s)
	if(ispunct(c))
		++count;
cout << count << endl;

下标

string s("sone thing);
for(auto index = 0; index != s.size() && !isspace(s[index]); ++index)
	s[index] = toupper(s[index])
	//输出 SOME thing

2.7 插入 删除

函数原型:

  • string& insert(int pos, const char* s); //插入字符串
  • string& insert(int pos, const string& str); //插入字符串
  • string& insert(int pos, int n, char c); //在指定位置插入n个字符c
  • string& erase(int pos, int n = npos); //删除从Pos开始的n个字符

2.8 string子串

从字符串中获取想要的子串

  • string substr(int pos = 0, int n = npos) const; //返回由pos开始的n个字符组成的字符串
void test01()
{
	string str = "abcdefg";
	string subStr = str.substr(1, 3);
	cout << "subStr = " << subStr << endl;

	string email = "hello@sina.com";
	int pos = email.find("@");
	string username = email.substr(0, pos);
	cout << "username: " << username << endl;
}

灵活的运用求子串功能,可以在实际开发中获取有效的信息

3 string对象中的字符

头文件cctype中定义的一组标准库函数:

char c;
isalnum(c);		//是字母或数字时为真
isalpha(c);		//是字母时为真
iscntrl(c);		//是控制字符时为真
isdigit(c);		//是数字时为真
isgraph(c);		//当c不是空格但可以打印时为真
islower(c);		//是小写字母时为真
isprint(c);		//是可打印字符时为真(即c是空格或具有可视形式)
ispunct(c);		//是标点符号时为真
isspace(c);		//是空白时为真(即c是空格、横向制表符、纵向制表符、回车符、换行符、进纸符中的一种)
isupper(c);		//是大写字母时为真
isxdigit(c);	//是十六进制数字时为真
tolower(c);		//
toupper(c);		//

习题

3.2 编写一段程序,一次读入一行,再改一次读入一个词

void q3_2(){
	string line;
	//一次读入一行
	while (getline(cin, line))
		cout << line << endl;
	//一次读入一个词
	while (cin>> line)
		cout << line << endl;
}

3.3
对于string类的输入运算符,自动忽略开头的空白(空格、制表符、换行等等),取值从第一个真字符开始直到下一个非空字符结束。

//一次读入一个词
while (cin>> line)
{
	cout << line <<","<<line.size()<< endl;
}

在这里插入图片描述
对于getline()函数,它会保存字符串中的空白符,它读入数据,直到遇到换行符位置。
3.4

void q3_4(){
	string s1;
	string s2;
	cin>>s1>>s2;
	if(s1 == s2)
		cout<<"deng"<<endl;
	else
		cout<< (s1>s2 ? s1 :s2) <<endl;
}
void q3_4() {
	string s1;
	string s2;
	cin >> s1 >> s2;
	cout << (s1.size() == s2.size() ? "等长" :(s1.size()>s2.size() ? s1 : s2)) << endl;
}

3.5

void q3_5(){
	string s1;
	string s2;
	while(cin>>s1)
		s2 += s1;
	cout<<s2;
}
void q3_5(){
	string s1;
	string s2;
	while(cin>>s1)
		s2 = s1 + " ";
	cout<<s2;
}

3.6

void q3_6(){
	string s("sssssddddfffffff");
	for (auto i = 0; i < s.size(); ++i)
		s[i] = 'X';
	cout << s;
}
void q3_6(){
	string s("sssssddddfffffff");
	for (auto &i:s)
		i = 'X';
	cout << s;
}

3.7 效果一样!(没看懂题目,看了下别人的代码-_-)

void q3_7(){
	string s("sssssddddfffffff");
	for (char &i:s)
		i = 'X';
	cout << s;
}

3.8

void q3_8(){
	string s("sssssddddfffffff");
	size_t i = 0;
	while (i < s.size()){
		s[i] = 'X';
		++i;
	}
	cout << s;
}

3.9 越界不合法
3.10

void q3_10()
{
	string s;
	getline(cin, s);
	for (auto c : s)
	{
		if (!ispunct(c))
			cout<<c;
	}
}

3.11
在vs2015中合法,c的类型是 const char &

const string s("sssssddddfffffff");
for (auto &c:s)
	cout<<c;
cout << s;

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值