深入学习C++String21版经过个人的修改感谢原著作者.docx

上传人:b****0 文档编号:8920308 上传时间:2023-05-16 格式:DOCX 页数:25 大小:176.12KB
下载 相关 举报
深入学习C++String21版经过个人的修改感谢原著作者.docx_第1页
第1页 / 共25页
深入学习C++String21版经过个人的修改感谢原著作者.docx_第2页
第2页 / 共25页
深入学习C++String21版经过个人的修改感谢原著作者.docx_第3页
第3页 / 共25页
深入学习C++String21版经过个人的修改感谢原著作者.docx_第4页
第4页 / 共25页
深入学习C++String21版经过个人的修改感谢原著作者.docx_第5页
第5页 / 共25页
深入学习C++String21版经过个人的修改感谢原著作者.docx_第6页
第6页 / 共25页
深入学习C++String21版经过个人的修改感谢原著作者.docx_第7页
第7页 / 共25页
深入学习C++String21版经过个人的修改感谢原著作者.docx_第8页
第8页 / 共25页
深入学习C++String21版经过个人的修改感谢原著作者.docx_第9页
第9页 / 共25页
深入学习C++String21版经过个人的修改感谢原著作者.docx_第10页
第10页 / 共25页
深入学习C++String21版经过个人的修改感谢原著作者.docx_第11页
第11页 / 共25页
深入学习C++String21版经过个人的修改感谢原著作者.docx_第12页
第12页 / 共25页
深入学习C++String21版经过个人的修改感谢原著作者.docx_第13页
第13页 / 共25页
深入学习C++String21版经过个人的修改感谢原著作者.docx_第14页
第14页 / 共25页
深入学习C++String21版经过个人的修改感谢原著作者.docx_第15页
第15页 / 共25页
深入学习C++String21版经过个人的修改感谢原著作者.docx_第16页
第16页 / 共25页
深入学习C++String21版经过个人的修改感谢原著作者.docx_第17页
第17页 / 共25页
深入学习C++String21版经过个人的修改感谢原著作者.docx_第18页
第18页 / 共25页
深入学习C++String21版经过个人的修改感谢原著作者.docx_第19页
第19页 / 共25页
深入学习C++String21版经过个人的修改感谢原著作者.docx_第20页
第20页 / 共25页
亲,该文档总共25页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

深入学习C++String21版经过个人的修改感谢原著作者.docx

《深入学习C++String21版经过个人的修改感谢原著作者.docx》由会员分享,可在线阅读,更多相关《深入学习C++String21版经过个人的修改感谢原著作者.docx(25页珍藏版)》请在冰点文库上搜索。

深入学习C++String21版经过个人的修改感谢原著作者.docx

深入学习C++String21版经过个人的修改感谢原著作者

目录

目录错误!

未定义书签。

一、C++的string的使用错误!

未定义书签。

1.1C++string简介错误!

未定义书签。

1.2string的成员错误!

未定义书签。

1.2.1append2

1.2.2assign3

1.2.3at4

#include4

#include4

usingnamespacestd;4

intmain()4

{4

stringa,b;4

a="abcde";4

b="fghij";4

charx[]="123456";4

cout<

return0;4

}4

1.2.4begin4

1.2.5clear4

1.2.6compare4

1.2.7copy4

1.2.8empty5

1.2.9end5

1.2.10erase5

1.2.11find5

1.2.12find_first_not_of6

1.2.13find_first_of6

1.2.14find_last_not_of7

1.2.15find_last_of7

1.2.16insert7

1.2.17length8

1.2.18max_size8

1.2.19rbegin8

1.2.20rend8

1.2.21replace8

1.2.22rfind9

1.2.23size10

1.2.24substr10

1.2.25swap10

1.3string与algorithm相结合的使用10

1.5.1string与remove10

1.5.2string与unique、sort11

1.5.3string与search11

1.5.4string和find、find_if12

1.5.5string与copy、copy_if12

1.5.6string与count、count_if13

 

一、C++的string的使用

1.1C++string简介

C++兼容C对字符串的处理方式,与此同时还在标准库(STL)中提供了string容器,我们可以很容易的使用string来进行字符串处理。

而且string还能很好的与标准库中的泛型算法结合起来使用,非常的方便。

虽然在MFC等框架中也提供了诸如CString这样的字符串处理类,但是个人认为STL的string依然是最棒的,使用标准库提供的string可以轻松的与原来的CAPI兼容,也可以很好的与系统底层的API兼容。

1.2string的成员

1.2.1append

在尾部添加字符或者字符串

append共有8种重载:

#include

#include

#include

usingnamespacestd;

intmain()

