如何在c++中对字符串中的每个字符进行for循环?
当前回答
Looping through the characters of a std::string, using a range-based for loop (it's from C++11, already supported in recent releases of GCC, clang, and the VC11 beta): std::string str = ???; for(char& c : str) { do_things_with(c); } Looping through the characters of a std::string with iterators: std::string str = ???; for(std::string::iterator it = str.begin(); it != str.end(); ++it) { do_things_with(*it); } Looping through the characters of a std::string with an old-fashioned for-loop: std::string str = ???; for(std::string::size_type i = 0; i < str.size(); ++i) { do_things_with(str[i]); } Looping through the characters of a null-terminated character array: char* str = ???; for(char* it = str; *it; ++it) { do_things_with(*it); }
其他回答
对于C-string (char[]),你应该这样做:
char mystring[] = "My String";
int size = strlen(mystring);
int i;
for(i = 0; i < size; i++) {
char c = mystring[i];
}
对于std::string,你可以使用str.size()来获取它的大小并像示例一样迭代,或者可以使用迭代器:
std::string mystring = "My String";
std::string::iterator it;
for(it = mystring.begin(); it != mystring.end(); it++) {
char c = *it;
}
在现代c++中:
std::string s("Hello world");
for (char & c : s)
{
std::cout << "One character: " << c << "\n";
c = '*';
}
在 C++98/03 中:
for (std::string::iterator it = s.begin(), end = s.end(); it != end; ++it)
{
std::cout << "One character: " << *it << "\n";
*it = '*';
}
对于只读迭代,在c++ 98中可以使用std::string::const_iterator,在c++ 11中可以使用For (char const & C:s)或仅For (char C:s)。
Looping through the characters of a std::string, using a range-based for loop (it's from C++11, already supported in recent releases of GCC, clang, and the VC11 beta): std::string str = ???; for(char& c : str) { do_things_with(c); } Looping through the characters of a std::string with iterators: std::string str = ???; for(std::string::iterator it = str.begin(); it != str.end(); ++it) { do_things_with(*it); } Looping through the characters of a std::string with an old-fashioned for-loop: std::string str = ???; for(std::string::size_type i = 0; i < str.size(); ++i) { do_things_with(str[i]); } Looping through the characters of a null-terminated character array: char* str = ???; for(char* it = str; *it; ++it) { do_things_with(*it); }
for循环可以这样实现:
string str("HELLO");
for (int i = 0; i < str.size(); i++){
cout << str[i];
}
这将逐字符打印字符串。Str [i]返回索引i处的字符。
如果是字符数组:
char str[6] = "hello";
for (int i = 0; str[i] != '\0'; i++){
cout << str[i];
}
基本上以上两个是c++支持的两种类型的字符串。 第二个称为c字符串,第一个称为std字符串或(c++字符串)。我建议使用c++字符串,很容易处理。
我没有看到任何使用“c字符串”的基于范围的for循环的例子。
char cs[] = "This is a c string\u0031 \x32 3";
// range based for loop does not print '\n'
for (char& c : cs) {
printf("%c", c);
}
不是相关的,而是int数组的例子
int ia[] = {1,2,3,4,5,6};
for (int& i : ia) {
printf("%d", i);
}
推荐文章
- 是否需要手动关闭ifstream?
- 为什么函数指针定义可以使用任意数量的&号或星号* ?
- 为什么我必须通过this指针访问模板基类成员?
- 用PHP删除字符串的前4个字符
- 什么是可重入函数?
- 什么是栈展开?
- 我如何读整个文件到性病::字符串在c++ ?
- 如何在c++中使用枚举
- 为什么512x512矩阵的转置比513x513矩阵的转置慢得多?
- 我如何应用for-each循环到字符串中的每个字符?
- 通过引用传递数组
- 为什么非const引用不能绑定到临时对象?
- sizeof(某个指针)总是等于4吗?
- 在c++中使用数组或std::vector,性能差距是什么?
- 什么是std::decay ?什么时候应该使用它?