STL string容器用法

1 String概念
string是C++标准库的一个重要的部分,主要用于字符处理。C++的算法库对string类也有着很好的支持,并且string类还和C语言的字符串之间有着良好的接口。

2 String和char的比较
string是一个类,char
是一个指向字符的指针。string封装了char*,管理这个字符串是一个char*型的容器。string不用考虑内存释放和越界。string管理char所分配的内存。每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。string提供了一系列的字符串操作函数。

3 String的用法
3.1 string的构造函数
3.1.1 默认构造函数
string(); //构造一个空的字符串string s1。
3.1.2 拷贝构造函数
string(const string& str); //构造一个与str一样的string。
3.1.3 带参数的构造函数
string(const char* s); //用字符串s初始化
string(int n, char c); //用n个字符c初始化
3.2 String的存取字符操作

(1)char& operator[](int n);
(2)char& at(int n);

注:operator[]和at()在越界时会抛出异常,[]在刚好越界时会返回’\0’,再继续越界时程序直接中断。如果程序希望通过try,catch捕获异常,建议采用at()。
【例】

#include<iostream>
#include<string>
#include<errno.h>

using namespace std;

void Test()
{
	string str1 = "hello world!";
	cout << "str1[0] = " << str1[0] << endl;
	cout << "str1.at(0) = " << str1.at(0) << endl;
	cout << "str1[str1.size()] = " << str1[str1.size()] << endl; //输出'\0'
	//cout << str1.at(str1.size()) << endl;						 //error 出现异常
}

int main()
{
	Test();
	system("pause");
	return 0;
}

运行结果:
STL string容器用法
3.3 从String取得const char*
data()方法与c_str()方法相似,都返回const char类型。两者区别和联系如下:
1)在C++98版本中,c_str()返回const char
类型,返回的字符串会以空字符结尾。
2)在C++98版本中,data()返回const char*类型,返回的字符串不以空字符结尾。
3)在C++版本中,c_str()与data()用法相同。
【例】

#include<iostream>
#include<string>

using namespace std;

void Test()
{
	string str1 = "hello world!";
	cout << "str1.c_str() = " << str1.c_str() << endl;
	cout << "str1.data() = " << str1.data() << endl;
}

int main()
{
	Test();
	system("pause");
	return 0;
}

运行结果:
STL string容器用法
3.4 把string拷贝到char指向的内存空间的操作
int copy(char
s, int n, int pos=0)const;
把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符串数组中,返回实际拷贝的数目。注意要保证s所指向的空间足够大以容纳当前的字符串,不然会越界。

string s1("hello");
char arr[10] = "";
s1.copy(arr, 2);
cout << s1.at(1) << endl;
cout << s1.operator[](1) << endl;
cout << arr << endl;

3.5 string的长度
3.5.1 int length() const
返回当前字符串的长度,长度不包括字符串结尾的’\0’。
3.5.2 int size() const
同上。
【例】

#include<iostream>
#include<stdlib.h>
#include<string.h>

using namespace std;

int main()
{
	int size = 0;
	int length = 0;
	unsigned long maxsize = 0;
	int capacity = 0;
	string str("12345678");
	string pstr;
	pstr = str;
	pstr.reserve(20);
	size = pstr.size();
	length = pstr.length();
	capacity = pstr.capacity();
	cout << "size=" << size << endl;
	cout << "length=" << length << endl;
	cout << "capacity=" << capacity << endl;
	cout << "pstr=" << pstr.c_str() << endl;
	system("pause");
	return 0;
}

运行结果:
STL string容器用法
3.5.3 reserve()
函数原型是:void reserve(size_type n),预留容量,但是不改变容器有效个数。对于n值得大小,有两种情况:
(1)如果n小于capacity值,则没有作用。
(2)如果n大于capacity值,则预留足够的空间,但是有效元素个数不变。
【例】

#include<iostream>
#include<stdlib.h>
#include<string.h>

using namespace std;

int main()
{
	int size = 0;
	int length = 0;
	unsigned long maxsize = 0;
	int capacity = 0;
	string str("12345678");
	string pstr;
	pstr.reserve(20);
	pstr = str;
	size = pstr.size();
	length = pstr.length();
	capacity = pstr.capacity();
	cout << "size=" << size << endl;
	cout << "length=" << length << endl;
	cout << "capacity=" << capacity << endl;
	cout << "pstr=" << pstr.c_str() << endl;
	system("pause");
	return 0;
}

运行结果:
STL string容器用法
释:pstr.reserve(20)预留容量后,size值和length值不变,capacity值变大了。
3.5.4 resize()
函数原型:void resize(size_type n, const T& c = T()),用于调整有效元素的个数。其中,n是要保留的元素个数,如果是要新增元素的话,c则是新增元素的默认初始值。对于n的值,有三种情况:
(1)n等于容器当前的大小,则什么也不做。
(2)如果n小于容器当前的大小,则删除末尾的size()-n个元素,但是不会释放内存。
【例】pstr.resize(2);