{

stringa,b;

a="abcde";

b="fghij";

charx[]="123456";

cout<<1<

a.append(x);//第一种重载方法

cout<

//输出:

abcde123456

cout<<2<

a="abcde";

a.append(x,3);//第二种重载分方法

cout<

//输出:

abcde123

cout<<3<

a="abcde";

a.append(b,0,2);//第三种重载方法

cout<

//输出:

abcdefg

cout<<4<

a="abcde";

a.append(b);

cout<

//输出:

abcdefghij

cout<<5<

a="abcde";

a.append(5,'0');//第五种重载方法

cout<

//输出:

abcde00000

cout<<6<

a="abcde";

string:

:

const_iteratori=a.begin(),i1=a.end();

a.append(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符

cout<

//输出:

abcdeabcde

cout<<7<

chartest0[]="987654321";

vectortest(test0,test0+9);

vector:

:

iteratori2=test.begin(),i3=test.end();

a="abcde";

a.append(i2,i3);//第七种重载方法和第六种差不多!

//不过这个可以加除了string以外类型的迭代器的字符

cout<

//输出:

abcde987654321

return0;

}

1.2.2assign

为字符串重新赋予新的内容。

你可以将它看做先把字符串清空,然后再append。

因此assign也有8种重载。

例子:

#include

#include

#include

usingnamespacestd;

intmain()

{

stringa,b;

a="abcde";

b="fghij";

charx[]="123456";

cout<<1<

a.assign(x);//第一种重载方法

cout<

//输出:

123456

cout<<2<

a="abcde";

a.assign(x,3);//第二种重载分方法

cout<

//输出:

123

cout<<3<

a="abcde";

a.assign(b,0,2);//第三种重载方法

cout<

//输出:

fg

cout<<4<

a="abcde";

a.assign(b);

cout<

//输出:

fghij

cout<<5<

a="abcde";

a.assign(5,'0');//第五种重载方法

cout<

//输出:

00000

cout<<6<

a="abcde";

b="edcba";

string:

:

const_iteratori=b.begin(),i1=b.end();

a.assign(i,i1);//第六种重载方法string加上迭代器i到i1之间的字符

cout<

//输出:

edcba

cout<<7<

chartest0[]="987654321";

vectortest(test0,test0+9);

vector:

:

iteratori2=test.begin(),i3=test.end();

a="abcde";

a.assign(i2,i3);//第七种重载方法和第六种差不多!

//不过这个可以加除了string以外类型的迭代器的字符

cout<

//输出:

987654321

return0;

}

1.2.3at

#include

#include

usingnamespacestd;

intmain()

{

stringa,b;

a="abcde";

b="fghij";

charx[]="123456";

cout<

return0;

}

1.2.4begin

用迭代器时使用!

1.2.5clear

删除所有字符。

1.2.6compare

以字典顺序进行比较。

>为正,=为零,<为负。

#include

#include

usingnamespacestd;

intmain()

{

stringa,b;

a="123";

b="023";

cout<

//输出:

1

return0;

}

1.2.7copy

拷贝,为内部调用。

#include

#include

usingnamespacestd;

intmain()

{

intlength;

charbuffer[20];

stringstr("Teststring...");

length=str.copy(buffer,6,5);//拷贝str[5]后门的6个字符给buffer

buffer[length]='\0';

cout<<"buffercontains:

"<

//输出:

buffercontains:

string

return0;

}

1.2.8empty

返回字符串是否为空。

1.2.9end

返回指向末尾的iterator或者const_iterator。

不能对其返回值提领!

1.2.10erase

删除字符串。

#include

#include

usingnamespacestd;

intmain()

{

stringstr("Thisisanexamplephrase.");

string:

:

iteratorit;

//eraseusedinthesameorderasdescribedabove:

str.erase(10,8);//删除str[10]后的8个字符

cout<

it=str.begin()+9;

str.erase(it);//深处迭代器位置的字符

cout<

str.erase(str.begin()+5,str.end()-7);//删除迭代器中间的字符

cout<

return0;

}

 

1.2.11find

1.2.12find_first_not_of

查找第一个不是指定字符或者不是指定字符串中的任意一个元素的位置并返回。

例子:

#include

#include

usingnamespacestd;

intmain()

{

stringstr("lookfornon-alphabeticcharacters...");

size_tfound;

found=str.find_first_not_of("abcdefghijklmnopqrstuvwxyz");

//查找串中没有的元素!

返回第一个找到的元素的下标!

if(found!

=string:

:

npos)

{

cout<<"Firstnon-alphabeticcharacteris"<

cout<<"atposition"<

}

//输出:

Firstnon-alphabeticcharacteris-atposition12;

return0;

}

1.2.13find_first_of

查找第一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!

例子:

#include

#include

usingnamespacestd;

intmain()

{

stringstr("lookfornon-alphabeticcharacters...");

size_tfound;

found=str.find_first_of("abcdefghijklmnopqrstuvwxyz");

//查找串中有的元素!

返回第一个找到的元素的下标!

if(found!

=string:

:

npos)

{

cout<<"Firstnon-alphabeticcharacteris"<

cout<<"atposition"<

}

//输出:

Firstnon-alphabeticcharacterislatposition0;

return0;

}

1.2.14find_last_not_of

查找最一个不是指定字符或者不是指定字符串中的任意一个字符的位置并返回!

例子:

#include

#include

usingnamespacestd;

intmain()

{

stringstr("lookfornon-alphabeticcharacters...");

size_tfound;

found=str.find_last_not_of("abcdefghijklmnopqrstuvwxyz");

//查找串中没有的元素!

返回最后一个个找到的元素的下标!

if(found!

=string:

:

npos)

{

cout<<"Firstnon-alphabeticcharacteris"<

cout<<"atposition"<

}

//输出:

Firstnon-alphabeticcharacteris.atposition36;

return0;

}

1.2.15find_last_of

查找最后一个是指定字符或者是指定字符串中的任意一个元素的位置并返回!

例子:

#include

#include

usingnamespacestd;

intmain()

{

stringstr("lookfornon-alphabeticcharacters...");

size_tfound;

found=str.find_last_of("abcdefghijklmnopqrstuvwxyz");

//查找串中有的元素!

返回最后一个个找到的元素的下标!

if(found!

=string:

:

npos)

{

cout<<"Firstnon-alphabeticcharacteris"<

cout<<"atposition"<

}

//输出:

Firstnon-alphabeticcharacterissatposition33;

return0;

}

1.2.16insert

插入一个字符、插入指定个数的字符、插入一个指定范围的字符串的元素。

这个函数拥有大量的重载,我们可以选择使用index或者是iterator来操作,非常的方便。

详细的例子可以参考MSDN,其它没有什么值得说的。

1.2.17length

返回字符串长度。

1.2.18max_size

返回一个string所能容纳的最大字符个数。

通常我们都不需要用到这个函数。

1.2.19rbegin

返回反向迭代器,指向反向的第一个元素。

同vector,不解释

1.2.20rend

返回反向迭代器,指向最后一个元素。

绝对不能对它进行提领操作。

同vector,不解释

1.2.21replace

字符串替换。

#include

#include

usingnamespacestd;

intmain()

{

stringbase="thisisateststring.";

stringstr2="nexample";

stringstr3="samplephrase";

stringstr4="useful.";

//functionversionsusedinthesameorderasdescribedabove:

//Usingpositions:

0123456789*123456789*12345

stringstr=base;//"thisisateststring."

str.replace(9,5,str2);//str[9]到后面的5个字符用str2代替

cout<

str.replace(19,6,str3,7,6);//str[19]后面的6个字符用str3上的str[7]后面的6个字符代替

cout<

str.replace(8,10,"justall",6);//str[8]后面的10个字符用"justall"上的前6个字符代替

cout<

str.replace(8,6,"ashort");//str[8]后面的6个字符用"ashort"代替

cout<

str.replace(22,1,3,'!

');//str[22]后面的1个字符用3个"!

"代替

cout<

//Usingiterators:

0123456789*123456789*

string:

:

iteratorit=str.begin();//^

str.replace(it,str.end()-3,str3);//str的it到str.end()-3的位置之间的字符用str3代替

cout<

str.replace(it,it+6,"replaceit",7);//str的it到it+6的位置之间的字符用"replaceit"的前7个字符代替

cout<

it+=8;//^

str.replace(it,it+6,"iscool");//str的it到it+6的位置之间的字符用"iscool"代替

cout<

str.replace(it+4,str.end()-4,4,'o');//str的it+4到str.end()-4的位置之间用个4个"o"代替

cout<

it+=3;//^

str.replace(it,str.end(),str4.begin(),str4.end());

//str里的it到str.end()位置的字符用str4里的

//str4.begin()到str4.end()的字符替换

cout<

cout<

return0;

}

输出:

.

1.2.22rfind

跟find类似,不过是反过来搜索。

特别注意,返回值是index,没有rindex的概念。

输出最后一个找到的字符串的第一个字符的下标

例子:

1.2.23size

返回长度。

跟length()一样。

1.2.24substr

例子:

1.2.25swap

同vector,不解释

1.3string与algorithm相结合的使用

string是C++以库形式提供的字符串,但是首先,它是一个通用的容器,因此用它和C++的泛型算法结合使用完全没有问题。

1.5.1string与remove

#include

#include

#include

usingnamespacestd;

intmain()

{

stringss("HelloWorld!

_HelloWorld!

");

cout<

ss.erase(remove(ss.begin(),ss.end(),'H'),ss.end());

cout<

ss="HelloWorld!

_HelloWorld!

";

ss.erase(remove_if(ss.begin(),ss.end(),islower),ss.end());

cout<

return0;

}

1.5.2string与unique、sort

#include

#include

#include

usingnamespacestd;

intmain()

{

stringss("HelloWorld!

_HelloWorld!

");

cout<

sort(ss.begin(),ss.end());

cout<

ss.erase(unique(ss.begin(),ss.end()),ss.end());

cout<

return0;

}

1.5.3string与search

#include

#include

#include

#include

usingnamespacestd;

structiCmp:

publicbinary_function

{

booloperator()(charleft,charright)

{

returntoupper(left)==toupper(right);

}

};

intmain()

{

stringstr(

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 自然科学 > 化学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2