最近在学C++,自己整理了一部分C++库函数<string>的一些 函数,主要都是C++的,想到以后可能会用到,所以打算记录一下,方便自己也方便了大家。在啃这些函数的时候有很多借鉴之处,在这里先将这些网页发出来,感谢这些作者的奉献与付出。
目录
1.2使用string(size_type n,char c) 定义 n 个 char 的字符串
1.3使用string(const string &str)定义为其他的 sting
2.1.1 使用 stirng.size() 函数获取string对象长度 --->>为了兼容STL容器而引进的
2.1.2使用 stirng.lenght() 函数获取string对象长度 --->>C语言习惯保留
2.1.3获取string对象最大长度——string.max_size();
2.3.3使用string.append() 函数 添加字符
2.4使用string.push_back() 函数 添加字符
1.stirng.compare(const basic_string& str);
2.string.compare(const CharT* s) const;
3.string.compare(size_type pos1, size_type n1, const basic_string& str);
4.string.compare(size_type pos1, size_type n1, const CharT* s) ;
5.string.compare(size_type pos1, size_type n1, const CharT* s, size_type n2);
string.substr(size_type pos = 0, size_type n = npos);
5.1使用 string.find() 函数查找字符 ( 从前向后 )
5.1.1string.find(const CharT* s, size_type pos = 0);
5.1.2string.find(const basic_string& str, size_type pos = 0);
5.1.3string.find(CharT c, size_type pos = 0);
5.1.4string.find(const CharT* s, size_type pos, size_type n);
5.2使用 string.rfind() 函数查找字符 ( 从后向前 )
5.2.1string.find(const CharT* s, size_type pos = 0);
5.2.2string.rfind(CharT c, size_type pos = npos);
5.2.3string.rfind(const basic_string& str,size_type pos = npos);
5.2.4string.rfind(const CharT* s, size_type pos, size_type n);
5.3.1string.find_first_of(const basic_string& str, size_type pos = 0);
5.3.2string.find_first_of(const CharT* s, size_type pos = 0);
5.3.3string.find_first_of(CharT c, size_type pos = 0);
5.3.4string.find_first_of(const CharT* s, size_type pos, size_type n);
5.4.1string.find_last_of(const basic_string& str,size_type pos = npos);
5.4.2string.find_last_of(const CharT* s, size_type pos = npos);
5.4.3string.find_last_of(CharT c, size_type pos = npos);
5.4.4string.find_last_of(const CharT* s, size_type pos, size_type n);
5.5.1string.find_first_not_of(const basic_string& str, size_type pos = 0);
5.5.2string.find_first_not_of(const CharT* s, size_type pos = 0);
5.5.3string.find_first_not_of(CharT c, size_type pos = 0);
5.5.4string.find_first_not_of(const CharT* s, size_type pos, size_type n);
5.6.1string.find_last_not_of(const basic_string& str, size_type pos = 0);
5.6.2string.find_last_not_of(const CharT* s, size_type pos = 0);
5.6.3string.find_last_not_of(CharT c, size_type pos = 0);
5.6.4string.find_last_not_of(const CharT* s, size_type pos, size_type n);
6.1.1string.insert(size_type pos, const basic_string& str);
6.1.2string.insert(size_type pos1, const basic_string& str,size_type pos2, size_type n = npos);
6.1.3string.insert(size_type pos, const CharT* s, size_type n);
6.1.4string.insert(size_type pos, const CharT* s);
6.1.5string.insert(size_type pos, size_type n, CharT c);
6.1.6string.insert(const_iterator p, CharT c);
6.1.7string.insert(const_iterator p, size_type n, CharT c);
6.1.8string.insert(const_iterator p, InputIt first, InputIt last);
6.1.9string.insert(const_iterator p, initializer_list);
6.2.1string.erase(size_type pos = 0, size_type n = npos);
6.2.2string.erase(const_iterator p);
6.2.3string.erase(const_iterator first, const_iterator last);
7.1.1getline(cin, const basic_string& str);
7.1.2getline(cin, const basic_string& str, char c);
7.3.1stiring.swap(basic_string& str)
——————————————————分割线————————————————————
引用:
首先是我主要借鉴的部分,同样,基本格式和分类都大同小异
接下来是我查询头文件的网址
标准库头文件 <string> - cppreference.com
还有就是关于一些函数的具体使用方法的借鉴
c++中getline的用法_IT_xiaolaoshu的博客-优快云博客_c++ getlinec++initializer_list详解_co小东东的博客-优快云博客_c++ initializer_list
——————————————————分隔符——————————————————————
大纲:
我将关于 sting 对象的处理方式主要分为了7个部分:
1、sting类构建--定义
2、具体对于字符串的操作函数
3、string字符串的比较
4、获取子串以及访问字符串元素
5、string对象的查找操作
6、string 对象的插入和删除
7、string 对象的一些其他操作
————————————————————分隔符————————————————————
1、sting类构建--定义
1.1使用string() 函数直接定义值
函数为:string(const basic_string& str);
#include <iostream>
#include <string>
using namespace std;
int main (){
string str ("Hello world!");//直接定义
cout << str << endl;
return 0;
}
C++方式的初始化定义。
1.2使用string(size_type n,char c) 定义 n 个 char 的字符串
#include <iostream>
#include <string>
using namespace std;
int main (){
string da (10, 'a');//定义da数据为10个 'a'
cout << da << endl;
return 0;
}
定义 n 个 char 字符到字符串内
1.3使用string(const string &str)定义为其他的 sting
#include <iostream>
#include <string>
using namespace std;
int main (){
string str ("Hello world!");
string out (str);//定义 out 为 str
cout << out << endl;
return 0;
}
定义一个新的字符串来存储其他字符串
1.4创建一个空的字符串
#include <iostream>
#include <string>
using namespace std;
int main (){
string out2;
cout << out2 << endl;
return 0;
}
定义空字符串
1.5使用链表类型定义字符串
#include <iostream>
#include <string>
using namespace std;
int main (){
string sa ({"dafdafd"});//链表类型----C++11之后才有的一种类型
cout << sa << endl;
return 0;
}
定义一个字符串通过链表方式-----具体使用方式见
c++initializer_list详解_co小东东的博客-优快云博客_c++ initializer_list
库函数关于 链表变量的表示方式是: " initializer_list<CharT> "若在其他地方见到这种类型就是链表变量。
1.6.使用C语言的方式定义
#include <iostream>
#include <string>
using namespace std;
int main (){
string ou = "Hello 世界!!";
cout << ou << endl;
return 0;
}
这里就是C语言的定义方式,其实 sting 就是一个没有具体空间的char 类型数组,当然如果你先学的C,大部分规则都能够应用到C++来。
当然,上述的定义方式可能还不太全面,但是已经满足大部分日常使用了,当然你更好的方式也可以在评论区说出来,一起分享给大家共同进步,抛砖引玉吗。
————————————————————分隔符————————————————————
2.具体对于字符串的操作函数
2.1获取字符串长度
2.1.1 使用 stirng.size() 函数获取string对象长度 --->>为了兼容STL容器而引进的
#include <iostream>
#include <string>
using namespace std;
int main (){
string a("abcdefg");
cout << a.size() << endl;
return 0;
}
这是C++的获取字符长度代码。
2.1.2使用 stirng.lenght() 函数获取string对象长度 --->>C语言习惯保留
#include <iostream>
#include <string>
using namespace std;
int main (){
string a("abcdefg");
cout << a.length() << endl;
return 0;
}
这个方法与上一个没有任何差别,这个只是对于C语言习惯的一个保存
2.1.3获取string对象最大长度——string.max_size();
#include <iostream>
#include <string>
using namespace std;
int main (){
string a("abcdefg");
cout << a.max_size() << endl;
return 0;
}
这个语句可以输出 string 对象的最大空间,在实际应用中可能使用频率不太高,但是在一些特殊情况下还是很有用的。
2.2复制字符串
2.2.1使用赋值字符进行复制
#include <iostream>
#include <string>
using namespace std;
int main (){
string da("玩什么玩,还不好好学习"), a1;
a1 = da;
cout << "源字符串为:" << da << "\n目标字符串为:" << a1 << endl;
return 0;
}
当然复制的方式很多,在C语言中可以使用strcpy、strncpy 函数来实现,同时还可以使用 1.3 的初始化时就赋给其其他的字符串,同样可以实现复制操作。
2.3字符串拼接和附加
2.3.1使用 ' + ' 操作符进行拼接字符串
#include <iostream>
#include <string>
using namespace std;
int main (){
string aone("我爱西瓜"), atwo("大强好棒");
string athree;
athree = aone + atwo;
cout << "\n第一个字符串为:" << aone << "\n第二个字符串为:" << atwo;
cout << "\n合并后的字符串为:" << athree << endl;
return 0;
}
这里使用了三个变量,好处是,对于合并后的字符串更改不会影响两个原始字符串,但是也需要占用一个新的内存空间。
2.3.2使用 ' += ' 操作符在字符串后进行拼接
#include <iostream>
#include <string>
using namespace std;
int main (){
string one("once in "),two("a lift time");
cout << "\n第一个字符串为:" << one << "\n第二个字符串为:" << two;
one += two;
cout << "\n合并后的字符串为:" << one << endl;
return 0;
}
使用 ‘+=’ 符号来进行拼接,拼接后前一个字符串就会更改。使用时请具体考虑
2.3.3使用string.append() 函数 添加字符
接下来就是我为什么想要分享的原因了,像 append 函数这样的有很多,函数后面有着各种不同的变量,在库函数中,没有关于它的具体解释,但是只要你弄懂了这些函数的变量,那么你都可以调用函数,输入变量,推导出它的指令意思了。
关于 append 函数有着 7 种的格式,接下来就是一个一个的解析了,如有不足望大佬指教。
append 函数的有两个模板,其余的都是在这个上面的展开。
它们分别是:
append(const T& t);
append(const T& t, size_type pos, size_type n = npos);
下面就是基于这两个模板的具体实现了。
1、string.append(const basic_string& str);————在stirng字符串后面添加其他string对象
#include <iostream>
#include <string>
using namespace std;
int main (){
string b1 ("abandon "), b2 ("never give up");
cout << "\nformerly字符串b1为:\n" << b1 << "\nformely字符串b2为:\n" << b2 << endl;
b1.append(b2);//1例
cout << "b1 插入 b2 后的数据为:\n" << b1;
return 0;
}
将 b2 的数据插入到 b1 的后面。
2.string.append(const CharT* s);————在string字符串后面添加自定义字符串
#include <iostream>
#include <string>
using namespace std;
int main (){
string b1 ("abandon ");
cout << "\nformerly字符串b1为:\n" << b1 << endl;
b1.append("d81");
cout << "b1 插入数据后 为:\n" << b1;
return 0;
}
在 b1 字符串的后面加入一段字符“ d81 ”
3.string.append(const CharT* s, size_type n);————在string字符串后面添加 自定义字符串 的前 n 个字符
#include <iostream>
#include <string>
using namespace std;
int main (){
string b1 ("abandon ");
cout << "\nformerly字符串b1为:\n" << b1 << endl;
b1.append("12345", 3);//添加3个空格
cout << "b1 插入数据后为:\n" << b1;
return 0;
}
在b1字符串后插入 字符串 “12345” 的前3个字符。
4.string.append(size_type n, CharT c);————在string字符串后面添加 n 个, 字符
#include <iostream>
#include <string>
using namespace std;
int main (){
string b2 ("never give up");
cout << "\nformerly字符串b2为:\n" << b2 << endl;
b2.append(3, 'w');//在后面添加 3 个 w
cout << "b2 插入数据后为:\n" << b2;
return 0;
}
在 b2 字符串后插入 3 个 ‘ w ’ 字符
5.string.append(const basic_string& __str, size_type __pos, size_type __n);————在string字符串后面添加这个字符串 从第 pos 开始的 n 个数据
#include <iostream>
#include <string>
using namespace std;
int main (){
string b1 ("abandon "), b2 ("never give up");
cout << "\nformerly字符串b1为:\n" << b1 << "\nformely字符串b2为:\n" << b2 << endl;
b1.append(b2,5, 5);//添加b2中的第 5个开始的 5个字符串
cout << "b1 插入数据后为:\n" << b1;
return 0;
}
在字符串 b1 后插入 字符串 b2 第5个位置开始的 5 个字符串 " give"
6.string.append(InputIt first, InputIt last);————在string字符串后添加迭代器first和last表示的字符序列。
#include <iostream>
#include <string>
using namespace std;
int main (){
string b2 ("never give up");
cout << "\nformely字符串b2为:\n" << b2 << endl;
b2.append(b2.begin()+8,b2.begin()+12);//在后面添加 b2.begin() + 8 到 b2.end() + 12的字符 --->>可用作复制
cout << "b2 插入数据后为:\n" << b2;
return 0;
}
C++中的迭代器就如同C语言的指针,Inputlt first 为 b2 开始后的第8个字符的位置,Inputlt last 为b2 开始后的第12个字符的位置。在b2 后添加 b2[8] 到 b2[12]的字符
7.string.append(initializer_list<CharT>);————在 string 对象后添加 链表
#include <iostream>
#include <string>
using namespace std;
int main (){
string b1 ("abandon ");
cout << "b1 的字符为:\n" << b1;
b1.append({"this is a list"});
cout << "\nb1添加链表后的字符为:\n" << b1 << endl;
return 0;
}
在字符 b1 后添加链表变量。(见1.5)
2.4使用string.push_back() 函数 添加字符
#include <iostream>
#include <string>
using namespace std;
int main (){
string b3("我要一个");
cout << "\nformerly字符串b1为:" << b3;
b3.push_back('V');
cout << "\nnow字符串b1为:" << b3;
return 0;
}
push是入栈的意义,但是只能入栈一个 字符;同样也可以使用 pop_back()删除一个字符。
3.string字符串的比较
3.1使用关系运算符进行比较
#include <iostream>
#include <string>
using namespace std;
int main (){
string a1("helmo world"), a2("hello world");
if(a1 == a2) cout << a1 << " == " << a2 <<endl;
else if(a1 > a2) cout << a1 << " > " << a2 <<endl;
else if(a1 < a2) cout << a1 << " < " << a2 <<endl;
return 0;
}
由于C++中将string对象声明为了简单变量,所以对于字符串的比较操作就很简单了,可以直接使用关系运算符进行操作(==、!=、<、<=、>、>=),具体比较是按照ASCII码的大小进行比较的。
3.2使用string.compare()函数进行比较
compare()函数有6种具体的使用方式,三种模板。
利用compare函数比较后会有一个返回值。
若返回 0 :代表相等
若返回 1 :代表string 字符大于 ( ) 内的字符,或 string 字符 比 ( ) 内的字符长
若返回 -1 : 代表 stirng 字符小于 ( ) 内的字符,或 string 字符 比 ( ) 内的字符短
模板如下:
compare(const T& t);
compare(size_type pos1, size_type n1, const T& t);
compare(size_type pos1, size_type n1, const T& t,size_type pos2, size_type n2 = npos);
1.stirng.compare(const basic_string& str);
————比较 string 与 string函数
#include <iostream>
#include <string>
using namespace std;
int main (){
string b1("taael1"), b2("tabel1");
if(!b1.compare(b2)) cout << b1 << " == " << b2 <<endl;// 1例
else if(b1.compare(b2) == 1) cout << b1 << " > " << b2 <<endl;
else if(b1.compare(b2) == -1)cout << b1 << " < " << b2 <<endl;
return 0;
}
此处比较 b1 和 b2 的字符,可以看出 b1 的第三个字符 小于 b2 的字符,所以 输出 b1 < b2
2.string.compare(const CharT* s) const;
————比较 string 与 字符串
#include <iostream>
#include <string>
using namespace std;
int main (){
string b1("taael1"), b2("tabel1");
if(b1.compare("tabel2") < 0) cout << b1 << " < " << "tabel2" <<endl;//2例
return 0;
}
这里是 和 “ tabel2 ” 进行比较当然你可以换成其他的字符串
3.string.compare(size_type pos1, size_type n1, const basic_string& str);
————比较 string 与 string函数
#include <iostream>
#include <string>
using namespace std;
int main (){
string c1("name dodowhale"),c3("name ");
if(c1.compare(0, 5, c3) == 0) cout << "\nc1 与 c3 前5个字符相等\n";
return 0;
}
这里是 c1的第 0 位开始的 5 个字符 和 c3 进行比较,当然,你也可以根据自己的需求自行调节
4.string.compare(size_type pos1, size_type n1, const CharT* s) ;
———— string 与 字符串比较
#include <iostream>
#include <string>
using namespace std;
int main (){
string c1("name dodowhale");
if(c1.compare(5, 9, "dodowhale") == 0) cout << "\n相等\n";
//if(c1.compare(c1.size() - 9, 9, "dodowhale") == 0) cout << "\n相等\n";//这句和上面的功能相似,可以用来开拓思路。
return 0;
}
这里是 c1 的第5位开始的9个字符和 “dodowhale” 字符串进行比较
5.string.compare(size_type pos1, size_type n1, const CharT* s, size_type n2);
————string 与 字符串比较(4改进)
#include <iostream>
#include <string>
using namespace std;
int main (){
string c1("name dodowhale");
if(c1.compare(5, 3, "dodow", 3) == 0) cout << "\n后面3个字符相等\n";
return 0;
}
c1第5位开始的3个字符 和 "dodow" 字符串的 前3个字符进行比较
6.string.compare(size_type pos1, size_type n1, const basic_string& str, size_type pos2, size_type n2 = npos);
————string 从 pos1 开始的 n1 个字符 与 string & str 从 pos2 开始 的n2个字符
#include <iostream>
#include <string>
using namespace std;
int main (){
string c1("name dodowhale"), c2("wehilr dodowhale");
if(c1.compare(5, 5, c2, 7, 5) == 0 ) cout << "\n他们后面5个字符相同\n";
return 0;
}
c1 从 5 开始的 5位数据 与 c2 从 7 开始 的 5 位数据进行比较
4.获取子串以及访问字符串元素
4.1、使用 substr函数 获取字串
string.substr(size_type pos = 0, size_type n = npos);
————复制 string对象 从 pos 开始的 n个字符
#include <iostream>
#include <string>
using namespace std;
int main (){
string a1("hello my honey"), a2;
cout << "\na1的数据为:" << a1 << endl;
a2 = a1.substr(6, 8);
cout << "\n复制的数据为:" << a2 << endl;
return 0;
}
将 a1 从 6 开始 的 8 个字符 复制到 a2
4.2、访问字符串元素
4.2.1使用数组下标访问
#include <iostream>
#include <string>
using namespace std;
int main (){
string ba("goodgoodstudy");
cout << "\nba数组的第二个数据是:" << ba[2] << endl;
return 0;
}
将string当作数组进行访问
4.2.2使用 at 函数访问
#include <iostream>
#include <string>
using namespace std;
int main (){
string bb("heloba");
cout << "\nbb字符串数组的第一个字符是:" << bb.at(0) << endl;
return 0;
}
5.string对象的查找操作
5.1使用 string.find() 函数查找字符 ( 从前向后 )
函数模板
string.find(const T& t, size_type pos = 0);
找到相同字符 or 字符串就输出,字符 or 字符串 当前位置。否则返回 -1
5.1.1string.find(const CharT* s, size_type pos = 0);
————在 string 中搜索 字符串 出现的位置,可以设定搜索位置 pos, 不设定默认为 0
#include <iostream>
#include <string>
using namespace std;
int main (){
string a1("oabandonnd");
int i = a1.find("nd");//搜索第一次出现的位置
int j = a1.find("nd", i+1);//在第一次之后搜索第二次出现的位置
if(i) cout << "nd在a1中的位置是:" << i << endl;
cout<< "n的在a1中第二次出现的位置是:" << j << endl;
return 0;
}
此处使用了两种方法:第一种是 不设定位置,使用默认位置。第二种自己设定位置
5.1.2string.find(const basic_string& str, size_type pos = 0);
————在 string 中搜索 string 出现的位置,可以设定搜索位置 pos, 不设定默认为 0
#include <iostream>
#include <string>
using namespace std;
int main (){
string a1("oabandonnd");
string a("d");
int k = a1.find(a);//例2
int l = a1.find( a , k + 1);//例2.1
if(k) cout << "n在a1中的位置是:" << k << endl;
if(l) cout<< "n在a1中第二次出现的位置是:" << l << endl;
return 0;
}
使用了两种方法:一种使用默认设定位置, 一种自己设定位置
5.1.3string.find(CharT c, size_type pos = 0);
————在 string 中搜索 字符 出现的位置,可以设定搜索位置 pos, 不设定默认为 0
#include <iostream>
#include <string>
using namespace std;
int main (){
string a1("oabandonnd");
int k1 = a1.find('o');//例3
int l2 = a1.find( 'o' , k1 + 1);//例3.1
if(k1>=0) cout << "n在a1中的位置是:" << k1 << endl;
if(l2) cout<< "n在a1中第二次出现的位置是:" << l2 << endl;
return 0;
}
使用了两种方法:一种使用默认设定位置, 一种自己设定位置
5.1.4string.find(const CharT* s, size_type pos, size_type n);
————在 string 中搜索 字符串 出现的位置 pos为stirng搜索开始位置, n为搜索的字符长度(不能大于字符串长度)
#include <iostream>
#include <string>
using namespace std;
int main (){
string ba("banabadbanaba");
int o = ba.find("aba", 4, 3);//例4
cout << "ba在ba中的位置是:" << o << endl;
return 0;
}
ba 的第4位开始的 3 个字符 与 字符串比较
5.2使用 string.rfind() 函数查找字符 ( 从后向前 )
函数模板
string.find(const T& t, size_type pos = 0);
找到相同字符 or 字符串就输出,字符 or 字符串 当前位置,否则返回 -1 ()
5.2.1string.find(const CharT* s, size_type pos = 0);
————在 string 中 ※倒序※ 搜索 字符串 出现的位置 ,可以设定开始搜索位置,默认为末尾
#include <iostream>
#include <string>
using namespace std;
int main (){
string fa("banadbnanad");
int i = fa.rfind("ad");
int j = fa.rfind("ad", i - 1);
cout << "\n字符串\"ad\" 出现在fa的位置是" << i ;//例1
cout << "\n字符串\"ad\" 出现在fa的位置是" << j;//例1.2
return 0;
}
使用了两种方法:一种使用默认设定位置, 一种自己设定位置
5.2.2string.rfind(CharT c, size_type pos = npos);
————在 string 中 ※倒序※ 搜索 字符 出现的位置 ,可以设定开始搜索位置,默认为末尾
#include <iostream>
#include <string>
using namespace std;
int main (){
string fa("banadbnanad");
int i = fa.rfind('d');
int j = fa.rfind('d', i - 1);
cout << "\n字符串\"d\" 出现在fa的位置是" << i;//例2
cout << "\n字符串\"d\" 出现在fa的位置是" << j;//例2.2
return 0;
}
使用了两种方法:一种使用默认设定位置, 一种自己设定位置
5.2.3string.rfind(const basic_string& str,size_type pos = npos);
————在 string 中 ※倒序※ 搜索 string 出现的位置 ,可以设定开始搜索位置,默认为末尾
#include <iostream>
#include <string>
using namespace std;
int main (){
string fa("banadbnanad"), fd("na");
int i = fa.rfind(fd);
int j = fa.rfind(fd,i - 1);
cout << "\nfd出现在fa的位置是" << i;//例3
cout << "\nfd出现在fa的位置是" << j;//例3.2
return 0;
}
使用了两种方法:一种使用默认设定位置, 一种自己设定位置
5.2.4string.rfind(const CharT* s, size_type pos, size_type n);
————在 string 中 ※倒序※ 搜索 字符串 出现的位置 pos为stirng搜索开始位置(倒序搜索), n为搜索的字符长度(不能大于字符串长度)
#include <iostream>
#include <string>
using namespace std;
int main (){
string ba("banabadbanaba");
cout << "\n\"ana\"在ba中的位置是:" << ba.rfind("ana", 6, 2) << endl;
return 0;
}
从 ba 的第 6 个位置开始倒叙 搜索 “ana” 的2个字符
5.3string.find_first_of()
函数模板
find_first_of(const T& t, size_type pos = 0);
在 stirng 中查找 字符 || 字符串 || string 中的字符,若出现了第一个字符,就返回当前的位置
★★★ find_first_of 函数最容易出错的地方是和find函数搞混。它最大的区别就是如果在一个字符串str1中查找另一个字符串str2,如果str1中含有str2中的任何字符,则就会查找成功。
5.3.1string.find_first_of(const basic_string& str, size_type pos = 0);
————在 string 中搜索 string 内字符第一次出现的位置 ,可以设定开始搜索位置
#include <iostream>
#include <string>
using namespace std;
int main (){
string ha("abljdjdjbafjuujdfdjlk"), hb("dj");
int i = ha.find_first_of(hb);
int j = ha.find_first_of(hb, i + 1);
cout << "\n字符串\"dj\" 出现在ha的位置是" << i;
cout << "\n\n字符串\"dj\" 第二次出现在ha的位置是" << j;
return 0;
}
i 是 ha 中搜索到的 hb 中任意一个字符的位置,j 是 ha 中搜索到的 hb 中任意一个字符的位置,在 i 之后。
5.3.2string.find_first_of(const CharT* s, size_type pos = 0);
————在 string 中搜索 字符串 内字符第一次出现的位置, 可以设定开始搜索的位置
#include <iostream>
#include <string>
using namespace std;
int main (){
string ha("abljdjdjbafjuujdfdjlk");
int i = ha.find_first_of("lj");
int j = ha.find_first_of("lj",i + 1);
cout << "\n字符串\"lj\" 出现在ha的位置是" << i;
cout << "\n字符串\"lj\" 第二次出现在ha的位置是" << j;
return 0;
}
i 是 ha 中搜索到的 “ lj ” 中任意一个字符的位置,j 是 ha 中搜索到的 “ lj ” 中任意一个字符的位置,在 i 之后。
5.3.3string.find_first_of(CharT c, size_type pos = 0);
————在 string 中搜索 字符 第一次出现的位置,可以设定开始位置
#include <iostream>
#include <string>
using namespace std;
int main (){
string ha("abljdjdjbafjuujdfdjlk");
int i = ha.find_first_of('a');
int j = ha.find_first_of('a', i + 1);
cout << "\n字符\'a\' 出现在ha的位置是" << i;
cout << "\n字符\'a\' 第二次出现在ha的位置是" << j;
return 0;
}
使用了两种方法:一种使用默认设定位置, 一种自己设定位置
5.3.4string.find_first_of(const CharT* s, size_type pos, size_type n);
————在 string 中搜索 字符串 内字符第一次出现的位置, 可以设定开始位置pos, 设定字符搜索个数 n
#include <iostream>
#include <string>
using namespace std;
int main (){
string ha("abljdjdjbafjuujdfdjlk");
int i = ha.find_first_of("lj", 3, 1);
cout << "\n\n字符串\"lj\" 出现在ha的位置是" << i;
return 0;
}
在 ha 开始的 第 3 个位置,搜索 “ lj ” 字符串中的 一个字符
5.4string.find_last_of()
函数模板
find_last_of(const T& t, size_type pos = npos);
从后向前 ※倒序※ 搜索 字符串 || 字符 || string 对象 在 string 内任何字符出现的最后一次地址
5.4.1string.find_last_of(const basic_string& str,size_type pos = npos);
————在 string 中 ※倒序※ 搜索 string 内任意字符最后一次出现的位置,并返回,可以设定搜索位置
#include <iostream>
#include <string>
using namespace std;
int main (){
string az("lduikduioiulkdu"), za("du");
int i = az.find_last_of(za);
int j = az.find_last_of(za, i - 5);
cout << "\n\nza 出现在 az的位置是" << i;
cout << "\n\nza 第二次出现在 az的位置是" << j;
return 0;
}
i 是 az 中 ※倒序※ 搜索 搜索到的 za中任意一个字符的位置,j 是 az 中搜索到的 za 中任意一个字符的位置,在 i 之后。
5.4.2string.find_last_of(const CharT* s, size_type pos = npos);
————在 string 中 ※倒序※ 搜索 字符串 内任意字符最后一次出现的位置,并返回,可以设定搜索位置
#include <iostream>
#include <string>
using namespace std;
int main (){
string az("lduikduioiulkdu");
int i = az.find_last_of("lk");
int j = az.find_last_of("lk", 7);
cout << "\n\n字符串\"lk\" 出现在 az的位置是" << i;
cout << "\n\n字符串\"lk\" 第二次出现在 az的位置是" << j;
return 0;
}
i 是 az 中 ※倒序※ 搜索 搜索到的 “ lk ” 中任意一个字符的位置,j 是 az 中搜索到的 “ lk ” 中任意一个字符的位置,在 7 之后。
5.4.3string.find_last_of(CharT c, size_type pos = npos);
————在 string 中 ※倒序※ 搜索 字符 最后一次出现的位置,并返回,可以设定搜索位置
#include <iostream>
#include <string>
using namespace std;
int main (){
string az("lduikdukioiulkdu");
int i = az.find_last_of('k');
int j = az.find_last_of('k',10);
cout << "\n\n字符\"k\" 出现在 az的位置是" << i;
cout << "\n\n字符\"k\" 第二次出现在 az的位置是" << j;
return 0;
}
使用了两种方法:一种使用默认设定位置, 一种自己设定位置
5.4.4string.find_last_of(const CharT* s, size_type pos, size_type n);
————在 string 中 ※倒序※ 搜索 字符串 内任意字符最后一次出现的位置,并返回,可以设定搜索开始位置 ,以及搜索字符串个数
#include <iostream>
#include <string>
using namespace std;
int main (){
string az("lduikdukioiulkdu");
int i = az.find_last_of("ldu",5,2);
cout << "\n\n字符串\" ldu\" 出现在 az 的位置是" << i;
return 0;
}
※倒序※ 搜索 在 az 开始的 第 5 个位置,搜索 “ ldu ” 字符串中的 两个字符
5.5string.find_first_not_of()
函数模板
find_first_not_of(const T& t, size_type pos = 0);
从前先后查找第一个不包括在string中的字符的位置, 可设定开始位置
5.5.1string.find_first_not_of(const basic_string& str, size_type pos = 0);
————查找 stinrg 对象中第一个 不包括在 string 中的字符的位置, 可设定开始位置
#include <iostream>
#include <string>
using namespace std;
int main (){
string jl("asda asad dadf s"), jk("asa");
int i = jl.find_first_not_of(jk);
int j = jl.find_first_not_of(jk, i + 1);
cout << "\n\njl 中不包括 jk 的位置是" << i;
cout << "\n\njl 中第二个不包括 jk 的位置是" << j;
return 0;
}
使用了两种方法:一种使用默认设定位置, 一种自己设定位置
5.5.2string.find_first_not_of(const CharT* s, size_type pos = 0);
————查找 字符串 中第一个 不包括在 string 中的字符的位置, 可设定开始位置
#include <iostream>
#include <string>
using namespace std;
int main (){
string jl("asda asad dadf s");
int i = jl.find_first_not_of("as");
int j = jl.find_first_not_of("as", i + 1);
cout << "\n\njl 中不包含 \"as\" 的位置是" << i;
cout << "\n\njl 第二次不包括 \"az\"的位置是" << j;
return 0;
}
使用了两种方法:一种使用默认设定位置, 一种自己设定位置
5.5.3string.find_first_not_of(CharT c, size_type pos = 0);
————查找 字符 中第一个 不包括在 string 中的字符的位置, 可设定开始位置
#include <iostream>
#include <string>
using namespace std;
int main (){
string jl("asda asad dadf s");
int i = jl.find_first_not_of("s");
int j = jl.find_first_not_of("s", i + 1);
cout << "\n\njl 中不包含字符\"s\"的位置是" << i;
cout << "\n\njl 中不包括字符\"s\"的位置是" << j;
return 0;
}
使用了两种方法:一种使用默认设定位置, 一种自己设定位置
5.5.4string.find_first_not_of(const CharT* s, size_type pos, size_type n);
————查找 字符串 对象中第一个 不包括在 string 中的字符的位置, 可设定开始位置, 以及检索个数
#include <iostream>
#include <string>
using namespace std;
int main (){
string jl("asda asad dadf s");
int i = jl.find_first_not_of("asda ", 4, 5);
cout << "\n\njl 第 4 个位置开始的不包含字符串\"asda\" 5个字符的位置是" << i;
return 0;
}
寻找 字符 jl 中的 第 4 个位置开始的 不包含 “asda ”字符串的 5个字符 位置
5.6string.find_last_not_of()
函数模板
find_last_not_of(const T& t, size_type pos = 0);
从前先后查找最后一个不包括在string中的字符的位置, 可设定开始位置
5.6.1string.find_last_not_of(const basic_string& str, size_type pos = 0);
———— ※倒序※ 查找 stinrg 对象中 最后一个 不包括在 string 中的字符的位置, 可设定开始位置
#include <iostream>
#include <string>
using namespace std;
int main (){
string ui("asda asad dadf s"), uo("asa");
int i = ui.find_last_not_of(uo);
int j = ui.find_last_not_of(uo, 3);
cout << "\n\nui 中不包括uo的位置是" << i;
cout << "\n\nui 中第二个不包括uo的位置是" << j;
return 0;
}
※倒序※ 寻找 字符串中 最后一个 不包括在 uo 中的字符的位置, 从ui的 3 个字符开始 寻找 字符串中 最后一个 不包括在 uo 中的字符的位置
5.6.2string.find_last_not_of(const CharT* s, size_type pos = 0);
———— ※倒序※ 查找 字符串 中最后一个 不包括在 string 中的字符的位置, 可设定开始位置
#include <iostream>
#include <string>
using namespace std;
int main (){
string ui("asda asad dadf s");
int i = ui.find_last_not_of("as");
int j = ui.find_last_not_of("as", 3);
cout << "\n\nui中不包括 \"as\"的位置是" << i;//例2
cout << "\n\nui中第二次不包括 \"as\"的位置是" << j;//例2.1
return 0;
}
※倒序※ 寻找 字符串中 最后一个 不包括在 “as”中的字符的位置, 从ui的 3 个字符开始 寻找 “as”中 最后一个 不包括在 uo 中的字符的位置
5.6.3string.find_last_not_of(CharT c, size_type pos = 0);
———— ※倒序※ 查找 字符 中最后一个 不包括在 string 中的字符的位置, 可设定开始位置
#include <iostream>
#include <string>
using namespace std;
int main (){
string ui("asda asad dadf s");
int i = ui.find_last_not_of("s");
int j = ui.find_last_not_of("s", 4);
cout << "\n\nui 中不包含字符\"s\" 中的位置是" << i;
cout << "\n\nui 中第二次不包含字符\"s\" 中的位置是" << j;
return 0;
}
使用了两种方法:一种使用默认设定位置, 一种自己设定位置
5.6.4string.find_last_not_of(const CharT* s, size_type pos, size_type n);
———— ※倒序※ 查找 字符串 对象中最后一个 不包括在 string 中的字符的位置, 可设定开始位置, 以及检索个数
#include <iostream>
#include <string>
using namespace std;
int main (){
string ui("asda asad dadf s");
int i = ui.find_last_not_of("asda ", 17, 2);
cout << "\n\n字符串中 第17个位置 不包含\"asda \" 中的 2 个字符的位置是" << i;//例4.1
return 0;
}
※倒序※ 搜索 在 ui 开始的 第 17 个位置,搜索 “asda ” 字符串中的 两个字符
6.string 对象的插入和删除
6.1. stirng.insert() 函数 对象的插入
函数模板(1)
insert(size_type pos, const T& t);
在 string 对象 的 pos 位置 插入 字符串 || 字符 || string 对象
函数模板(2)
insert(size_type pos1, const T& t, size_type pos2, size_type n = npos);
在 string 对象的 pos1 位置处 插入 字符串 || 字符 || string 对象从 pos2 处开始的 npos 个字符
6.1.1string.insert(size_type pos, const basic_string& str);
————在 string 对象 pos 位置处插入 string 对象 str
#include <iostream>
#include <string>
using namespace std;
int main (){
string ao("abcd efgh ijkl"),bo("java");
cout << "\n\n将 bo 插入到 ao 的第五个字符后\n" << ao.insert(5, bo);
return 0;
}
ao插入前: abcd efgh ijkl
ao插入后: abcd javaefgh ijkl
6.1.2string.insert(size_type pos1, const basic_string& str,size_type pos2, size_type n = npos);
————在 string 对象 pos1 位置处插入 string 对象从 pos2 处开始的 npos 个字符
#include <iostream>
#include <string>
using namespace std;
int main (){
string ao("abcd efgh ijkl"),bo("java");
cout << "\n\n在 ao 的第13个字符后插入 bo 从 1 开始的 3 个字符\n" << ao.insert(13, bo, 1, 3);
return 0;
}
ao插入前: abcd efgh ijkl
ao插入后: abcd efgh ijkaval
6.1.3string.insert(size_type pos, const CharT* s, size_type n);
————在 string 对象 pos 位置处插入 字符串 对象的前 n 个字符
#include <iostream>
#include <string>
using namespace std;
int main (){
string ao("abcd efgh ijkl"),bo("java");
cout << "\n\n在 ao 的第五个字符后插入 “髑”\n" << ao.insert(5, "髑\t");
return 0;
}
ao插入前: abcd efgh ijkl
ao插入后:abcd 髑 efgh ijkl
6.1.4string.insert(size_type pos, const CharT* s);
————在 string 对象 pos 位置处插入 字符串
#include <iostream>
#include <string>
using namespace std;
int main (){
string ao("abcd efgh ijkl"),bo("java");
cout << "\n\n在 ao 的第13个字符后插入 \"myself\" 从 1 开始的 3 个字符\n" << ao.insert(13, "myself", 1, 3);
return 0;
}
ao插入前: abcd efgh ijkl
ao插入后: abcd efgh ijkysel
6.1.5string.insert(size_type pos, size_type n, CharT c);
————在 string 对象 p 位置处插入 n 个 字符 c
#include <iostream>
#include <string>
using namespace std;
int main (){
string ao("abcd efgh ijkl"),bo("java");
cout << "\n\n在 ao 的第五个字符后插入 \' V\'\n" << ao.insert(0, 3,'V');
return 0;
}
ao插入前: abcd efgh ijkl
ao插入后: VVVabcd efgh ijkl
6.1.6string.insert(const_iterator p, CharT c);
————在 string 对象 p 位置处插入字符 c 并返回插入后常量迭代器的位置
#include <iostream>
#include <string>
using namespace std;
int main (){
string ao = ("abcd efgh ijkl");
string::iterator it;//定义一个迭代器————————类似指针
it=ao.begin()+9;//调整迭代器指向
ao.insert (it, 'V');//---------无法直接进行输出,只能先操作,在输出
cout << "\n\n在 ao 的第9个字符后插入 \' V\'\n" << ao;
return 0;
}
ao插入前: abcd efgh ijkl
ao插入后: abcd efghV ijkl
6.1.7string.insert(const_iterator p, size_type n, CharT c);
————在 string 对象 p 位置处插入 n 个 字符 c 并返回插入常量后迭代器的位置
#include <iostream>
#include <string>
using namespace std;
int main (){
string ao = ("abcd efgh ijkl");
string::iterator it;//定义一个迭代器————————类似指针
it = ao.begin() + 2;//调整迭代器指向
ao.insert(it, 2, 'H');
cout << "\n\n在 ao 的第2个字符后插入两个 \'H\'\n" << ao;
return 0;
}
ao插入前: abcd efgh ijkl
ao插入后: abHHcd efgh ijkl
6.1.8string.insert(const_iterator p, InputIt first, InputIt last);
————在 string 对象 p 位置添加以常量迭代器first和last表示的字符序列。 案例s1.append(s2.begin()+4,s2.end());
#include <iostream>
#include <string>
using namespace std;
int main (){
string ao = ("abcd efgh ijkl");
string::iterator it;//定义一个迭代器————————类似指针
string VG("ZXCVBNM");
it=ao.end() - 2;//调整迭代器指向
ao.insert(it, VG.begin()+2, VG.end());
cout << "\n在 ao 的倒数2个字符后插入VG开始 的第二个位置到结束位置的字符 \n" << ao;
return 0;
}
ao插入前: abcd efgh ijkl
ao插入后: abcd efgh ijCVBNMkl
6.1.9string.insert(const_iterator p, initializer_list<CharT>);
————在 string 对象 常量迭代器p 位置处添加 链表格式字符
#include <iostream>
#include <string>
using namespace std;
int main (){
string ao = ("abcd efgh ijkl");
cout << "\n\n在 ao 的第2个字符后插入链表字符 \n" << ao.insert(2,{"好好好"});
return 0;
}
ao插入前: abcd efgh ijkl
ao插入后: ab好好好cd efgh ijkl
6.2.string.erase()
元素删除函数
6.2.1string.erase(size_type pos = 0, size_type n = npos);
————对 string 对象 从 pos 位置 删除 n 个字符
#include <iostream>
#include <string>
using namespace std;
int main (){
string uy("QWERTYUIOP");
cout << "uy原来的数据为:" << uy << endl;
cout << "\n在 1 位置 删除 4 个字符后的 uy 为:\n" << uy.erase(1, 4);
cout << "\n删除 第 4 个后的所有字符后为:\n" << uy.erase(4);
return 0;
}
删除前: QWERTYUIOP
1 位置删除 4 个字符: QYUIOP
删除 4 位置后的所有字符: QYUI
6.2.2string.erase(const_iterator p);
————对 string 对象 删除迭代器所指位置的第一个字符
#include <iostream>
#include <string>
using namespace std;
int main (){
string uy = ("QWERTYUIOP");
cout << "uy原来的数据为:\n" << uy << endl;
string::iterator ob = uy.begin() + 2;;//定义一个迭代器
uy.erase(ob);
cout << "\n在 迭代器 ob 位置 删除 1 个字符后的 uy 为:\n" << uy;
return 0;
}
删除前: QWERTYUIOP
删除后: QWRTYUIOP
6.2.3string.erase(const_iterator first, const_iterator last);
————对 stirng 对象 删除迭代器所指 first 位置 到 迭代器所指的 last 位置的 字符
#include <iostream>
#include <string>
using namespace std;
int main (){
string uy = ("QWERTYUIOP");
cout << "uy原来的数据为:\n" << uy << endl;
uy.erase(uy.begin() + 2, uy.end() - 2);
cout << "\nstring 对象 uy 删除 迭代器 first ~ last 的字符后为:\n" << uy;
return 0;
}
删除前: QWERTYUIOP
删除后: QWOP
7.string 对象的一些其他操作
7.1string.getline()
这个函数在 string 库中只有两种形式
getline(basic_istream<CharT, Traits>&& is, basic_string<CharT, Traits, Allocator>& str, CharT delim);
getline(basic_istream<CharT, Traits>& is,basic_string<CharT, Traits, Allocator>& str);
但是关于其具体用法还是不了解,但是找到了两个用法
7.1.1getline(cin, const basic_string& str);
————读取输入的数据到 string
#include <iostream>
#include <string>
using namespace std;
int main (){
string da;//定义 string 对象
getline(cin, da);//调用 函数
cout << "输入的数据是:" << da << endl;
return 0;
}
输入: hello world
输出: 输入的数据是:hello world
7.1.2getline(cin, const basic_string& str, char c);
————读取输入的字符串到 string ,当读取到 char c 字符就结束
#include <iostream>
#include <string>
using namespace std;
int main (){
string ov;
getline(cin, ov , 'h');//读取输入的字符串,当读取到 ' h ' 后就结束,丢弃后面的字符
cout << "输入的数据是:" << ov << endl;
return 0;
}
输入:jake hello
输出:输入的数据是:jake
7.2empty()
函数判断字符串是否为空
7.2.1string.empty();
————若字符串为空,就返回 1 否则就返回 0——————————该函数较为特殊,若有值就返回 0 , 无值返回 1
#include <iostream>
#include <string>
using namespace std;
int main (){
string dd,dj("我有东西");
cout << dd.empty() << endl;
if((dd.empty())) cout << "dd字符串为空\n" ;
else cout << "dd字符串有数据\n" ;
cout << dj.empty() << endl;
if((dj.empty())) cout << "dj字符串为空\n" ;
else cout << "dj字符串有数据\n" ;
return 0;
}
输出:
1
dd字符串为空
0
dj字符串有数据
7.3swap()
字串交换函数
7.3.1stiring.swap(basic_string& str)
————将 两个函数交换
#include <iostream>
#include <string>
using namespace std;
int main (){
string jk("日本少女水手服"), coat("防寒大棉衣");
cout << "befroe jk date was " << jk << endl;
cout << "before coat date was " << coat << endl;
jk.swap(coat);
cout << "\nnow jk date is " << jk << endl;
cout << "now coat date is " << coat << endl;
return 0;
}
输出:
befroe jk date was 日本少女水手服
before coat date was 防寒大棉衣
now jk date is 防寒大棉衣
now coat date is 日本少女水手服
————————————————————分隔符————————————————————
后记:
到此基本上就是全部的内容了,自己可能也只是写了一部分,如果各位看官还有补充的,可以在评论区写下相关函数,我会去查询相关的库然后添加进去的。
自己整理出来用了3天,但是写出这篇博客,断断续续都一周多了,主要还是不知道怎么将这些函数讲解清楚,所以每条函数都有实例,如果我表达不清晰,含糊的地方。你可以跑跑代码,这些代码都是可以运行的。而且有些代码查了库函数还是不能理解,如果有了解的大佬,请指教。这是由C到C++的学习过程中的终结,以后可能还会由些经验分享,大家一起进步吧!!!
加油,乾坤未定,你我皆是牛马U•ェ•*U