您的位置:首页 > 其它

用string

2015-07-23 10:51 155 查看
用char s[]=”hello”构造1string,把world连接到hello后面,输出容器大小cap,反转字符串,修改第5个字符为a

用”c++ilovec++andjavac++andc++”string,find查找c++的位置,统计c++的个数,replace替换c++为java

.data和c_str有什么区别,length,size和max_size有什么区别

用resize重设string容器大小

#include <iostream>
#include <vector>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

int main()
{
char s[] = "hello";
string str(s); //用char s[]="hello"构造1string
str.append("world"); //把world连接到hello后面
cout<<str<<"\t容器:"<<str.capacity()<<endl; //输出容器大小cap
//用反向迭代器输出
string::reverse_iterator rpos;
for(rpos = str.rbegin();;rpos!=str.rend(),++rpos)
{
cout<<*rpos<<”\t”;
}
char c&=str.at(5);c=’a’;// 修改第5个字符为a
string s2 = "c++ilovec++andjavac++andc++";
int count = 0; //计数器
int ipos=s2.find(“c++”,0);
//find查找c++的位置,统计c++的个数
while(ipos!=-1)//如果找到
{
s2.replace(ipos,3,"java");//replace替换c++为java
count++;
ipos=s2,find(“c++”,ipos+3);//从下一个位置开始
}
cout<<"\n共有"<<count<<"个"<<sh2<<endl;
str2.resize(10);//用resize重设string容器大小
}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: