C++ stl中的string的相关用法

string类的使用,首先要加上头文件

#include <string>

string的定义

string类有多个构造函数的重载,以下为常用的构造函数:

string();  //构造一个空字符串

string(const char* s);  //复制s所指的字符串

string(const char* s, size_t n);  //复制s所指字符串的前n个字符

string(size_t n, char c);  //生成有n个c字符的字符串

string(const string& str);  //复制str

string(const string& str, size_t pos, size_t len = npos);  //str中从字符下标pos开始的len个字符构成的子串

示例:

string s1;                     //空字符串
string s2("hello string");     //复制"hello string"
string s3("hello string", 3);  //复制"hello string"的前3个字符 为"hel"
string s4(10, 'h');            //生成10个'h'字符的字符串 为"hhhhhhhhhh"
string s5(s2);                 //s5为复制s2的结果
string s6(s2, 0, 5);           //复制s2中从字符位置0开始5个字符的部分 为"hello"

string的插入与拼接

1.push_back
用于末尾插入

void push_back (char c);

例子:

int main()
{
	string s;
	s.push_back('f');
	s.push_back('a');
	s.push_back('l');
	s.push_back('l');
	cout << s << endl; //fall
	return 0;
}

2.insert
能够指定位置插入字符或字符串

string& insert (size_t pos, const string& str);
string& insert (size_t pos, const char* s);
iterator insert (iterator p, char c);

例子:

int main()
{
	string s("fall"); //fall  初始化字符串s为fall

	//insert(pos, str)在pos位置插入字符串str
	s.insert(4, "zz"); //fallzz

	//insert(pos, string)在pos位置插入string对象
	string tmp("zzz");
	s.insert(6, tmp); //fallzzzzz

	//insert(pos, char)在pos位置插入字符char  s.end()获取末尾位置
	s.insert(s.end(), '!'); //fallzzzzz!
	
	cout << s << endl; //fallzzzzz!
	return 0;
}

3.append
能够完成string的拼接

string& append (const string& str);
string& append (const char* s);
string& append (size_t n, char c);

例子:

int main()
{
	string s1("fall");
	string s2(" zzzzz");

	//append(string)完成两个string对象的拼接
	s1.append(s2); //fall zzzzz

	//append(str)完成string对象和字符串str的拼接
	s1.append(" ZZ"); //fall zzzzz ZZ

	//append(n, char)将n个字符char拼接到string对象后面
	s1.append(3, '!'); //fall zzzzz ZZ!!!
	
	cout << s1 << endl; //fall zzzzz ZZ!!!
	return 0;
}

4.operator+=
使用+=能在字符串后追加字符串或者字符

string& operator+= (const string& str);
string& operator+= (const char* s);
string& operator+= (char c);

例子:

int main()
{
	string s1("fall");
	string s2(" zzzzz");

	// operator += str 在字符串s1后追加s2
	s1+=s2; //fall zzzzz

	//operator += c 在字符串s1后追加字符!
	s1+='!'; //fall zzzzz!
	
	cout << s1 << endl; //fall zzzzz!
	return 0;
}

string的删除

1.pop_back
能够进行尾删

void pop_back();

例子:

int main()
{
	string s("C++");
	s.pop_back();
	cout << s << endl; //C+
	return 0;
}

2.erase
能够对指定位置进行删除

string& erase (size_t pos = 0, size_t len = npos);
iterator erase (iterator p);
iterator erase (iterator first, iterator last);

例子:

int main()
{
	string s("I lovefall!");

	//erase(pos, n)删除pos位置开始的n个字符
	s.erase(6, 5); //I love 

	//erase(pos)删除pos位置的字符
	s.erase(s.end()); //I lov

	//erase(pos1, pos2)删除[pos1pos2)上所有字符 注意左闭右开
	s.erase(s.begin() + 1, s.end()); //I

	cout << s << endl; //I
	return 0;
}

string的查找

1.find
能够正向查找第一个匹配项

