当前位置: 首页 > news >正文

string类的总结

目录

1.为什么要学习string类

2.string的标准库

3.string类的常用接口说明

1.string类对象的常见构造

2.string类对象的容量操作

3.string类对象的3种遍历方法

3.1 [ ] + 下标

3.2 基于范围的for循环

3.3 迭代器

4 string类对象的元素访问

4.1 operator[]:

4.2  at

4.3  front

4.4  back

5.string类对象的修改器(Modifiers)

5.1  operator+=

5.2   push_back

5.3  pop_back

5.4  insert

 5.5  erase

 5.6   append

 5.7    assign

 5.8 swap

5.9  replace

 6.string类对象的修改操作

6.1  copy():

6.2 find()、rfind()、find_first_of()

6.3   compare():

7.string类非成员函数


1.为什么要学习string类

学习 string 类是在 C++ 中非常重要的一步,string 类是 C++ 标准库提供的用于处理字符串的类,它相比 C 语言中的字符串处理函数更为高级、灵活和安全。

功能强大:string 类提供了丰富的成员函数和操作符,用于处理字符串的拼接、查找、替换、截取、插入等操作。使用 string 类能够更方便地实现复杂的字符串处理任务。


安全性:string 类自动处理字符串的内存分配和释放,不会像 C 语言中的字符数组那样容易出现缓冲区溢出等安全问题。使用 string 类能够有效避免许多与字符数组相关的安全漏洞。


可读性:使用 string 类可以使代码更加易读,不需要过多关注底层的字符处理,而是通过成员函数和操作符来操作字符串,使代码更加清晰明了。


便捷性:string 类可以直接进行赋值、比较和连接等操作,无需额外的库函数支持,让代码编写更为简洁。


STL 通用性:string 类是 C++ 标准模板库(STL)中的一部分,学习 string 类也是为了更好地理解和使用 STL 的其他组件,如向量、链表、映射等。


————————————————

本篇文章看的是下面的这个大佬文章

版权声明:本文为CSDN博主「爱学习的鱼佬」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/kingxzq/article/details/131933471

2.string的标准库

1. string是表示字符串的字符串类
2. 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。
3. string在底层实际是:basic_string模板类的别名。
4. 不能操作多字节或者变长字符的序列。
使用string类时,必须包含#include头文件以及using namespace std;

3.string类的常用接口说明

1.string类对象的常见构造

string()
构造空的string类对象,即空字符串
string(const char* s)
用C-string来构造string类对象
string(const string&s)
拷贝构造函数

一般用法 

void Teststring()
{string s1; // 构造空的string类对象s1string s2("hello bit"); // 用C格式字符串构造string类对象s2string s3(s2); // 拷贝构造s3
}

2.string类对象的容量操作

函数名字功能说明
size返回字符串有效字符长度
length返回字符串有效字符长度
resize用于改变字符串对象的长度
max_size返回字符串对象能够容纳的最大字符数
empty检测是否为空串,是返回true,不是返回false
reserve为字符串预留空间
clear清空有效字符

 1.返回字符串有效字符长度,size和length是等价的

size_type size() const;

string s1("hello world");
//利用有效长度逐个打印字符,并间隔打印
for (size_t i = 0; i < s1.size(); ++i)
{cout << s1[i] << ' ';
}

2.返回字符串对象能够容纳的最大字符数

size_type max_size() const;

std::string str;
std::cout << "Max size: " << str.max_size() << std::endl;

 3.用于改变字符串对象的长度、

void resize (size_type n);

void resize (size_type n, charT c);

 第一个版本的 resize() 函数用于改变字符串对象的长度。
resize() 会将字符串的长度调整为 n,如果 n 小于当前长度,则会截断字符串;如果 n 大于当前长度,则会在末尾添加空字符以扩展字符串长度。

