C++11 auto类型说明符如for(atuo &x : s)

#include <bits/stdc++.h>

个头文件包含C++以下头文件:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>

在C++prime 中》》 
如果要更改字符串中的字符值,我们必须将循环变量定义为引用类型(第2.3.1节,第50页)。请记住,引用只是给定对象的另一个名称。当我们使用引用作为我们的控制变量时,该变量依次绑定到序列中的每个元素。使用引用,我们可以更改引用所绑定的字符。

string s("Hello World!!!");

// convert s to uppercase

for (auto &c : s) // for every char in s (note: c is a reference)

c = toupper(c); // c is a reference, so the assignment changes the char

in s

cout << s << endl;

 

此代码的输出是HELLO WORLD !!!

for循环中的每个迭代初始化一个新的引用

for (auto &c : s)

c = toupper(c);

相当于

for (auto it = s.begin(); it != s.end(); ++it)
{
    auto &c = *it;

    c = toupper(c);
}
  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值