#include<iostream>
#include<stdlib.h>
#include<string.h>

using namespace std;

int main()
{
	int size = 0;
	int length = 0;
	unsigned long maxsize = 0;
	int capacity = 0;
	string str("12345678");
	string pstr;
	pstr = str;
	pstr.resize(2);
	size = pstr.size();
	length = pstr.length();
	capacity = pstr.capacity();
	cout << "size=" << size << endl;
	cout << "length=" << length << endl;
	cout << "capacity=" << capacity << endl;
	cout << "pstr=" << pstr.c_str() << endl;
	system("pause");
	return 0;
}

(3)如果n大于容器当前的大小,则在容器的末尾插入n-size()个初始值为c的元素;如果没有指定初始值,则默认构造函数初始化。
【例】pstr.resize(20);

#include<iostream>
#include<stdlib.h>
#include<string.h>

using namespace std;

int main()
{
	int size = 0;
	int length = 0;
	unsigned long maxsize = 0;
	int capacity = 0;
	string str("12345678");
	string pstr;
	pstr = str;
	pstr.resize(20);
	size = pstr.size();
	length = pstr.length();
	capacity = pstr.capacity();
	cout << "size=" << size << endl;
	cout << "length=" << length << endl;
	cout << "capacity=" << capacity << endl;
	cout << "pstr=" << pstr.c_str() << endl;
	system("pause");
	return 0;
}

运行结果:
STL string容器用法
注:reserve和resize的共同点就是都不缩减容器本身的容量。

3.6 string的赋值
3.6.1 operator=
string& operator=(const string& s): 把字符串s赋给当前的字符串。
string& operator=(const char* s):同上。
3.6.2 assign
3.6.2.1 string& assign(const char* s)
把字符串s赋给当前的字符串。
3.6.2.2 string& assign(const char* s, int n)
把字符串s的前n个字符赋给当前的字符串。
3.6.2.3 string& assign(const string& s)
把字符串s赋给当前字符串。
3.6.2.4 string& assign(int n, char c)
用n个字符c赋给当前字符串。
3.6.2.5 string& assign(const string& s, int start, int n)
把字符串s中start开始的n个字符赋给当前字符串。
【例】

#include<iostream>
#include<string>

using namespace std;

int main()
{
	string str1("hello");
	string str2;
	string str3;
	string str4;
	string str5;

	str2.assign(str1);
	str3.assign("world", 4);
	str4.assign(str1, 2, 3);
	str5.assign(6, 'd');

	cout << "str1=" << str1 << endl;
	cout << "str2=" << str2 << endl;
	cout << "str3=" << str3 << endl;
	cout << "str4=" << str4 << endl;
	cout << "str5=" << str5 << endl;

	system("pause");
	return 0;
}

运行结果:
STL string容器用法
3.6.2.2 string& operator
3.7 string字符串连接
3.7.1 operator+=()
3.7.1.1 string& operator+=(const string& s)
把字符串s连接到当前字符串。
3.7.1.2 string& operator+=(const char* s)
把字符串s连接到当前字符串结尾。
3.7.2 append
3.7.2.1 string& append(const chars)
把字符串s连接到当前字符串结尾。
3.7.2.2 string& append(const char
s, int n)
把字符串s的前n个字符连接到当前字符串结尾。
3.7.2.3 string& append(const string& s)
同operator+=()。
3.7.2.4 string& append(const string& s, int pos, int n)
把字符串s中从pos开始的n个字符连接到当前字符串结尾。
3.7.2.5 string& append(int n, char c)
在当前字符串结尾添加n个字符c。
【例】

#include<iostream>
#include<string>

using namespace std;

int main()
{
	char* str = "666";
	string str1 = "I like C++";
	string str2 = ",I like you!";
	string str3 = "hello ";
	string str4("world");
	string str5;
	string str6;

	str5.append(str1);
	str1.append(str2);
	str3.append(str4, 0, 5);
	str4.append(str, 4);
	str6 += string(str5) + str3[0];

	cout << "str1=" << str1 << endl;
	cout << "str2=" << str2 << endl;
	cout << "str3=" << str3 << endl;
	cout << "str4=" << str4 << endl;
	cout << "str5=" << str5 << endl;
	cout << "str6=" << str6 << endl;

	system("pause");
	return 0;

}

运行结果:
STL string容器用法
3.8 string比较
3.8.1 int compare(const string& s)const
与字符串s比较。
3.8.2 int compare(const char* s)const
与字符串s比较。
注:compare函数在>时返回1,<时返回-1,==时返回0。此外,还支持>、<、>=、<=、==直接比较。
3.9 string的子串
string substr(int pos=0, int n=npos)const:返回由pos开始的npos个字符组成的字符串。
【例】

