C++学习笔记(1) C++ string总结

小结一下C++的string ~

1. string和int 、double、char数组的相互转换

在刷题的时候经常碰到string和其它类型相互转换的情况,这里做一点点小结。

(1)int 、double转string

C++11起, string转int、double可以使用std::to_string函数,该函数定义于头文件<string>中:

string to_string(int value);
string to_string(double value);

对于long、long long、unsigned、unsigned long、unsigned long long 、float、long doublestring也是一样可以使用to_string函数。

(2) C语言风格的字符串转C++ string

在声明C++ string 的时候可以用C语言风格的字符串进行初始化赋值string s(str)strC语言风格的字符串数组,例如:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	char str[] = "hello";  // char *str = "hello";
	string s(str);
	cout << "c format string:\t" << str << endl;
	cout << "cpp format string:\t" << s << endl;
	return 0;
} 

(3) string转int 、double

可以使用#include<string>头文件中stoistod两个函数。stoi函数把n进制 的字符串自某个位置起的部分转成10进制数。默认起始位置是0,字符串进制是10。stod函数把字符串转成double类型的浮点数。
即:

int stoi(字符串, 起始位置=0, 进制=10)
double stod(字符串, 起始位置=0)
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s = "666";
	cout << stoi(s) << endl;
	cout << stod(s) << endl;
	return 0;
} 

(4) string转C语言风格字符串

使用stringc_str()方法,注意声明字符数组的时候要加const

string s = "hello";
const char *str = s.c_str();

2. C++ string的基本用法

假如有一个string字符串s

(1) string的定义

string s;  // 定义
string s = "abc";  // 定义和初始化

(2) string的长度

s.size() 或者s.length()

(3) string的加法运算(operator+=)

string支持和另外的多个string进行加法操作。

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s = "hello";
	cout << "before plus:\t" << s << endl;
	s = s + " world!" + " Welcome to here!";
	cout << "after plus:\t" << s << endl;
	return 0;
} 

结果:

before plus:    hello
after plus:     hello world! Welcome to here!

但是使用+=操作时,+=右侧只能有一个string

(4) string的compare operator

两个string类型可以直接用==!=<<=>>=等进行比较,比较规则是字典序

(5) s.substr()的使用

s.substr(pos, length)返回字符串spos位置开始长度为length的子字符串。

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s = "hello";
	cout << s.substr(1, 3) << endl;
	return 0;
} 

输出结果如下:

ell

(6) s.find()的用法

s.find(s1)返回字符串或者字符s1s中第一次出现的位置,若梦没则返回s.npos
例子:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s = "hello hello";
	cout << s.find('o') << endl;
	cout << s.find("ll") << endl;
	cout << s.find('a');
	return 0;
} 

运行结果

4
2
18446744073709551615

(7) s.replace()用法

s.replace(pos, len, s1)  // 把s从pos位置开始,长度为len的子串替换为s1
s.replace(it1, it2, s2)  // 把s的迭代器[it1, it2)范围内的子串替换为s2

例如:

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s = "Maybe you will turn around.";
	string s1 = "will not";
	string s2 = "surely";
	cout << s.replace(10, 4, s1) << endl;
	cout << s.replace(s.begin(), s.begin()+5, s2)  << endl;
	return 0;
} 

运行结果:

Maybe you will not turn around.
surely you will not turn around.
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值