size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
size_t find (char c, size_t pos = 0) const;

例子:

int main()
{
	string s1("There are two needles in this haystack with needles");

	//find(string)正向搜索与string对象所匹配的第一个位置
	string s2("two");
	size_t pos1 = s1.find(s2);
	cout << pos1 << endl; //10

	//find(str)正向搜索与字符串str所匹配的第一个位置
	char str[] = "needles";
	size_t pos2 = s1.find(str);
	cout << pos2 << endl;  //14

	//find(char)正向搜索与字符char所匹配的第一个位置
	size_t pos3 = s1.find('l');
	cout << pos3 << endl; //18
	return 0;
}

注意: find()函数一般配合npos使用,当函数找到传入的参数元素时,返回相应下标;未找到时,返回npos

2.rfind
能够反向查找第一个匹配项

size_t rfind (const string& str, size_t pos = npos) const;
size_t rfind (const char* s, size_t pos = npos) const;
size_t rfind (char c, size_t pos = npos) const;

例子:

int main()
{
	string s1("There are two needles in this haystack with needles");

	//find(string)正向搜索与string对象所匹配的第一个位置
	string s2("needles");
	size_t pos1 = s1.find(s2);
	cout << pos1 << endl; //44

	//find(str)正向搜索与字符串str所匹配的第一个位置
	char str[] = "two";
	size_t pos2 = s1.find(str);
	cout << pos2 << endl;  //10

	//find(char)正向搜索与字符char所匹配的第一个位置
	size_t pos3 = s1.find('l');
	cout << pos3 << endl; //18
	return 0;
}

string的比较

compare
比较两个字符串的大小

