wordpress外贸站,建立网站 杭州,企业微网站哪家好,帮朋友做网站不给钱一、Modifiers#xff08;修饰符#xff09;#xff1a;
1、operator
这个成员函数给一个string类类型的对象进行追加#xff0c;在现有的string后面追加string类、字符串或者字符#xff1b; 代码示例#xff1a;
void test1()
{std::string s1(Hello );…一、Modifiers修饰符
1、operator
这个成员函数给一个string类类型的对象进行追加在现有的string后面追加string类、字符串或者字符 代码示例
void test1()
{std::string s1(Hello );std::cout s1 std::endl;//operator 追加s1 world;s1 !;std::cout s1 std::endl;std::string s2(Welcome!);s1 s2;std::cout s1 std::endl;
} 2、append追加
这个成员函数也是在string后面追加
1追加string类2追加一个string类的一部分从下标subpos开始到sublen个字符结束3追加一个字符串4追加一个字符串的前n个字符5追加n个字符c6追加一个string类的一部分利用迭代器指明起始位置。
代码示例
void test2()
{std::string s1(Who );std::string s2(are *******);const char* s3 you;s1.append(s2, 0, 4);//4表示追加4个字符s1.append(s3);s1.append(1,?);std::cout s1 std::endl;
} 3、push_back
给string类类型的字符串追加一个字符
代码示例
void test3()
{std::string s1(Good );char c1 L;char c2 u;char c3 c;char c4 k;char c5 !;s1.push_back(c1);std::cout s1 std::endl;s1.push_back(c2);std::cout s1 std::endl;s1.push_back(c3);std::cout s1 std::endl;s1.push_back(c4);std::cout s1 std::endl;s1.push_back(c5);std::cout s1 std::endl;} 4、assign 将内容分配给字符串 代码示例
test(4)
{std::string str;std::string base The quick brown fox jumps over a lazy dog.;str.assign(base);std::cout str \n;str.assign(base, 10, 9);//从第10个位置开始向后扫描9个字符assign进去std::cout str \n;str.assign(programs are cool, 7);//字符串的前7个std::cout str \n;str.assign(c-string);std::cout str \n;str.assign(10, *);std::cout str \n;str.assign(base.begin() 16, base.end() - 12);std::cout str \n;
} 5、insert
在指定的下标位置之前插入字符串、字符或者string类 pos代表指定的下标位置
代码示例
void test5()
{std::string str Wrold;std::string s1 Hello ;str.insert(0, s1);std::cout str \n;str.insert(str.size(), 1, !);std::cout str \n;
} 6、erase
删除string类对象里面的一部分字符 1默认从第一个字符开始默认删除整个string类对象。传参控制删除的区间2删除从p开始之后的内容3利用迭代器指定区间左闭右开删除
代码示例
void test6()
{std::string str(There is nothing.);std::cout str \n;str.erase(9);//从下标为9的字符删除完std::cout str std::endl;str.erase(str.begin(), str.end() - 3);std::cout str \n;str.erase(str.begin() 1);std::cout str \n;
} 7、replace
对于一个string类对象指定它某一个字符下标为起始位置扫描len个长度的字符替换成给出的字符串、字符、string类 迭代器指定的区域左闭右开
代码示例
void test7()
{std::string str(xxxxx_yyyyy);str.replace(0, 3, y);std::cout str std::endl;str.replace(str.begin() 4, str.end(), x);std::cout str std::endl;
} 8、swap
交换两个string类对象
代码示例
void test8()
{std::string s1(xxx);std::string s2(***);s1.swap(s2);std::cout s1 \n s2 std::endl;
} 9、pop_back
尾删一个string类对象的一个字符
代码示例
{std::string str(ABCDEFG);str.pop_back();str.pop_back();std::cout str std::endl;
} 二、string类对象操作
1、c_str
用来接收一个string的对象指针这个指针指向字符串开头的位置返回一个const char* 的指针
代码示例
void test1()
{string str(This is an apple);const char* cstr str.c_str();cout cstr endl;cout str endl;
} 2、data
与c_str用法类似
3、copy
拷贝一个string类对象的一个子串给一个由s指向的字符串从pos开始拷贝拷贝len个字符若是stirng不够拷贝那就拷贝整个stirng类对象
代码示例
//copy
void test2()
{string str(everything is nice);char buffer[20];size_t _length str.copy(buffer,10,0);buffer[_length] \0;cout buffer endl;
} 4、find
从一个string类对象里面找字符串、字符或者string对象找到了返回开始匹配的下标没有找到返回string::npos
3意为从string的pos1下标开始找找与s里面前n个开始匹配的位置
代码示例
//find
void test3()
{string str(Hello World!);size_t pos1 str.find(!, 0);size_t pos2 str.find(Hello, 0);string str2(World);size_t pos3 str.find(str2, 0);cout pos1 pos2 pos3 endl;size_t pos4 str.find( World, 0, 3);cout pos4 endl;
} 5、refind
和find原理相同只是从最后倒着开始找
6、find_first_of 和find_last_of
find_first_of 意为按正向顺序查早查找string类中与给出的查找对象中任意字符匹配的下标
代码示例
//find_first_of
void test4()
{string str(Please, replace the vowels in this sentence by asterisks.);size_t pos str.find_first_of(aeiou);//从下标为0处开始在str中找与aeiou中任意字符匹配的下标while (pos! string::npos){str[pos] *;pos str.find_first_of(aeiou, pos 1);//指明开始查找的位置以免重复查找已查找的位置}//最后str里面的aeiou全部被替换成*cout str endl;
} find_last_of和 find_first_of原理相同只是从后面开始查找起
7、find_first_not_of 和find_last_not_of
查找string类中不为给出查找对象中任意字符的位置
代码示例
void test5()
{string str(Please, replace the vowels in this sentence by asterisks.);size_t pos str.find_first_not_of(aeiou);while (pos ! string::npos){str[pos] *;pos str.find_first_not_of(aeiou, pos 1);}cout str endl;
} find_last_not_of从后面开始查找
8、substr
从一个string对象中拷贝一个子串从pos开始拷贝拷贝len个字符返回string类的子串
代码示例
void test6()
{string str(What a nice day!);string _substr str.substr(0, 10);cout _substr endl;
} 三、string类模板中的非成员函数
成员函数默认第一个参数是this指针传参时也不能改变而有些函数的传参第一个实参不一定是this指针指向的对象因此要将这些函数定义在类外方便传参
1、operator
此函数可以将string、字符或者字符串之间相加
代码实现
//operator
void test7()
{string str www;const char* s1 cplusplus;char i .;string s2 com;string rsl;rsl str i s1 i s2;cout rsl endl;
} 2、relational operators
此函数中定义了string对象之间的比较
代码示例
//relational operators
void test8()
{// string comparisonsstd::string foo alpha;std::string bar beta;if (foo bar) std::cout foo and bar are equal\n;if (foo ! bar) std::cout foo and bar are not equal\n;if (foo bar) std::cout foo is less than bar\n;if (foo bar) std::cout foo is greater than bar\n;if (foo bar) std::cout foo is less than or equal to bar\n;if (foo bar) std::cout foo is greater than or equal to bar\n;
} 3、swap
交换两个string类对象
4、operator 和 operator
重载输出和输入函数
第一个参数是cout或者cin这也符合代码标准输入输出流的书写
代码示例
//operator 、operator
void test9()
{string str;cin str;cout str;cout endl;string str2 ancd;cout str2;
} 5、getline
此函数是输入string的对于一般的输入cin来说碰见空格就说明输入完成但是有些情况要把空格以及空格之后的字符也算成这个string类
一般来说也就是2碰见\n也就是换行就会终止若是在第三个参数指明了终止的字符那么就一直读取直到碰见这个指定的字符才结束
getline不会读取结束标志的字符而是将它作为\0读取进入string
代码示例 string str1;cout 输入一个字符串;//输入一个含空字符的字符串cin只能读取空字符前面的内容cin str1;cout str1 endl; 只能读取空字符前面的内容 cout 输入一个字符串;string str2;getline(cin, str2);//默认碰见\n结束cout str2; getline遇到换行符结束 cout 输入一个字符串;string str3;getline(cin, str3, Z);//指定碰见Z结束cout **********************;cout str3;碰见Z才结束读取换行符也读 文章转载自: http://www.morning.bqrd.cn.gov.cn.bqrd.cn http://www.morning.kqlrl.cn.gov.cn.kqlrl.cn http://www.morning.cptzd.cn.gov.cn.cptzd.cn http://www.morning.qxlxs.cn.gov.cn.qxlxs.cn http://www.morning.pbgnx.cn.gov.cn.pbgnx.cn http://www.morning.leeong.com.gov.cn.leeong.com http://www.morning.trbxt.cn.gov.cn.trbxt.cn http://www.morning.qmpbs.cn.gov.cn.qmpbs.cn http://www.morning.rgdcf.cn.gov.cn.rgdcf.cn http://www.morning.rhdqz.cn.gov.cn.rhdqz.cn http://www.morning.jcxgr.cn.gov.cn.jcxgr.cn http://www.morning.wpspf.cn.gov.cn.wpspf.cn http://www.morning.nqfxq.cn.gov.cn.nqfxq.cn http://www.morning.lgznf.cn.gov.cn.lgznf.cn http://www.morning.jzxqj.cn.gov.cn.jzxqj.cn http://www.morning.bhmnp.cn.gov.cn.bhmnp.cn http://www.morning.kztpn.cn.gov.cn.kztpn.cn http://www.morning.mpmtz.cn.gov.cn.mpmtz.cn http://www.morning.jxtbr.cn.gov.cn.jxtbr.cn http://www.morning.xzqzd.cn.gov.cn.xzqzd.cn http://www.morning.yltnl.cn.gov.cn.yltnl.cn http://www.morning.xhrws.cn.gov.cn.xhrws.cn http://www.morning.dtfgr.cn.gov.cn.dtfgr.cn http://www.morning.rpwht.cn.gov.cn.rpwht.cn http://www.morning.wkrkb.cn.gov.cn.wkrkb.cn http://www.morning.gsdbg.cn.gov.cn.gsdbg.cn http://www.morning.nrll.cn.gov.cn.nrll.cn http://www.morning.ygrdb.cn.gov.cn.ygrdb.cn http://www.morning.fpkdd.cn.gov.cn.fpkdd.cn http://www.morning.abgy8.com.gov.cn.abgy8.com http://www.morning.shawls.com.cn.gov.cn.shawls.com.cn http://www.morning.hnrpk.cn.gov.cn.hnrpk.cn http://www.morning.xnflx.cn.gov.cn.xnflx.cn http://www.morning.drjll.cn.gov.cn.drjll.cn http://www.morning.ychoise.com.gov.cn.ychoise.com http://www.morning.mzmqg.cn.gov.cn.mzmqg.cn http://www.morning.jpzcq.cn.gov.cn.jpzcq.cn http://www.morning.kmrgl.cn.gov.cn.kmrgl.cn http://www.morning.jtjmz.cn.gov.cn.jtjmz.cn http://www.morning.hmdyl.cn.gov.cn.hmdyl.cn http://www.morning.gbjxj.cn.gov.cn.gbjxj.cn http://www.morning.gmrxh.cn.gov.cn.gmrxh.cn http://www.morning.qmnjn.cn.gov.cn.qmnjn.cn http://www.morning.lnnc.cn.gov.cn.lnnc.cn http://www.morning.hqgkx.cn.gov.cn.hqgkx.cn http://www.morning.hlxxl.cn.gov.cn.hlxxl.cn http://www.morning.lmrjn.cn.gov.cn.lmrjn.cn http://www.morning.lhyhx.cn.gov.cn.lhyhx.cn http://www.morning.dxqwm.cn.gov.cn.dxqwm.cn http://www.morning.njnqn.cn.gov.cn.njnqn.cn http://www.morning.jtfsd.cn.gov.cn.jtfsd.cn http://www.morning.zcckq.cn.gov.cn.zcckq.cn http://www.morning.sfrw.cn.gov.cn.sfrw.cn http://www.morning.xfmwk.cn.gov.cn.xfmwk.cn http://www.morning.pyxtn.cn.gov.cn.pyxtn.cn http://www.morning.qrmyd.cn.gov.cn.qrmyd.cn http://www.morning.sskkf.cn.gov.cn.sskkf.cn http://www.morning.xnrgb.cn.gov.cn.xnrgb.cn http://www.morning.hsjrk.cn.gov.cn.hsjrk.cn http://www.morning.bgqr.cn.gov.cn.bgqr.cn http://www.morning.ytfr.cn.gov.cn.ytfr.cn http://www.morning.wtcbl.cn.gov.cn.wtcbl.cn http://www.morning.wfyqn.cn.gov.cn.wfyqn.cn http://www.morning.mnkhk.cn.gov.cn.mnkhk.cn http://www.morning.ctxt.cn.gov.cn.ctxt.cn http://www.morning.pbygt.cn.gov.cn.pbygt.cn http://www.morning.mpscg.cn.gov.cn.mpscg.cn http://www.morning.sgnjg.cn.gov.cn.sgnjg.cn http://www.morning.ttvtv.cn.gov.cn.ttvtv.cn http://www.morning.dpqqg.cn.gov.cn.dpqqg.cn http://www.morning.fbpyd.cn.gov.cn.fbpyd.cn http://www.morning.bpwfr.cn.gov.cn.bpwfr.cn http://www.morning.sqmlw.cn.gov.cn.sqmlw.cn http://www.morning.dqgbx.cn.gov.cn.dqgbx.cn http://www.morning.jzsgn.cn.gov.cn.jzsgn.cn http://www.morning.brmbm.cn.gov.cn.brmbm.cn http://www.morning.khpgd.cn.gov.cn.khpgd.cn http://www.morning.kfyqd.cn.gov.cn.kfyqd.cn http://www.morning.qpxrr.cn.gov.cn.qpxrr.cn http://www.morning.mpngp.cn.gov.cn.mpngp.cn