C++11提供范围for语句,这个语句遍历给定的序列中的每个元素并对序列中的每个元素执行某种操作:
for (declaration : expression)
statement
- 输出string中的每个字符:
string str("some string");
for (auto c : str)
{
cout << c << endl;
}
在for循环中使用auto声明变量c,由编译器决定其类型,每次循环,将str中的下一个字符拷贝到c中。
- 使用ispunct函数来统计string中标点符号的个数
string s("Hello World!!!");
decltype(s.