int compare (const string& str) const;
int compare (size_t pos, size_t len, const string& str) const;
int compare (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const;

比较规则:
假设有字符串str1和str2

1、str1中第一个不相同的字符值较小,或者所有比较字符都相同,但str1较短,则返回小于0的值。
2、str1中第一个不相同的字符值较大,或者所有比较字符都相同,但str1较长,则返回大于0的值。
3、比较的两个字符串相等(包括长度),则返回0。

例子:

int main()
{
	string s1("hello world");
	string s2("hello fall");

	//"hello world"和"hello fall"比较
	cout << s1.compare(s2) << endl; //-1

	//"ello"和"hello fall"比较
	cout << s1.compare(1, 4, s2) << endl; //-1

	//"hello"和"hello"比较
	cout << s1.compare(0, 4, s2, 0, 4) << endl; //0

	return 0;
}

注意: string类可以对象与对象之间比较,也可以string类对象和字符串进行比较

string的替换

replace
能够对string进行替换

string& replace (size_t pos, size_t len, const char* s);
string& replace (size_t pos, size_t len, size_t n, char c);

例子:

int main()
{
	string s("hello world");

	//replace(pos, len, str)将pos位置开始的len个字符替换为字符串str
	s.replace(6, 4, "fall"); //hello falld

	//replace(pos, len, n, char)将pos位置开始的len个字符替换为n个字符char
	s.replace(10, 1, 3, '!'); //hello fall!!!

	cout << s << endl;
	return 0;
}

string的交换

swap
能够完成两个string的交换

void swap (string& x, string& y);
void swap (string& str);

例子:

int main()
{
	string s1("hello");
	string s2("fall");
	
	//使用string类的成员函数swap交换s1和s2
	s1.swap(s2);
	cout << s1 << endl; //fall
	cout << s2 << endl; //hello

	//使用非成员函数swap交换s1和s2
	swap(s1, s2);
	cout << s1 << endl; //hello
	cout << s2 << endl; //fall
	return 0;
}

string的大小和容量

1.size或者length
能够获取当前有效字符的个数,即长度

size_t size() const;
size_t length() const;

例子:

int main()
{
	string s("fall");
	cout << s.size() << endl; //4
	cout << s.length() << endl; //4
	return 0;
}

2.max_size
能够获取string对象最多可包含的字符数

size_t max_size() const;

例子:

int main()
{
	string s("fall");
	cout << s.max_size() << endl; //4294967294
	return 0;
}

3.capacity
能够获取当前对象所分配的存储空间的大小

size_t capacity() const;

例子:

int main()
{
	string s("fall");
	cout << s.capacity() << endl; //15
	return 0;
}

4.resize
能够改变当前对象的有效字符的个数

void resize (size_t n);
void resize (size_t n, char c);

resize使用规则

1、当n大于对象当前的size时(即有效字符个数),将size扩大到n,扩大的字符为c,若c未给出,则默认为’\0’。
2、当n小于对象当前的size时,将size缩小到n。

例子:

int main()
{
	string s1("fall");
	//resize(n)n大于对象当前的size时,将size扩大到n,扩大的字符默认为'\0'
	s1.resize(20);
	cout << s1 << endl; //fall
	cout << s1.size() << endl; //20
	cout << s1.capacity() << endl; //31

	string s2("fall");
	//resize(n, char)n大于对象当前的size时,将size扩大到n,扩大的字符为char
	s2.resize(20, 'x');
	cout << s2 << endl; //fallxxxxxxxxxxxxxxxx
	cout << s2.size() << endl; //20
	cout << s2.capacity() << endl; //31

	string s3("fall");
	//resize(n)n小于对象当前的size时,将size缩小到n
	s3.resize(2);
	cout << s3 << endl; //fa
	cout << s3.size() << endl; //2
	cout << s3.capacity() << endl; //15
	return 0;
}

注意:若给出的n大于对象当前的capacity,则capacity也会根据自己的增长规则进行扩大

5.reserve
能够改变当前对象的容量大小

void reserve (size_t n = 0);

reserve使用规则

1、当n大于对象当前的capacity时,将capacity扩大到n或大于n。
2、当n小于对象当前的capacity时,什么也不做。

例子:

int main()
{
	string s("fall");
	cout << s.size() << endl; //4
	cout << s.capacity() << endl; //15

	//reverse(n)当n大于对象当前的capacity时,将当前对象的capacity扩大为n或大于n
	s.reserve(20); 
	cout << s << endl; //fall
	cout << s.size() << endl; //4
	cout << s.capacity() << endl; //31

	//reverse(n)当n小于对象当前的capacity时,什么也不做
	s.reserve(2);
	cout << s << endl; //fall
	cout << s.size() << endl; //4
	cout << s.capacity() << endl; //31
	return 0;
}

注意:此函数对字符串的size没有影响,并且无法更改其内容。主要会对capacity有影响

6.clear
删除对象的内容,删除后对象变为空字符串

void clear();

例子:

int main()
{
	string s("fall");

	//clear()删除对象的内容,该对象将变为空字符串
	s.clear();
	cout << s << endl; //空字符串
	return 0;
}

7.empty
判断对象是否为空字符串

bool empty() const;

例子:

int main()
{
	string s("fall");
	cout << s.empty() << endl; // false 不为空

	//clear()删除对象的内容,该对象将变为空字符串
	s.clear();
	cout << s.empty() << endl; // true 为空
	return 0;
}

string对元素进行访问

1.[ ]+下标
由于string类中对[ ]运算符进行了重载,所以能够直接使用这种方式访问对象中的元素,为字符。
并且由于该重载是引用返回,所以我们可以通过该方式修改对应位置的元素

char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;

例子:

int main()
{
	string s("fall");
	//[]+下标访问对象元素
	for (int i = 0; i < s.size(); i++)
	{
		cout << s[i];
	}
	cout << endl; // fall

	//[]+下标修改对象元素内容
	for (int i = 0; i < s.size(); i++)
	{
		s[i] = 'x';
	}
	cout << s << endl; //xxxx
	return 0;
}

2.at
能够访问对象中的元素,并且也是使用引用返回,所以可以通过该函数修改对应位置的元素

char& at (size_t pos);
const char& at (size_t pos) const;

例子:

int main()
{
	string s("fall");
	for (int i = 0; i < s.size(); i++)
	{
		//at(pos)访问pos位置的元素
		cout << s.at(i);
	}
	cout << endl;

	for (int i = 0; i < s.size(); i++)
	{
		//at(pos)访问pos位置的元素,并对其进行修改
		s.at(i) = 'y';
	}
	cout << s << endl; //yyyy
	return 0;
}

3.范围for
如果需要通过范围for对对象的元素进行修改,那么用于接收元素的变量的类型需要为引用类型,否则用于接收元素的变量只是对象元素的拷贝,不会影响到对象中的元素

例子:

int main()
{
	string s("fall");
	//使用范围for访问对象元素
	for (auto n : s)
	{
		cout << n;
	}
	cout << endl; //fall

	//使用范围for访问对象元素,并对其进行修改
	for (auto& n : s) //需要修改对象的元素,n必须是引用类型
	{
		n = 'x';
	}
	cout << s << endl; //xxxx
	return 0;
}

4.使用迭代器访问对象中的元素
例子:

int main()
{
	string s("fall");
	
	//使用迭代器访问对象元素
	string::iterator it1 = s.begin(); // 获得起始位置
	while (it1 != s.end()) // 当迭代器的指向不为末尾位置时 就循环打印
	{
		cout << *it1;
		it1++;
	}
	cout << endl; //fall

	//使用迭代器访问对象元素,并对其进行修改
	string::iterator it2 = s.begin();
	while (it2 != s.end())
	{
		*it2 += 1;
		it2++;
	}
	cout << s << endl; // gbmm
	return 0;
}

string相关运算符

1.operator=
string类中对=运算符进行了重载,能够对string类进行赋值

例子:

int main()
{
	string s1;
	string s2("fall");

	//支持string类的赋值
	s1 = s2;
	cout << s1 << endl; // fall

	//支持字符串的赋值
	s1 = "haha";
	cout << s1 << endl;  // haha

	//支持字符的赋值
	s1 = 'x';
	cout << s1 << endl; // x
	return 0;
}

2.operator+=
string类中对+=运算符进行了重载,能够对字符串进行拼接
例子:

int main()
{
	string s1("fall");
	string s2(" zzzzz");

	// operator += str 在字符串s1后追加s2
	s1+=s2; //fall zzzzz

	//operator += c 在字符串s1后追加字符!
	s1+='!'; //fall zzzzz!
	
	cout << s1 << endl; //fall zzzzz!
	return 0;
}

3.operator+
string类中对+运算符进行了重载,能够支持string类对象,字符串,字符之间的相加

例子:

int main()
{
	string s;
	string s1("best");
	string s2("friend");
	char str[] = "you";
	char ch = '!';

	//string类 + string类
	s = s1 + s2;
	cout << s << endl; //bestfriend

	//string类 + 字符串
	s = s1 + str;
	cout << s << endl; //bestyou

	//字符串 + string类
	s = str + s1;
	cout << s << endl; //youbest

	//string类 + 字符
	s = s1 + ch;
	cout << s << endl; //best!
	
	//字符 + string类
	s = ch + s1;
	cout << s << endl; //!best
	return 0;
}

4.operator>> 和 operator<<
string类中对<<和>>运算符进行了重载,能够直接使用<<和>>对string类对象进行输入和输出

例子:

int main()
{
	string s; // 创建string类对象
	cin >> s; //输入
	cout << s << endl; //输出
	return 0;
}

5.relational operators
relational operator是关系比较运算符,包括了==、!=、<、>、<=、>=。能够支持string类对象和字符串之间关系的比较

例子:

int main()
{
	string s1("abc");
	string s2("abd");
	cout << (s1 > s2) << endl; // 0 返回false
	cout << (s1 < s2) << endl; // 1 返回true
	cout << (s1 == s2) << endl; // 0 返回false
	return 0;
}

注意:重载的关系比较运算符的比较是根据对应字符的ASCII码值进行比较的,如果匹配的字符一样,就看长度

string中与迭代器相关的函数

1.与正向迭代器相关
begin
返回一个指向字符串第一个字符的迭代器

iterator begin();
const_iterator begin() const;

end
返回一个指向字符串结束字符的迭代器,即指向末尾’\0’的位置

iterator end();
const_iterator end() const;

例子:

int main()
{
	string s("hello world");

	// 正向迭代器  用于遍历
	string::iterator it = s.begin();
	while (it != s.end())
	{
		cout << *it;
		it++;
	}
	cout << endl; //hello world

	return 0;
}

2.与反向迭代器相关
rbegin
返回指向字符串最后一个字符的反向迭代器

reverse_iterator rbegin();
const_reverse_iterator rbegin() const;

rend
返回指向字符串第一个字符的前面的理论元素的反向迭代器

reverse_iterator rend();
const_reverse_iterator rend() const;

例子:

int main()
{
	string s("hello world");

	//反向迭代器 反向遍历
	string::reverse_iterator rit = s.rbegin();
	while (rit != s.rend())
	{
		cout << *rit;
		rit++;
	}
	cout << endl; //dlrod olleh

	return 0;
}

string与字符串之间的转换

1.将字符串转换为string

int main()
{
	//方式一
	string s1("hello world");

	//方式二
	char str[] = "hello world";
	string s2(str);

	cout << s1 << endl; //hello world
	cout << s2 << endl; //hello world
	return 0;
}

2.将string转换为字符串
有两种方式,分别是使用c_str或者data

const char* c_str() const;
const char* data() const;

两者的区别:

在C++98中,c_str()返回 const char* 类型,返回的字符串以空字符结尾
在C++98中,data()返回 const char* 类型,返回的字符串 不以空字符结尾
在C++11版本中,c_str()与data()用法相同

例子:

int main()
{
	string s("hello world ");
	const char* str1 = s.data();
	const char* str2 = s.c_str();

	cout << str1 << endl;
	cout << str2 << endl;
	return 0;
}

string中字符串的提取

1.substr
能够提取string中的子字符串

string substr (size_t pos = 0, size_t len = npos) const;

例子:

int main()
{
	string s1("helloworld");
	string s2;

	//substr(pos, n)提取pos位置开始的n个字符序列作为返回值
	s2 = s1.substr(2, 3);
	cout << s2 << endl; //llo
	return 0;
}

2.copy
能够将string中的子字符串拷贝到字符数组中

size_t copy (char* s, size_t len, size_t pos = 0) const;

例子:

int main()
{
	string s("helloworld");
	char str[20];

	//copy(str, n, pos)复制pos位置开始的n个字符到str字符串
	size_t length = s.copy(str,2, 3);
	
	//copy函数不会在复制内容的末尾附加'\0',需要手动加
	str[length] = '\0';
	cout << str << endl; //llo
	return 0;
}

getline函数

当我们使用>>进行输入操作时,读取到空格就会停止,但是在一些场景下我们是不想停止的,getline能够解决这个问题
示例:

int main()
{
	string s;
	cin >> s; //输入:hello world
	cout << s << endl; //输出:hello
	return 0;
}

使用方法一:

istream& getline (istream& is, string& str);

getline函数将从is中提取到的字符存储到str中,直到读取到换行符’\n’为止

例子:

int main()
{
	string s;
	getline(cin, s); //输入:hello world
	cout << s << endl; //输出:hello world
	return 0;
}

使用方法二:

istream& getline (istream& is, string& str, char delim);

getline函数将从is中提取到的字符存储到str中,直到读取到分隔符delim(自定义)或换行符’\n’为止

例子:

int main()
{
	string s;
	getline(cin, s, 'r'); //输入:hello world
	cout << s << endl; //输出:hello wo
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值