新式for循环遍历字符串的每一个字符(C++)
#include <iostream>
#include <string>
#include <vector>
#include <list>
using namespace std;
int StrToInt3(string str)
{
int value = 0;
for (auto ch : str)//ch依次取的是str里面的字符,直到取完为止
{
value *= 10;
value += (ch - '0');
}
return value;
}
int main()
{
cout << StrToInt3("1234") << endl;
system("pause");
return 0;
}
底层还是用迭代器实现的
C++新式for循环解析:字符串字符遍历与底层迭代器应用
本文详细介绍了如何使用C++的新式for循环遍历字符串中的每个字符,并揭示了其实现原理——底层利用迭代器。通过实例展示了将字符串转换为整数的StrToInt3函数的应用。
&spm=1001.2101.3001.5002&articleId=120295172&d=1&t=3&u=821120a1f3794c70b76f5c5829df2267)
1062

被折叠的 条评论
为什么被折叠?



