C++ STL 之 string

string 是 C++ 对数据结构字符动态数组的实现。要使用 string,请在程序头添加:

#include <string>
using name space std;

string 对象的定义与初始化

string<int> s; 定义一个空 string 对象
string<int> s(3,'a'); s = “aaa”
string<int> s("aaa");
string<int> s1(s); s1 = s
string<int> s1 = s;
string<int> s { "abc" }; s = “abc”
string<int> s = { "abc" };
string<int> s = "abc";

高级 string 对象构造方法

string s(cp, n) 拷贝 cp 指向的数组中的前 n 个字符
string s(s1, pos) s 是 string s1 从下标 pos 开始的拷贝
string s(s1, pos, len) s 是 string s1 从下标 pos 开始的长度为 len 的拷贝

substr操作
string s = s1.substr(pos, n) s 是 s1 从下标开始,长度为 n 的拷贝。

向 string 对象添加元素

string 可以看做是一种专门用于存储字符的 vector,因此与 vector 大部分的操作也类似。

使用 push_back 在 string 尾部插入元素

string s ("abc");
s.push_back('d'); // s = "abcd";

使用 insert 在 string 任意位置插入元素

插入单个元素

string s ("abc");
s.insert(s.begin(), 'd'); // s = "dabc";

插入一段元素

string s ("ab");
string s1 ("cd");
s1.insert(s1.begin(), s.begin(), s.end()); // s1 = "abcd";

在循环中插入元素
插入元素通常会使迭代器失效,这会给在循环中插入元素带来不小的麻烦。insert 操作在插入元素成功后会返回插入位置的有效迭代器。

string s ("ab");
string s1 ("cd");
auto it = s1.begin();
for (auto c:s)
	auto it = s1.insert(it, c);
// s1 = "bacd";

删除 string 中的一个元素

使用 pop_back 删除 string 尾元素

string s ("abc");
s.pop_back(); // s = "ab";

使用 erase 删除 string 中任意位置的元素

删除一个元素

string s ("abc");
s.erase(s.begin()); // s = "bc";

删除一段元素

string s ("abc");
s.erase(s.begin(), s.begin()+2); //  s = "c";

在循环中删除元素
删除元素通常会使迭代器失效,这会给在循环中删除元素带来不小的麻烦。erase 操作在插入元素成功后会返回插入位置的有效迭代器。

string s ("abcd");
auto it = s.beign();
while (it!=s.end()) {
	//删除 c 之前的字母
	if (*it < 'c')
		auto it = s.erase(it);
	else
		it++;
	}
// s = "c";

访问 string 中的元素

使用下标访问

像数组一样,string 支持下标随机访问

string s ("abcd");
cout << s[1]; // 输出 b

使用迭代器进行访问

string s ("abcd");
//将 s 中在 c 前面的字母置为 c
for (auto it=s.begin(); it!=s.end(); it++) {
    if (*it < c)
    	*it = c;
}
// s = "cccd"

使用 C++ 11 新特性访问

string s ("abcd");
//输出 s 中在 c 之前的字母
for (auto e : s) {
    if (e < 'c') 
    	cout << e;
}
// 输出:ab

注意:这种方式得到的 e 是 s 中元素的拷贝,若想要得到 s 元素的本身,请使用 &
for (auto &e:s)

string 搜索操作

string 类提供了 6 个不同的搜索函数。每个搜索函数都返回一个 string :: size_type 类型的值,表示匹配发生位置的下标。如果搜索失败,则返回一个名为 string :: npos 的 static 成员 (string :: size_type npos = -1,由于 string :: size_type 实际上就是 unsigned int 类型,故 npos == UINT_MAX)

s.find(c)s.find(s1)
在 s 搜索指定的字符 c 或者字符串 s1,返回第一个匹配位置的下标。

string s {"hello"};
cout << s.find('l'); // 输出 2
cout << s.find("he");// 输出 0
cout << s.find("eo");// 输出 UINT_MAX

s.rfind(c)s.rfind(s1)
在 s 中搜索指定的字符 c 或者字符串 s1,返回最后一个匹配位置的下标。

s.find_first_of(c)s.find_first_of(s1)
在 s 中搜索指定的字符 c 或者字符串 s1 中的任何一个字符,返回第一个匹配位置的下标。