第二个版本的 resize() 会将字符串的长度调整为 n,同时用字符 c 来填充新添加的字符(仅当 n 大于当前长度时生效

std::string str = "Hello";
str.resize(10);          // 调整字符串长度为 10,现在 str 为 "Hello     "
str.resize(15, '-');     // 调整字符串长度为 15,用字符 '-' 填充,现在 str 为 "Hello-----"

4.检测字符串释放为空串

bool empty() const;

empty() 函数返回一个 bool 类型的值,如果字符串对象为空,则返回 true,否则返回 false。

std::string str1 = "Hello";
std::string str2;if (str1.empty()) {std::cout << "str1 is empty." << std::endl;
} else {std::cout << "str1 is not empty." << std::endl;
}if (str2.empty()) {std::cout << "str2 is empty." << std::endl;
} else {std::cout << "str2 is not empty." << std::endl;
}

 在上面的示例中,我们创建了两个 std::string 对象 str1 和 str2,然后使用 empty() 函数检查它们是否为空。str1 包含字符 “Hello”,所以 str1.empty() 返回 false,而 str2 是一个空字符串,所以 str2.empty() 返回 true

5.为字符串预留空间

void reserve (size_type n = 0);

在 C++ 的 std::string 类中,reserve() 是一个成员函数,用于请求字符串对象重新分配内存,以便能够容纳至少指定数量的字符。这个函数在字符串需要频繁增长长度时非常有用,它可以避免多次内存分配和拷贝操作,从而提高性能。

std::string str = "Hello";
std::cout << "Capacity before reserve: " << str.capacity() << std::endl; // 输出当前容量
str.reserve(20); // 请求重新分配至少能容纳20个字符的内存空间
std::cout << "Capacity after reserve: " << str.capacity() << std::endl; // 输出重新分配后的容量

6.清空有效字符

void clear();

使用 clear() 函数会将字符串对象的内容清空,即将其长度设置为 0,但并不会释放内存或缩小容量。它会将字符串对象变为空字符串,等效于给字符串赋值一个空字符串。

std::string str = "Hello, World!";
std::cout << "Before clear: " << str << std::endl; // 输出原字符串内容
str.clear(); // 清空字符串内容,使其变为空字符串
std::cout << "After clear: " << str << std::endl;  // 输出空字符串

clear() 函数在清空字符串内容时非常方便,尤其在需要重新使用同一个字符串对象或者需要重置字符串内容的情况下,可以使用这个函数来快速清空字符串。请注意,clear() 只清空字符串内容,不会释放内存或改变容量。如果需要释放内存或缩小容量,可以使用 shrink_to_fit() 函数。

3.string类对象的3种遍历方法

3.1 [ ] + 下标

size()函数返回有效字符的个数,不包括\0,\0是标识字符。

void test_string2()
{string s1("1234");//需求:让对象s1里面的每个字符都加1//如果要让字符串的每个字符都加1,肯定离不开遍历,下面学习三种遍历string的方式。//1.下标 + []for (size_t i = 0; i < s1.size(); i++){s1[i]++;//本质上}cout << s1 << endl;//GB2312兼容ascll编码,所以++后的结果为2345.
}

3.2 基于范围的for循环

std::string str = "Hello";
for (char c : str) {std::cout << c << " "; // 输出:H e l l o
}

3.3 迭代器

迭代器也可以用于遍历 std::string 中的每个字符,它提供了更灵活的方式来访问字符串的内容,可以自定义遍历方式。std::string 类提供了两种迭代器:iterator 和 const_iterator。

iterator:用于修改 std::string 对象中的字符。
const_iterator:用于只读访问 std::string 对象中的字符。

std::string str = "Hello";
for (std::string::iterator it = str.begin(); it != str.end(); ++it) {std::cout << *it << " "; // 输出:H e l l o
}

auto自动推导也可以

for (auto it = str.begin(); it != str.end(); ++it) {std::cout << *it << " "; // 输出:H e l l o
}

4 string类对象的元素访问

函数名称功能说明
operator[]访问字符串中指定位置 pos 处的字符
at访问字符串中指定位置 pos 处的字符
back获取字符串的第一个字符
front获取字符串的最后一个字符

4.1 operator[]:

reference operator[](size_type pos);
const_reference operator[](size_type pos) const;

operator[] 函数用于访问字符串中指定位置 pos 处的字符。返回一个引用,允许您读取或修改该位置上的字符。如果使用 const 修饰符,则表示对字符串进行只读访问。

std::string str = "Hello";
char first_char = str[0];      // 获取字符串的第一个字符 'H'
str[0] = 'h';                  // 修改字符串的第一个字符为 'h',现在 str 为 "hello"

4.2  at

reference at(size_type pos);
const_reference at(size_type pos) const;
at() 函数与 operator[] 类似,也用于访问字符串中指定位置 pos 处的字符。不同之处在于,at() 函数会检查索引是否有效,如果索引超出字符串范围,则抛出 std::out_of_range 异常。

std::string str = "Hello";
char first_char = str.at(0);   // 获取字符串的第一个字符 'H'
str.at(0) = 'h';               // 修改字符串的第一个字符为 'h',现在 str 为 "hello"

4.3  front

reference front();
const_reference front() const;
front() 函数用于获取字符串的第一个字符,返回一个引用。如果使用 const 修饰符,则表示对字符串进行只读访问。

std::string str = "Hello";
char first_char = str.front(); // 获取字符串的第一个字符 'H'

4.4  back

reference back();
const_reference back() const;
back() 函数用于获取字符串的最后一个字符,返回一个引用。如果使用 const 修饰符,则表示对字符串进行只读访问。

std::string str = "Hello";
char last_char = str.back();   // 获取字符串的最后一个字符 'o'

5.string类对象的修改器(Modifiers)

             函数名称                                                         功能说明
           operator+= 用于字符串的连接
           push_back 在字符串末尾添加一个字符
          pop_back删除字符串中的最后一个字符
           insert在指定位置 pos 处插入字符或字符序列
          erase删除指定位置的字符或字符范围
         append在字符串末尾添加字符或字符序列
          assign将新的内容赋值给字符串对象,从而修改字符串的内容
           swap调用该函数的字符串对象与参数表示的另一个字符串对象的内容进行交换
          replace替换指定位置或字符范围的字符

5.1  operator+=

+= 运算符是字符串的连接操作符,也称为字符串的拼接运算符。使用 += 运算符,您可以将一个字符串连接到另一个字符串的末尾,从而实现字符串的拼接。

+= 运算符的原型如下:

basic_string& operator+=(const basic_string& str);
basic_string& operator+=(const charT* s);
basic_string& operator+=(charT ch);

 下面是例子

std::string str = "Hello";
str += " World";   // 将字符串 " World" 连接到 str 的末尾,现在 str 变为 "Hello World"
str += '!';        // 将字符 '!' 连接到 str 的末尾,现在 str 变为 "Hello World!"

 在上述示例中,我们通过使用 += 运算符将字符串 " World" 和字符 ‘!’ 连接到字符串 str 的末尾,实现了字符串的拼接。

5.2   push_back

void push_back(charT ch);
使用 push_back() 函数可以将一个字符 ch 添加到字符串的末尾

std::string str = "Hello";
str.push_back('!'); // 在末尾添加字符 '!',现在 str 变为 "Hello!"

push_back() 函数会直接修改调用它的字符串对象,在字符串的末尾添加指定的字符。在使用 push_back() 之前,应该确保字符串的长度仍然在合理的范围内,避免发生字符串的溢出。在实际使用中,务必注意处理字符串长度和内存的限制。另外,C++11 之后的版本还引入了字符串拼接操作符 +=,可以使用 += 运算符来在字符串的末尾添加字符,更加简洁方便。


5.3  pop_back

pop_back() 函数没有参数,它只需调用它的字符串对象,就会将最后一个字符从字符串中删除。

std::string str = "Hello";
str.pop_back(); // 删除最后一个字符,现在 str 变为 "Hell"

 需要注意的是,使用 pop_back() 之前,应该确保字符串的长度不为零,否则会引发未定义的行为。在调用 pop_back() 之前,通常需要检查字符串是否为空。

5.4  insert

insert() 函数用于在指定位置插入字符或字符序列,从而改变字符串的内容。

basic_string& insert(size_type pos, const charT* s);
这个重载函数在字符串中的指定位置 pos 处插入一个 C 风格字符串(以 null 结尾的字符数组)。

std::string str = "Hello";
str.insert(2, "xx");      // 在位置2处插入字符串 "xx",现在 str 变为 "Hexxllo"

 

basic_string& insert(size_type pos, const charT* s, size_type n);
这个重载函数在字符串中的指定位置 pos 处插入一个指定长度的字符数组。

std::string str = "Hello";
str.insert(3, "xx", 1);   // 在位置3处插入字符数组 "x"(前1个字符),现在 str 变为 "Helxlo"

 

 basic_string& insert(size_type pos, const basic_string& str);
这个重载函数在字符串中的指定位置 pos 处插入另一个 std::string 对象的内容。

std::string str1 = "Hello";
std::string str2 = " World";
str1.insert(5, str2);     // 在位置5处插入 str2 的内容,现在 str1 变为 "Hello World"

 

basic_string& insert(size_type pos, const basic_string& str, size_type subpos, size_type sublen);
这个重载函数在字符串中的指定位置 pos 处插入另一个 std::string 对象的子字符串,从 str 的 subpos 处开始,长度为 sublen。

std::string str1 = "Hello";
std::string str2 = "World";
str1.insert(5, str2, 0, 3);   // 在位置5处插入 str2 的子字符串 "Wor",现在 str1 变为 "HelloWor"

 5.5  erase

erase() 函数用于从字符串中删除指定位置的字符或字符序列,从而修改字符串的内容。

basic_string& erase(size_type pos = 0, size_type n = npos);
这个重载函数删除从指定位置 pos 开始的 n 个字符(默认情况下,删除从 pos 开始的所有字符)。

std::string str = "Hello World";
str.erase(5);       // 删除从位置5(包含)开始的所有字符,现在 str 变为 "Hello"
str.erase(0, 3);    // 删除从位置0(包含)开始的3个字符,现在 str 变为 "lo"

iterator erase(const_iterator position);
这个重载函数删除指定位置 position 处的字符,并返回一个指向删除后的下一个字符的迭代器。

std::string str = "Hello";
auto it = str.erase(str.begin() + 1); // 删除位置1处的字符 'e',现在 str 变为 "Hllo",it 指向 'l'

 5.6   append

append() 函数用于在字符串的末尾添加字符或字符序列,从而实现字符串的拼接。
append() 函数有多个重载形式,提供了不同的方式来添加内容到字符串的末尾。以下是 std::string 类中 append() 函数的几种重载形式及其介绍和使用方法:

basic_string& append(const charT* s);
这个重载函数将一个 C 风格字符串(以 null 结尾的字符数组)添加到字符串的末尾。
 

std::string str = "Hello";
str.append(" World");      // 在末尾添加字符串 " World",现在 str 为 "Hello World"

 

basic_string& append(const charT* s, size_type n);
这个重载函数将一个指定长度的字符数组添加到字符串的末尾。 

std::string str = "Hello";
str.append(" World", 5);   // 在末尾添加字符数组 " Worl"(前5个字符),现在 str 为 "Hello Worl"

 

 basic_string& append(const basic_string& str);
这个重载函数将另一个 std::string 对象的内容添加到当前字符串的末尾。

std::string str1 = "Hello";
std::string str2 = " World";
str1.append(str2);         // 将 str2 的内容添加到 str1 的末尾,现在 str1 为 "Hello World"

 5.7    assign

assign() 函数用于将新的内容赋值给字符串对象,从而修改字符串的内容。

assign() 函数有多个重载形式,提供了不同的方式来赋值新的内容给字符串。以下是 std::string 类中 assign() 函数的几种重载形式及其介绍和使用方法:

basic_string& assign(const charT* s);
这个重载函数将一个 C 风格字符串(以 null 结尾的字符数组)赋值给字符串对象。

std::string str;
str.assign("Hello");      // 将 C 风格字符串 "Hello" 赋值给字符串对象 str

basic_string& assign(const basic_string& str);
这个重载函数将另一个 std::string 对象的内容赋值给当前字符串对象。 

std::string str1 = "Hello";
std::string str2;
str2.assign(str1);       // 将 str1 的内容赋值给 str2

 basic_string& assign(const basic_string& str, size_type pos, size_type n);
这个重载函数将另一个 std::string 对象从指定位置 pos 处开始的 n 个字符赋值给当前字符串对象。

std::string str1 = "Hello";
std::string str2;
str2.assign(str1, 1, 3); // 将 str1 从位置1(包含)开始的3个字符 "ell" 赋值给 str2

 5.8 swap

swap() 函数没有参数,它将调用它的字符串对象与另一个字符串对象进行内容交换。

std::string str1 = "Hello";
std::string str2 = "World";str1.swap(str2); // 将 str1 和 str2 的内容交换,现在 str1 变为 "World",str2 变为 "Hello"

5.9  replace

replace()函数用于将字符串中的一部分内容替换为新的子串。

replace() 函数有多个重载形式,提供了不同的方式来替换字符串中的一部分内容。以下是 std::string 类中 replace() 函数的几种重载形式及其描述和使用方法:

basic_string& replace(size_type pos, size_type count, const charT* s);
这个重载函数从字符串的位置 pos 开始,用指定的 C 风格字符串(以 null 结尾的字符数组)替换 count 个字符。

std::string str = "Hello, World!";
str.replace(7, 5, "Universe"); // 从位置 7 开始,用 "Universe" 替换 5 个字符,现在 str 变为 "Hello, Universe!"

 basic_string& replace(size_type pos, size_type count, const charT* s, size_type n);
这个重载函数从字符串的位置 pos 开始,用指定长度的字符数组中的前 n 个字符替换 count 个字符。

std::string str = "Hello, World!";
str.replace(7, 5, "Earth", 3); // 从位置 7 开始,用 "Ear"("Earth" 的前 3 个字符)替换 5 个字符,现在 str 变为 "Hello, Ear, World!"

 6.string类对象的修改操作

6.1  copy():

size_type copy(charT* dest, size_type count, size_type pos = 0) const;
这个函数从当前字符串中复制 count 个字符到指定位置 pos 开始的字符数组 dest。

std::string str = "Hello";
char buffer[6];
str.copy(buffer, 5); // 将 str 的前 5 个字符复制到 buffer 中,buffer 现在为 "Hello\0"(带有 null 终止符)

6.2 find()、rfind()、find_first_of()

这些函数用于在当前字符串中搜索指定的字符或子字符串,并返回找到的第一个匹配位置或位置偏移。

std::string str = "Hello, World!";
size_t pos = str.find("World"); // 在 str 中找到子字符串 "World",返回位置 7
size_t lastPos = str.rfind("l"); // 从字符串末尾开始找到字符 'l',返回位置 9
size_t foundPos = str.find_first_of(",!"); // 在 str 中找到第一个出现的 ',' 或 '!',返回位置 5

6.3   compare():

int compare(const basic_string& str) const noexcept;
这个函数用于比较当前字符串与另一个 std::string 对象 str 的大小关系。

std::string str1 = "Hello";
std::string str2 = "World";
int result = str1.compare(str2); // 返回一个整数,表示 str1 和 str2 的大小关系(类似于字符串比较的结果)

7.string类非成员函数

1.operator+(重载运算符+):

这个函数模板用于将两个 std::string 对象进行字符串连接,返回一个新的 std::string 对象,包含两个原始字符串的内容。

template <class CharT, class Traits, class Allocator>
basic_string<CharT, Traits, Allocator> operator+(const basic_string<CharT, Traits, Allocator>& lhs,const basic_string<CharT, Traits, Allocator>& rhs);

例子 

std::string str1 = "Hello";
std::string str2 = " World";
std::string result = str1 + str2; // 返回 "Hello World"
std::string str1 = "Hello";
std::string str2 = "World";
bool isEqual = (str1 == str2); // 返回 false,因为 str1 不等于 str2
bool isGreater = (str1 > str2); // 返回 true,因为 str1 大于 str2

 2. swap

template <class CharT, class Traits, class Allocator>
void swap(basic_string<CharT, Traits, Allocator>& lhs,basic_string<CharT, Traits, Allocator>& rhs);

例子

std::string str1 = "Hello";
std::string str2 = "World";
swap(str1, str2); // 将 str1 和 str2 的内容交换,现在 str1 变为 "World",str2 变为 "Hello"

3. getline():

template <class CharT, class Traits, class Allocator>
std::basic_istream<CharT, Traits>& getline(std::basic_istream<CharT, Traits>& is, basic_string<CharT, Traits, Allocator>& str,CharT delim);

例子

std::string str;
std::cout << "请输入一行文本:";
std::getline(std::cin, str); // 从标准输入流中读取一行文本并存储到 str 中
std::cout << "您输入的文本是:" << str << std::endl;

4. npos

npos 是 std::string 类中的一个静态常量成员,用于表示无效或未找到的位置。它是一个特殊的 std::string::size_type 类型的常量,通常被定义为 std::string::npos,其值在不同的编译器和实现中可能不同,但通常被设为 -1 或一个非常大的值,用于表示在字符串中未找到指定的子串或字符。

npos 主要用于字符串查找操作,比如在使用 find()、rfind()、find_first_of()、等成员函数时,当查找失败或没有找到指定的子串或字符时,这些函数通常会返回 std::string::npos 来表示无效的位置。
 

std::string str = "Hello, World!";
std::string::size_type pos1 = str.find("Universe"); // 在 str 中找不到 "Universe",返回 std::string::npos
std::string::size_type pos2 = str.find('X'); // 在 str 中找不到字符 'X',返回 std::string::npos
std::string::size_type pos3 = str.find("World"); // 在 str 中找到 "World",返回 "World" 在 str 中的位置

 注意:npos 的值是一个非常大的无符号整数,因此在比较 std::string::size_type 类型的值时,应使用无符号类型的比较方式,避免可能出现的错误。比如使用 pos != std::string::npos 来判断是否找到了指定的子串或字符。

相关文章:

  • springboot jar包 无法读取静态资源文件
  • py 异步
  • 【2】SM2验签工具和RSA验签工具
  • EasyExcel导入从第几行开始
  • Linux的几个常用基本指令
  • 对象和数据结构
  • 【AI视野·今日Robot 机器人论文速览 第六十二期】Wed, 25 Oct 2023
  • debian 修改镜像源为阿里云【详细步骤】
  • Leetcode 【2342. 数位和相等数对的最大和】
  • 【Spring】AOP进阶-JoinPoint和ProceedingJoinPoint详解
  • 实力进阶,教你使用thinkphp6开发一款商城系统
  • 电力感知边缘计算网关产品设计方案-网关软件架构
  • 金融业务系统: Service Mesh用于安全微服务集成
  • 好用的开源项目地址
  • ImportError: DLL load failed while importing _iterative: %1 不是有效的 Win32 应用程序。
  • codis proxy处理流程
  • Cookie 在前端中的实践
  • js继承的实现方法
  • js正则,这点儿就够用了
  • mysql 5.6 原生Online DDL解析
  • Mysql5.6主从复制
  • oldjun 检测网站的经验
  • passportjs 源码分析
  • select2 取值 遍历 设置默认值
  • Spark学习笔记之相关记录
  • Terraform入门 - 1. 安装Terraform
  • Traffic-Sign Detection and Classification in the Wild 论文笔记
  • yii2权限控制rbac之rule详细讲解
  • 七牛云假注销小指南
  • 前端工程化(Gulp、Webpack)-webpack
  • 区块链共识机制优缺点对比都是什么
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • 一天一个设计模式之JS实现——适配器模式
  • 译有关态射的一切
  • 看到一个关于网页设计的文章分享过来!大家看看!
  • AI算硅基生命吗,为什么?
  • Linux权限管理(week1_day5)--技术流ken
  • 好程序员web前端教程分享CSS不同元素margin的计算 ...
  • ​LeetCode解法汇总1276. 不浪费原料的汉堡制作方案
  • ​软考-高级-系统架构设计师教程(清华第2版)【第1章-绪论-思维导图】​
  • # 透过事物看本质的能力怎么培养?
  • #define MODIFY_REG(REG, CLEARMASK, SETMASK)
  • #define与typedef区别
  • (+4)2.2UML建模图
  • (Matalb回归预测)PSO-BP粒子群算法优化BP神经网络的多维回归预测
  • (Note)C++中的继承方式
  • (Redis使用系列) Springboot 实现Redis消息的订阅与分布 四
  • (八)Spring源码解析:Spring MVC
  • (已解决)报错:Could not load the Qt platform plugin “xcb“
  • (转)甲方乙方——赵民谈找工作
  • .bat批处理(十一):替换字符串中包含百分号%的子串
  • .net中调用windows performance记录性能信息
  • /etc/shadow字段详解
  • @SuppressWarnings(unchecked)代码的作用
  • @Transactional 详解