C++中范围for语句
2024-10-19 04:24:42
如果想对string对象中的每个字符做点什么操作,目前最好的办法是使用C++11新标准提供的一种语句:范围for(range for)语句。
示例代码:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str("some string");
//每行输出str中的一个字符。
for (auto c : str)
cout << c << endl;
return 0;
}
如果想要改变string对象中字符的值,必须把循环变量定义成引用类型。
示例代码:
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main()
{
string str("some string");
//转换成大写形式
for (auto &c : str)
c = toupper(c);
cout << str << endl;
return 0;
}
最新文章
- JetBrain WebStorm 注册码
- Kafka 快速起步(作者:杜亦舒)
- poj 2184 Cow Exhibition
- Android Service 系统服务
- 宣布发布 Windows Azure ExpressRoute,宣告与 Level 3 建立全新的合作伙伴关系并推出关于其他 Azure 服务令人振奋的更新
- html+css实现图片的层布局
- Linux内核进程管理
- Hibernate-----关系映射 重点!!!
- Linux中后台执行任务
- jQuery测验题
- js二维码插件总结
- CCF CSP 认证
- 内置函数_range()
- USB to UART
- Birdman Quotes
- vue-cli+webpack在生成的项目中使用bootstrap方法(二)
- 使用Linux命令行测试网速-----speedtest-cli
- 悟空模式-java-工厂方法模式
- 关于django过滤器的使用
- Ubuntu14.04下Cloudera安装搭建部署大数据集群(图文分五大步详解)(博主强烈推荐)(在线或离线)