string s {"hello"};
cout << s.find_first_of('l'); // 输出 2
cout << s.find_first_of("eo");// 输出 1

s.find_last_of(c)s.find_last_of(s1)
在 s 中搜索指定的字符 c 或者字符串 s1 中的任何一个字符,返回最后一个匹配位置的下标。

s.find_first_not_of(c)s.find_first_not_of(s1)
在 s 中搜索不是字符 c 或者不在字符串 s1 中的字符,返回第一个匹配位置的下标。

string s {"hello"};
cout << s.find_first_not_of('h'); // 输出 1
cout << s.find_first_not_of("heo");// 输出 2

s.find_last_not_of(c)s.find_last_not_of(s1)
在 s 中搜索不是字符 c 或者不在字符串 s1 中的字符,返回最后一个匹配位置的下标。

指定开始搜索的位置

上述 string 的 6 个搜索函数都有一个带有指定开始加搜索位置的参数的重载版本。如:
s.find(c, string::size_type pos)

数值转换

将其他类型的值转换为 string 对象
to_string(val)

将 string 对象转换为其他类型的值
stoi(s, p, b)
stol(s, p, b)
stoul(s, p, b)
stoll(s, p, b)
stoull(s, p, b)
stof(s, p)
stod(s, p)
stold(s, p)

string 流

sstream 头文件定义了三个类型来支持内存 IO。
istringstream 从 string 读取数据,ostringstream 向 string 写入数据,而 stringstream 既可以从 string 读取数据,也可以向 string 写入数据。要使用这些类型,需要包涵头文件
include<sstream>
using namespace std;

string 流的创建与绑定

istringstream is; 创建一个输入流
ostringstream os;
stringstream ss;
istringstream is(s); 创建一个输入流并绑定 string s
ostringstream os(s);
stringstream ss(s);
is.str(); 返回 is 绑定的 string
os.str();
ss.str();
is.str(s); 将 string s 拷贝(绑定)到 is 中
os.str(s);
ss.str(s);

使用 istringstream

在进行处理文本时,如果文本中的元素类型混杂(例如既有数字也有单词),亦或元素的数目不定。使用 istringstream 将带来很大的方便。

例如,输入算式的值,需要我们计算这个算式的值。一个必要的预处理便是将输入字符串中的数字和运算符号解析出来。使用 istreamstring 可以很轻松的完成这一工作

string str;
cin >> str;
str.push_back('$'); // 为方便处理,添加一个结束标记符 $
istringstream is(str);
vector<int> nums;
vector<char> syms;
int n;
char c;
while (is >> n) {
	nums.push_back(n);
	is >> c;
	syms.push_back(c);
}
// 输入:2*3 - 5/4 + 6
// nums = { 2, 3, 5, 4, 6 }; syms = { '*', '-', '/', '+', '$' };

使用 ostringstream

有时,我们输出的内容中包含多种多样的元素类型,此时可先将一些输出写入输出流,带输出流填充完毕后,再一次性地输出。

例如,假设我们要读取一行以空格分开的数字,并将分隔符由空格变为等号输出。

string str;
getline(cin, str); // 输入:1 2 3 4 5
istringstream is(str);
ostringstream os;
int n;
is >> n; // 为了不在最末尾添加‘,’,进行了一下调整
os << n;
while (is >> n) {
	os << ',';
	os << n;
}
cout << os.str(); // 输出:1,2,3,4,5

常见字符串预处理

转换大小写

#include<algorithm>

string str = "Hello, World!";
// 转换为小写
for_each(str.begin(), str.end(), [](char &ch){ ch = tolower(ch); });
// 转换为大写
for_each(str.begin(), str.end(), [](char &ch){ ch = toupper(ch); });

分割

当分割符只有空格时,参看上文 istringstream 使用简例

当有多个分隔符时

vector<string> split(const string &str, const string &separators) {
	vector<string> words;
	int i = 0;
	while (i < str.size()) {
		while (i < str.size() && separators.find(str[i]) < separators.size())
			++i;
		if (i >= str.size())
			break;
		int j = min(str.find_first_of(separators, i), str.size());
		words.push_back(str.substr(i, j - i));
		i = j + 1;
	}
	return words;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章