天天看点

STL容器系列文章:string容器

1. 头文件

2. 定义

  • string类型是存储在内存中的连续的字符串,它隐藏了字符串的数组性质,可当做普通变量处理

3. 对象创建

方法 说明
string str; 创建一个空的string对象。原型:string()
string str(“hello”); 初始化指向”hello”字符串。原型string(const char* s)
string str(10, ‘a’); 包含n个char元素。原型:string(size_type n, char c)
string st1(“hello”); string st2(str1); 拷贝构造函数。原型:string(const string& str)
string str1(“hello”); string str2 = str1; 拷贝构造函数。重载运算符 =
string str = “hello”; C风格的string对象初始化

4. 常用方法

  • 长度
方法 说明
int strLen1 = str.length(); C语言习惯
int strLen2 = str.size(); STL容器
str.empty(); 是否为空
  • 添加
方法 说明
+: string str1(“hello ”); string str2(“world\n”); string str3 = str1 + str2; 拼接字符串
+=: string str1(“hello”); string str2(“world”); str1 += str2; 拼接字符串
string str1(“hello”); string str2(“world”); str1.append(str2); str1.append(“oh”); append()方法
string str(“123”); str.push_back(‘4’); “1234”。push_back(),末尾添加
string str(“123”); str.pop_back(); “12”。pop_back(): 末尾删除
  • 比较:compare()函数
方法 说明
== 0 相等
< 0 小于(指定的字符开始)或 更短
> 0 大于 或 更长
string str1("hello"), str2("hello, world");
int ret = str1.compare(str2);
ret = str1.compare(0, 2, str2);			// str1从0开始的2个元素与str2比较
ret = str1.compare(1, 2, str2, 0, 3);	// str1从1开始的2个元素与str2从0开始的3个元素比较
ret = str1.compare("Hello");			// 与字符串"Hello"比较
ret = str1.compare(1, 2, "Hello");		// str1从1开始的2个元素与"Hello"比较
ret = str1.compare(1, 2, “Hello”, 1, 2);// str1从1开始的2个元素比"Hello"子串(1,2)比较
           
  • 访问
string str("Hello,World!");
cout << str[1] << endl;			// 下标方式访问
cout << str.at(1) << endl;		// at方式访问
           
  • 子串
/* 函数原型 */
string substr(size_t pos = 0, size_t len = npos) const;	   // 从pos开始的len个元素

string str("Hello,World!");
string subStr = str.substr(3, 5);
cout << subStr << endl;				// 输出”lo,Wo”
           
  • 插入
方法 说明
string& insert(pos, str); 在pos之前插入str
string& insert(pos, str, subPos, subLen); pos之前插入字符串str从subPos开始的subLen字符
string& insert(pos, n, c); 在pos之前插入n个字符c
  • 删除
方法 说明
string& erase(pos, len); 删除从pos开始的len个字符
iterator erase(const_iterator ite); 删除ite处的一个字符,返回删除后迭代器位置
iterator erase(const_iterator first, const_iterator last); 删除first到last之间的字符
str.clear(); 清空所有字符
  • 交换
  • 查找:#include

    [1] find: 整体查找,返回元素下标

// case1:
int position = s.find('a'); if (position != s.npos) {std::cout << "找到元素";}

// case2:
int position = s.find('b', 5);	// 从下标5开始查找字符,找到返回绝对下标

// 返回值:找到返回下标,没找到返回一个特殊标记 string::npos 或 s.npos (其值为-1)
           

注释:string.find不光能找字符出现位置,也能找子串出现位置,举例

sting s1 = "I'm Jack";
string s2 = "not ";
s1.insert(s1.find("Jack", 0), s2);
std::cout << s1 << std::endl;		// I'm not Juruo
           

[2] find_first_of: 返回字符或字符串第一次出现的位置

auto position = s.find_first_of('a');

void FindFirstOfTest()
{
	string str = "0123456789abcdef";
	int index = str.find_first_of("456");
	std::cout << index << std::endl;	// 输出4
	int newIndex = str.find_first_of("xy2");
	std::cout << newIndex << std::endl;	// 输出2,x y都找不到,第一个找到的是2,对应下标2
}
           

[3] find_last_of: 返回字符或字符串最后一次出现的位置

[4] find_first_not_of: 返回第一个与子串不匹配的字符的位置

void FindFirstOfTest()
{
    string str="0123456789abcdef";
    int index = str.find_first_not_of("013");
    cout << index  << endl;			// 输出:2,第一个与子串不匹配的字符3对应下标2
    int newIndex = str.find_first_not_of("0123456789abcdef");
    cout << newIndex << endl;		// 输出:-1, 即string::npos,完全匹配
}
           

[5] find_last_not_of: 返回最后一个与子串不匹配的字符的位置

[6] rfind: 完全匹配,同find。反向查找子串最后出现的位置

[7] strstr: 能实现和find函数类似功能

  • strstr参数有两个:主字符串, 子字符串
  • strstr的返回值是指针:找到返回首次出现地址,减去首地址得到索引;找不到返回NULL
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int main()
{
	char a[] = "abcddabc";
	char b[] = "dda";
	int index;
	string str1(a);
	string str2(b);
	int index = str1.find(str2);
	if (index != string::npos) {
		std::cout << "str1找到str2" << std::endl;
	}
	char* pos = strstr(a, b);
	if (pos != null) {
		std::cout << "%s" << pos << std::endl;	// 输出ddabc,首次出现地址输出后得到ddabc
	}
	return 0;
}

           

参考文章:

string详解

string方法

string的find方法

string介绍:推荐阅读

created by shuaixio, 2021.07.28