一、字符串及区别
(1)字符串
字符串是表示字符序列的对象。标准字符串类(string)通过类似于标准字节容器的接口为这些对象提供支持,但添加了专门设计用于使用单字节字符串操作的功能。
该字符串类(string)是一个实例的basic_string类模板使用字符(即字节)作为其类型,其默认char_traits和分配器类型(见basic_string的更多信息的模板)。
请注意,此类(string)独立于所使用的编码处理字节:如果用于处理多字节或可变长度字符(如UTF-8)的序列,此类的所有成员(如长度或大小),以及它的迭代器仍将以字节(不是实际编码字符)的形式运行。
(2)区别
#include <string> , #include <cstring>, #include <string.h>区别。
①#include <string>是 C++中新添加的类库。(如求字符串大小可以用 str.size();)
②#include <string.h>是 C 中的标准类库。 (如求字符串大小可以用 strlen(str);)
③C++ 为了支持 C,同时为了区别 C,所以对 C 标准类库做了一些更改,将 C 类库中的尾缀 .h 去掉 ,添加首字母 c。所以在 C++ 中 #include <cstring>, #include <string.h> 是等同的,代表同一个类库,只不过名称有所区别。 (如求字符串大小可以用 strlen(str);)
二、函数总览
1.迭代器
begin | 返回字符串开头的迭代器 |
end | 返回字符串末尾的理论迭代器,此不应该被引用。因为标准库的函数使用的范围不包括其关闭迭代器指向的元素。 |
rbegin | 字符串反向开头的反向迭代器 |
rend | 返回一个反向迭代器,指向字符串第一个字符前面的理论元素(被认为是它的反向结束) |
2.容量
size / length | 返回字符串长度,返回类型 size_t (unsigned int 类型)和 string::size_type同一类型 |
clear | 清空字符串,返回类型 void |
empty | 判断字符串是否为空,返回类型 void |
注:max_size; resize; capacity; reserve; 这几个不常用,再此不列举,如想了解详情可以参看博主的另一篇vector文章 https://blog.csdn.net/qq_41291253/article/details/89840185。
3.元素访问
operator[] | 获取指定位置的字符,返回类型 char& |
at | 获取指定位置的字符,返回类型 char& |
4.编辑器
operator+= |
通过在当前字符串末尾添加其他字符来扩充字符串 返回string& |
push_back |
将单个字符添加字符串末尾,使其长度加1 返回 void |
assign |
为字符串分配一个新值,替换其当前内容(先清空在复制) 返回string& |
insert |
在pos(或p)指示的字符前面位置插入其他字符 返回string&,如果返回为迭代器类型,则返回插入的第一个字符的迭代器 |
erase |
删除部分字符串,减少其长度 返回string &,如果返回为迭代器类型,则返回现在占据删除第一个位置的字符,如果不存在这样的字符则返回 string::end()。 |
replace |
用新的字符串替换以pos字符开头长度为 len的字符串,(或[i1,i2)之间的范围内的字符串部分) 返回string & |
swap |
用str的内容交换容器的内容,str是另一个字符串对象。长度可能不同。在调用此成员函数之后,此对象的值是调用之前str的值,str的值是此对象在调用之前具有的值 返回 void |
pop_back |
删除最后一个字符删除字符串的最后一个字符,有效地将其长度减少1 返回 void |
append | 通过在其当前值的末尾附加其他字符来扩展字符串 |
5.字符串操作
copy |
将当前字符串对的子字符串复制到s指向的数组中。此子字符串包含从位置pos开始的len个字符。 该函数不会在复制内容的末尾附加空字符。 返回:复制到s指向的数组的字符数。这可能等于len或length()- pos(如果字符串值小于pos + len) |
find |
在字符串中搜索由其参数指定的第一个匹配项的序列(所有字符按顺序完全匹配)。 返回:完全匹配后,第一次匹配的第一个字符的位置。如果未找到匹配项,则函数返回string :: npos |
rfind |
在字符串中搜索由其参数指定的最后一次出现的序列(所有字符按顺序完全匹配)。 返回:完全匹配后,最后一次匹配的第一个字符的位置。如果未找到匹配项,则函数返回string :: npos |
find_fist_of |
在字符串中搜索与其参数中指定的任何字符(随意一个就可以,无先后顺序,也不用完全匹配)匹配的第一个字符。 返回:第一次匹配的第一个字符的位置。如果未找到匹配项,则函数返回string :: npos。 |
find_last_of |
在字符串中搜索与其参数中指定的任何字符匹配的最后一个字符。 返回:匹配的最后一个字符的位置。如果未找到匹配项,则函数返回string :: npos。 |
find_fist_not_of |
在字符串中搜索与其参数中指定的任何字符都不匹配的第一个字符(找不同)。 返回:第一次匹配的第一个字符的位置。如果未找到匹配项,则函数返回string :: npos。 |
find_last_not_of |
在字符串搜索与其参数中指定的任何字符都不匹配的最后一个字符。 返回:匹配的最后一个字符的位置。如果未找到匹配项,则函数返回string :: npos。 |
substr |
返回一个新构造的字符串对象,其值初始化为此对象的子字符串的副本。 |
compare |
将字符串对象(或子字符串)的值与其参数指定的字符序列进行比较。 |
6.非成员函数重载
getline |
从输入流中获取一行字符到回车符终止获取,空格也获取 |
7.成员常数
npos |
size_t 的最大值 |
读者想查看哪一步分内容直接点击下面相应链接即可。
目录
size / length:--->clear:--->empty:
operator+=:--->push_back:--->assign:--->insert:--->erase:--->replace:--->swap:--->pop_back: --->append
copy:--->find:---> rfind:---> find_fist_of:--->find_last_of--->find_fist_not_of:--->find_last_not_of:--->substr:--->compare:
三、函数详解
1.迭代器
rebegin:
返回字符串反向开头的“反向迭代器”。
reverse_iterator rbegin();
函数用例
#include <iostream>
#include <string>
using namespace std;
/*==============================================
函数名称:std::string::rbegin
函数输入:void
函数输出:反向后开头的反向迭代器reverse_iterator
=============================================*/
int main()
{
string str("now step live...");
//for(string::reverse_iterator rit = str.rbegin(); rit != str.rend(); rit++)
//如果为 begin 则 for(string::iterator it = str.begin(); it != str.end(); it++)
for(string::reverse_iterator rit = str.rbegin(); rit <= str.rend(); rit++)
cout << *rit;
cout << endl;
return 0;
}
/*===============================================
函数输出:...evil pets won
===============================================*/
2.容量
size / length:
返回字符串的长度,从 1 开始计数
size_t size()const; //size_t 为 unsigned int 类型
函数用例
#include <iostream>
#include <string>
using namespace std;
/*==============================================
函数名称:std::string::size
函数输入:void
函数输出:字符串中的字节数,size_t类型
=============================================*/
int main()
{
string str("Test string");
cout << "The size of str is " << str.size() << " bytes." << endl;
cout << "The length of str is " << str.length() << " bytes." << endl;
return 0;
}
/*===============================================
函数输出:The size of str is 11 bytes.
The length of str is11 bytes.
===============================================*/
clear:
清空字符串
void clear();
函数用例
/*==============================================
函数名称:std::string::clear
函数输入:void
函数输出:void
=============================================*/
int main()
{
char c;
std::string str;
std::cout << "Please type some lines of text. Enter a dot (.) to finish:\n";
do {
c = cin.get(); //输入单个字符
str += c; //将字符加入到Str尾部,见下文
if (c == '\n')
{
cout << str;
str.clear();
}
} while (c != '.'); //判断语句为真,继续循环
return 0;
}
/*===============================================
函数输出:Please type some lines of text. Enter a dot (.) to finish:
I am a student
I am a student
I am a student.
===============================================*/
empty:
判断字符串是否为空
bool empty()const;
函数用例
/*==============================================
函数名称:std::string::empty
函数输入:void
函数输出:1 - 字符串为空
0 - 字符串非空
函数功能:该程序逐行读取用户输入并将其存储到字符串内容中,直到输入空行。
=============================================*/
int main()
{
string content;
string line;
cout << "Please introduce a text. Enter an empty line to finish: " << endl;
do{
getline(cin, line);
content += line + '\n';
}while(!line.empty());
cout << "The text you introduced was:\n" << content;
return 0;
}
/*===============================================
函数输出:Please introduce a text. Enter an empty line to finish:
I am a student
I am a man
The text you introduced was:
I am a student
I am a man
===============================================*/
3.元素访问
获取字符串中指定位置元素
char&operator [](size_t pos);
char&at(size_t pos);
函数用例
#include <iostream>
#include <string>
using namespace std;
/*==============================================
函数名称:std::string::operator[]
函数输入:POS - 字符串的指定位置
注意:字符串中的第一个字符是值 0 (不是 1 )。
函数输出:字符串中指定位置的元素
注:at()相对于 operator[]多出的功能:at()函数自动检查pos是否是字符串中字符的有效位置
(即,pos是否小于字符串长度),如果不是则抛出out_of_range异常。
=============================================*/
int main()
{
string str("Test string");
for(int i = 0; i < str.size(); i++)
cout << str[i];
cout << endl;
string str2 = "Test string 2";
for(int i = 0; i < str2.length(); i++)
cout << str2.at(i);
cout << endl;
return 0;
}
/*===============================================
函数输出:Test string
Test string 2
===============================