目录
find_first_of()、find_last_of()
find_first_not_of()、find_last_not_of()
string类的概念
string在C++中,是C++标准库中是用于处理字符串的标准库类,它提供了一系列成员函数和操作符,使得字符串的操作更加方便和灵活
使用string必须包含头文件<string>
string的基础应用
七个常用string定义:
对于这常用七个定义的代码解释:
七个代码的使用:
完整代码:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s0("Initial string");
//string()
string s1;//构建一个长度为零人字符的空字符串
//string(const string& str)//str为字符串
string s2(s0);//构造副本
//string(const string& str,size_t pos, size_t len = npos);//pos指字符串位置,len为长度
string s3(s0, 8, 3);//复制从字符串pos位置开始并跨越len字符的字符串部分(如果len大于字符串长度,就到字符串尾部(即npos位置))
//string(const char* s);//字符系列
string s4("A character sequence");//复制由s指向的以null结尾的字符序列
//string(const char* s,size_t n);//n为长度
string s5("Another character sequence", 12);//从s指向的字符数组中复制前n个字符
//string(size_t n,char c)//c为字符
string s6a(10, 'x');//用字符c的n个连续副本填充字符串
string s6b(10, 42);
//template<class InputIterator>
//string(InputIterator first,InputIterator last);//first为开始位置,last为结束位置
string s7(s0.begin(), s0.begin() + 7);//按相同的顺序复制范围[first,last)中的字符序列
//打印
cout << "s1: " << s1 << endl;
cout << "s2: " << s2 << endl;
cout << "s3: " << s3 << endl;
cout << "s4: " << s4 << endl;
cout << "s5: " << s5 << endl;
cout << "s6a: " << s6a << endl;
cout << "s6b: " << s6b << endl;
cout << "s7: " << s7 << endl;
return 0;
}
string字符调用方式
string常用的字符调用
学习目的:学习不同的调用方法和其适用范围,可以更合理的调用相关方法来对处理字符串(下面仅为例子,实际操作时可以实现更多功能)
扩展知识--迭代器
概念
迭代器是一种检查容器内元素并遍历元素的数据类型,通常用于对C++中各种容器内元素的访问和操作处理
string迭代器代码
注意:
- 普通迭代器可以对string数组进行修改,而const迭代器无法对string数组进行修改
#include <iostream> #include <string> using namespace std; void Test1(const string& s)//const迭代器 { cout << "const迭代器打印:"; string::const_iterator it = s.begin(); while (it != s.end()) { cout << *it; it++; } cout << endl; } void Test2(const string& s)//const反向迭代器 { cout << "const反向迭代器打印:"; string::const_reverse_iterator it = s.rbegin(); while (it != s.rend()) { cout << *it; it++; } cout << endl; } int main() { string s("Initial string"); cout << "迭代器打印:"; string::iterator it = s.begin(); while (it != s.end()) { cout << *it; it++; } cout << endl; cout << "反向迭代器打印:"; string::reverse_iterator rit = s.rbegin(); while (rit != s.rend()) { cout << *rit; rit++; } cout << endl; Test1(s);//const迭代器调用 Test2(s);//const反向迭代器调用 return 0; }
string的成员函数
string成员函数目录
- size()、length():返回字符串的长度
- max_size():最大长度
- empty():检查字符串是否为空
- clear():清数据
- reserve():更改调整字符串容量
- at():和数组用法相同,用于定位某个位置的字符串
- front():访问头字符
- back():访问尾字符
- append()、push_back():添加
- operator[]:通过索引访问字符串中的字符(如s[10],就是用 [ ] 定位到10这个位置)
- operator+=:使字符串可以相加
- shrink_to_fit():缩容(一般不用)
- insert():用于字符的插入
- erase():用于删除字符
- replace():用于替换
- find()、rfind():查找子字符串在主字符串中的位置
- substr():获取子字符串
- find_first_of()、find_last_of():字符串中搜索与其参数中指定的任何字符匹配的首个字符
- find_first_not_of()、find_last_not_of():字符串中搜索与其参数中指定的任何字符不匹配的首个字符
- getline():输入
size()、length()
用于获取整个字符串的长度(包括空格)
max_size()
用于获取整个字符串的最大长度(不同编译器可能结果不同)
empty()
用于判断数组是否为空(0为假,1为真)
clear()
用于清空整个字符串,但是只清空字符串,不会影响原字符串的空间大小
reserve()
用于调整字符串空间,可扩容,可缩容
如果调节大小大于当前字符串容量,则该函数会导致容器将其容量增加到设定字符位置的字符空间大小
尽量不用于缩容,因为不同平台之间结果不同(有的编译器会缩容,有的不会,所以尽量不用其来进行缩容操作)
at()
使用方法和数组类似,可直接对设定位置进行操作
front()、back()
分别访问字符串的头字符和字符串的尾字符,可对其进行修改操作和访问等功能
append()
用于字符串的添加操作(下图为append函数的使用方法和格式)
样例声明:
操作结果:
操作代码(解释请看备注,其对应格式在操作代码上方标注着):
#include <iostream> #include <string> using namespace std; int main() { string str; string str2 = "A"; string str3 = "+=-_"; cout << "操作前的数组:" << str << endl; //string& append (const string& str); //添加字符串 str.append(str2);//直接添加str2 //string& append(const string & str, size_t subpos, size_t sublen = npos); //添加字符串设定位置上,以及之后设定的数量内的字符 str.append(str3, 1, 3);//添加str3从1开始往后数3个的字符 //string& append(const char* s, size_t n); //从头开始添加一串字符上设定数量的字符 str.append("12345678", 5);//添加前5个 //string& append(const char* s); //添加一串字符序列 str.append("!@#$%");//直接添加字符序列 //string& append(size_t n, char c); //添加设定数量的字符 str.append(10, '.');//添加10个. /*template <class InputIterator> string& append(InputIterator first, InputIterator last);*/ //添加设定范围从开始位置到结束前位置的数组范围内的字符串( 范围:[first,last) 不包括last) str.append(str3.begin() + 3, str3.end());//添加从str3的begin()+3位置开始到结束的字符 cout << "操作后的数组:" << str << endl; return 0; }
push_back()
用于字符串的添加操作(下面为push_back的操作格式)
将字符 c 附加到字符串的末尾,将其长度增加 1(只增加一个,不管添加的字符串有多长,都只添加1个字符)
operator
重载了运算符的 [ ] 和 += 、= 等一系列运算符,让字符串的定位和添加等操作成为了可能,对应的重载格式为下图:
insert()
用于字符串的插入功能(由于和append很多功能类似,所以只展示它插入字符串的功能)
(谨慎使用,因为insert的本质是挪动数据,会消耗不必要的时间和资源)
下面为:在pos位置插入字符串的n个(第一位为pos,第二位为n,第三位为字符串)
erase()
用于删除字符串的设定位置,减少其长度,如果字符串长度不够需要达到的长度,就删到字符串尾为止
样例声明:
操作结果:
操作代码(解释请看备注,其对应格式在操作代码上方标注着):
#include <iostream> #include <string> using namespace std; int main() { string str("ABCDEFGHIJKLNM12345678"); cout << "操作字符:" << str << endl; //string& erase(size_t pos = 0, size_t len = npos) //删除指定位置开始及之后的特定长度 //第一个参数为位置,第二个参数为删除长度 str.erase(11, 3); cout << "操作后字符:" << str << endl; //iterator erase(iterator p); //单独删除指定位置字符 str.erase(str.begin() + 9); cout << "操作后字符:" << str << endl; //iterator erase(iterator first,iterator last); //删除指定范围内的字符 //第一个参数为头,第二个参数为尾 str.erase(str.begin() + 5, str.end() - 8); cout << "操作后字符:" << str << endl; return 0; }
assign()
为字符串分配一个新值,替换其当前内容(整个字符串替换)
样例声明:
操作结果:
操作代码(解释请看备注,其对应格式在操作代码上方标注着):
#include <iostream> #include <string> using namespace std; int main() { string str; string base = "ABCDEFGHIJKLNM"; //string& assign(const string & str); str.assign(base); cout << str << endl; //string& assign(const string & str, size_t subpos, size_t sublen = npos); str.assign(base, 10, 9); cout << str << endl; //string& assign(const char* s, size_t n); str.assign("123456789", 7); cout << str << endl; //string& assign(const char* s); str.assign("c-string"); cout << str << endl; //string& assign(size_t n, char c); str.assign(10, '*'); cout << str << endl; /*template <class InputIterator> string& assign(InputIterator first, InputIterator last);*/ str.assign(base.begin() + 5, base.end()); cout << str << endl; return 0; }
replace()
string的替换操作,用于替换掉需要替换的部分
常用的操作方法:
指定位置开始往后的指定数量字符,替换成需要的字符
replace的参数说明:第一个参数(样例中的5)字符串的位置,第二个参数(样例中的1)为指定需要替换的字符数量,第三个参数为需要替换的字符串
下面为其他的替换操作(除了上面的为常用的,其他的不常用,可以在需要时再搜索使用)
结果:
完整代码:
#include <iostream> #include <iostream> #include <string> using namespace std; int main() { string base = "this is a test string."; string str2 = "n example"; string str3 = "sample phrase"; string str4 = "useful."; string str = base; cout << "原操作字符串:" << str << endl; //string& replace(size_t pos, size_t len, const string & str); //用字符串替换掉从指定位置开始后面指定数量的字符 //第一个参数为数据,第二个参数为删除的字符串数量,第三个参数为需要替换上的字符串 str.replace(9, 5, str2); cout << str << endl; //string& replace(size_t pos, size_t len, const string & str, size_t subpos, size_t sublen = npos); //第一个为位置,第二个为替换长度,第三个为替换数组,第四个为替换复制到对象的第一个字符的位置,第五个为要复制的子字符串的长度 str.replace(19, 6, str3, 7, 6); cout << str << endl; //string& replace(size_t pos, size_t len, const char* s); //第一个为位置,第二个为长度,第三个为字符串 str.replace(8, 10, "just a"); cout << str << endl; //string& replace(size_t pos, size_t len, const char* s, size_t n); //第一个为位置,第二个为长度,第三个为字符串,第四个要复制的字符数 str.replace(8, 6, "a shorty", 7); cout << str << endl; //string& replace(size_t pos, size_t len, size_t n, char c); //第一个为位置,第二个为长度,第三个为后面第四个需要替换的字符的数量,第四为字符 str.replace(22, 1, 3, '!'); cout << str << endl; //下面为迭代器的使用 str.replace(str.begin(), str.end() - 3, str3); cout << str << endl; str.replace(str.begin(), str.begin() + 6, "replace"); cout << str << endl; str.replace(str.begin() + 8, str.begin() + 14, "is coolness", 7); cout << str << endl; str.replace(str.begin() + 12, str.end() - 4, 4, 'o'); cout << str << endl; str.replace(str.begin() + 11, str.end(), str4.begin(), str4.end()); cout << str << endl; return 0; }
对于replace操作,有时候会占用太多不必要的资源,因为其替换操作会像插入和删除操作一样,移动整个字符串,会造成不必要的浪费,所以更多的用以下方法来代替replace操作
可以看到,通过创建一个字符串,并用for循环进行需要的替换操作后也能达到需要达到的效果
find()、rfind()
两者都为查找,用处为查找字符串中的指定字符串
因为find()和rfind()分别为正向查找和反向查找,所以rfind()就不做演示了
样例声明:
操作结果:
操作代码(解释请看备注,其对应格式在操作代码上方标注着):
#include <iostream> #include <string> using namespace std; int main() { string str("ABCDEFGHIJKL.GA"); string str2("G"); cout << "操作字符串:" << str << endl; //size_t find(const string & str, size_t pos = 0) const; //第一个为查询字符串,第二个位查询起始点(有缺省值,可以不用传) size_t found = str.find(str2); if (found != string::npos) cout << "第一次发现'G'是在:" << found << endl; //size_t find (const char* s, size_t pos, size_t n) const; //第一个为查询字符串,第二个位查询起始点,第三个为需要查询的字符串中的几个字符 found = str.find("G", found + 1, 1);//上面得到了found点,需要查询第二个,就在found点的基础上加1 if (found != string::npos) cout << "第二次发现'G'是在: " << found << endl; //size_t find (const char* s, size_t pos = 0) const; found = str.find("D"); if (found != string::npos) cout << "'D'位置在 " << found << endl; //size_t find(char c, size_t pos = 0) const; //第一个为查询字符,第二个位查询起始点(有缺省值,可以不用传) found = str.find('.'); if (found != string::npos) cout << "'.'的位置在:" << found << endl; //配合替换操作 str.replace(str.find(str2), str2.length(), "1234567"); cout << str << endl; return 0; }
substr()
返回一个新构造的对象,其值初始化为此对象的子字符串的副本
和find函数配合可以获取需要的字符串
或者也可以这样配合,把网址分开
总的来说,只需要配合好其他函数,就可以拿到需要的字符串
find_first_of()、find_last_of()
分别在字符串中搜索与其参数中指定的任何字符匹配的第一个字符(对应first函数)和最后一个字符(对应last函数)
样例声明:
为了更好的观察,所以使找到的字符替换成 * 字符,可以看到他可以找到相应需要查找字符串中的单个字符,如样例中查找ACT,它能单独找到A、C、T三个元素
find_first_of是从前往后找
find_last_of是从后往前找
操作结果:
操作代码:
#include <iostream> #include <string> #include <cstddef> using namespace std; int main() { string str("ABCDEFGHIJKLNMATC"); cout << str << endl; //find_first_of操作 cout << "find_first_of操作" << endl; size_t found1 = str.find_first_of("ACT"); str[found1] = '*'; cout << str << endl; found1 = str.find_first_of("ACT"); str[found1] = '*'; cout << str << endl << endl; str = "ABCDEFGHIJKLNMATC";//恢复字符串原样 cout << str << endl; //find_last_of操作 cout << "find_last_of操作" << endl; size_t found2 = str.find_last_of("ACT"); str[found2] = '*'; cout << str << endl; found2 = str.find_last_of("ACT"); str[found2] = '*'; cout << str << endl; return 0; }
find_first_not_of()、find_last_not_of()
和上面的find_first_of()和find_last_of()用法相同,但作用对象相反,这两个函数是查找除指定字符之外的第一个和最后一个字符
样例声明:
还是一样的操作,对找到的字符变成 * 字符,可以看到除了设定字符外,全部变成了字符*
find_first_not_of是从前往后找
find_last_not_of是从后往前找
操作结果:
操作代码:
#include <iostream> #include <string> #include <cstddef> using namespace std; int main() { cout << "设定字符为'ACT'" << endl; string str("ABCDEFGHIJKLNMATC"); size_t found1 = str.find_first_not_of("ACT"); cout << str << endl; while (found1 != string::npos) { str[found1] = '*'; found1 = str.find_first_not_of("ACT", found1 + 1); } cout << str << endl; return 0; }
getline()
用于字符串的输入功能,以及自定义输入结束符
在学习getline()之前我们先来看看string的输入
可以看到,明明输入的是hello world,可是字符串却只接收到了hello,这是因为在我们进行输入时,空格或回车默认会打断字符串的输入,当我们需要空格等进入字符串时,getline就提供了这个功能
(代码解释:cin为输入(直接照搬),str为输入的字符串)
通过使用getline,我们可以对空格也进行了接收,使回车键才结束字符串的输入,当然getline还有一种功能,就是自定义结束符
可以看到,通过自定义结束符(我设置为了#),我们可以尽情输入,不管是空格还是回车都无法结束我们的输入以及字符串的接收,而当遇到我们设定的结束符时才会结束我们的输入
string相关的转换
下面string转换时,string类的相关参数解释
- str:string对象
- idx:指向 size_t 类型的对象的指针,其值由函数设置为 str 中数值后下一个字符的位置。此参数也可以是 null 指针,在这种情况下,不使用它
- base:确定有效字符及其解释的数字基数
stod
stod函数功能:该函数将转换后的浮点数作为 double 类型的值返回
stod的输入格式:
代码样例:
stof
stof函数功能:该函数将转换后的浮点数作为 float 类型的值返回
stof的输入格式:
代码样例:
stoi
stoi函数功能:该函数将转换后的整数作为 int 值返回
stof的输入格式:
代码样例:
stol
stol函数功能:该函数将转换后的整数作为 long int 类型的值返回
stol的输入格式:
代码样例:
stold
stold函数功能:该函数将转换后的浮点数作为 long double 类型的值返回
stold的输入格式:
代码样例:
stoll
stoll函数功能:该函数将转换后的整数作为 long long 类型的值返回
stoll的输入格式:
代码样例:
stoul
stoul函数功能:该函数将转换后的整数作为无符号 long 值返回
stoul的输入格式:
代码样例:
stoull
stoull函数功能:该函数将转换后的浮点数作为 long double 类型的值返回
stoull的输入格式:
代码样例:
to_string
to_string函数功能:返回一个字符串
to_string的输入格式:
代码样例:
to_wstring
to_wstring函数功能:返回一个 wstring,其中包含 val 的表示形式
to_wstring的输入格式:
代码样例:
到此,我们今天的学习就结束了,感谢观看 \\\\٩( 'ω' )و ////