用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;
}
用”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;
}
相关文章推荐
- Source Insight使用配置
- oracle中的sql语句中timestamp与字符串转换
- 3.使用POJO与Servlet原生API作为参数
- Android开发之Service完全解析,关于服务你所需知道的一切
- Step into Kotlin - 19 - 正则表达式
- 针对开发者的21款移动开发框架
- centos中mysql重置密码
- iOS定位获取经纬度以及计算位置
- 求一个整数的二进制位数
- 关于iOS block循环引用的一点理解
- 设置Label行间距
- php -- PDO
- Entity Framework 之三层架构
- shell练习-远程监控
- 5分钟排错-拯救躺枪的tomcat
- Override与Overload区别
- g++动态库创建
- 使用nRF51822/nRF51422创建一个简单的BLE应用 ---入门实例手册(中文)之四
- iOS 晃动手势
- yii2的安装