#include<iostream>
#include<string>

using namespace std;

void Test()
{
	string str1("hello world!");
	string str2;
	str2 = str1.substr(6, 5);
	cout << "str1 = " << str1 << endl;
	cout << "str2 = " << str2 << endl;
	cout << "str2 - str1 = " << str1.compare(str2) << endl;
	cout << "str1 == str1 = " << str1.compare(str1) << endl;
	cout << "str1 - str2 = " << str2.compare(str1) << endl;
}

int main()
{
	Test();
	system("pause");
	return 0;
}

运行结果:
STL string容器用法
3.10 string的查找和替换
3.10.1 查找
3.10.1.1 正向查找
1、int find(char c, int pos=0)const:从pos开始查找字符c在字符串中的位置。
2、int find(const char* s, int pos=0)const:从pos开始查找字符串s在字符串中的位置。
3、int find(const string& s, int pos=0)const:从POS开始查找字符串s在字符串中的位置。
4、find函数如果查找不到就返回-1。
3.10.1.2 反向查找
1、int rfind(char c, int pos=npos)const:从pos开始从后向前开始查找字符c在字符串中的位置。
2、int rfind(const char* s, int pos=npos)const:从POS开始从后向前开始查找字符串s在字符串中的位置。
3、int rfind(const string& s, int pos=npos)const:从POS开始从后向前查找字符串s在字符串中的位置。
注:rfind是反向查找的意思,如果查找不到返回-1。
【例】

#include<iostream>
#include<string>

using namespace std;

void Test()
{
	string s = "hello world!";
	cout << "s = " << s << endl;

	cout << "s.find(\"l\") = " << s.find("l") << endl;

	if (s.find("l") == s.npos)
	{
		cout << "404 not found" << endl;
	}
	else
	{
		cout << "s.find(\"l\") = " << s.find("l") << endl;
	}

	//指定位置查找
	cout << "s.find(\"l\", 5) = " << s.find("l", 5) << endl;

	//找到目标字符在字符串中第一次出现和最后一次出现的位置
	cout << "s.find_first_of(\"l\") = " << s.find_first_of("l") << endl;
	cout << "s.find_last_of(\"l\") = " << s.find_last_of("l") << endl;

	//反向查找
	cout << "s.rfind(\"l\") = " << s.rfind("l") << endl;
}

int main()
{
	Test();
	system("pause");
	return 0;
}

运行结果:
STL string容器用法
3.10.2 替换
3.10.2.1 string &replace(int pos, int n, const char* s)
删除从POS开始的n个字符,然后在POS处插入字符串s。
3.10.2.2 string &replace(int pos, int n, const string& s)
删除从POS开始的n个字符,然后在POS处插入字符串s。
3.10.2.3 void swap(string &s2)
交换当前字符串与s2的值。
【例】

#include<iostream>
#include<string>

using namespace std;

void Test()
{
	string str1("hello world!");
	string str2 = "666";
	string str3 = "gird";
	cout << "str1 = " << str1 << endl;
	cout << "str2 = " << str2 << endl;

	str2.swap(str1);
	cout << "交换后 str1 = " << str1 << endl;
	cout << "交换后 str2 = " << str2 << endl;

	str2.replace(6, 5, str3);
	cout << "取代后 str2 = " << str2 << endl;	
}

int main()
{
	Test();
	system("pause");
	return 0;
}

运行结果:
STL string容器用法
3.11 string的区间删除和插入
3.11.1 插入insert
1、string& insert(int pos, const char* s):在POS位置插入字符串s。
2、string& insert(int pos, const string& s):在POS位置插入字符串s。
3.11.2 删除erase
string& erase(int pos=0, int n=npos):删除POS开始的n个字符,然后返回删除后的字符串。
【例】

#include<iostream>
#include<stdlib.h>
#include<string>

using namespace std;

void Test()
{
	string str1("hello world!");
	string str2 = "666";
	string str3 = "gird";
	cout << "str1 = " << str1 << endl;
	cout << "str2 = " << str2 << endl;
	cout << "str3 = " << str3 << endl;
	
	str1.erase(6, 5);
	str3.insert(0, str2);

	cout << "删除后 str1 = " << str1 << endl;
	cout << "插入数据后 str3 = " << str3 << endl;
}

int main()
{
	Test();
	system("pause");
	return 0;
}

运行结果:
STL string容器用法
3.12 把数字转换成字符串
string to_string(value):把数字转换成字符串。
【例】

#include<iostream>
#include<string>

using namespace std;

void Test()
{
	int num = 888;
	cout << "int num = " << num << endl;

	string str1;
	str1 = to_string(num);
	cout << "string str1 = " << str1 << endl;
}

int main()
{
	Test();
	system("pause");
	return 0;
}

运行结果:
STL string容器用法

上一篇:STL线程安全


下一篇:c++ 关于二分的STL 详解