对于字符串中的每个字符

Jac*_*don 208 c++ loops for-loop character

如何在C++中对字符串中的每个字符执行for循环?

R. *_*des 385

  1. 通过循环字符std::string,使用范围为基础的环路(它从C++ 11的,已经支持在最近GCC,铛和VC11的β版本):

    std::string str = ???;
    for(char& c : str) {
        do_things_with(c);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 循环遍历std::string带有迭代器的字符:

    std::string str = ???;
    for(std::string::iterator it = str.begin(); it != str.end(); ++it) {
        do_things_with(*it);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 循环std::string使用旧式for 循环的字符:

    std::string str = ???;
    for(std::string::size_type i = 0; i < str.size(); ++i) {
        do_things_with(str[i]);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 循环遍历以null结尾的字符数组的字符:

    char* str = ???;
    for(char* it = str; *it; ++it) {
        do_things_with(*it);
    }
    
    Run Code Online (Sandbox Code Playgroud)

  • @Robinson:_"std :: string是UTF8(假设是)_"从什么时候开始?! (6认同)
  • @Robinson:这是一个错误的假设.一个非常错误的假设.此外,"人物"有很多不同的含义,最好严格避免使用这个术语. (4认同)
  • @Robinson:我的所有人都被视为无编码因为我不是在面向用户的域中编程(也就是说,没有任何字符串注定要呈现给人类).如果你想谈论字符编码,你需要讨论一个更高级别的抽象_在_std :: string`之上,这只是一系列字节. (4认同)
  • 嗯,好吧,它没有编码,但是考虑到现在无处不在的utf8(特别是在网络上)以及人们可能想要在整个管道或应用程序中进行单一一致编码的事实,为此讨论的基础我的std :: strings都是utf8:p. (2认同)
  • 此外,案例 2 和案例 3 是您可以/应该使用“自动”的好例子 (2认同)

小智 28

for循环可以像这样实现:

string str("HELLO");
for (int i = 0; i < str.size(); i++){
    cout << str[i];
}
Run Code Online (Sandbox Code Playgroud)

这将按字符打印字符串.str[i]返回索引处的字符i.

如果是字符数组:

char str[6] = "hello";
for (int i = 0; str[i] != '\0'; i++){
    cout << str[i];
}
Run Code Online (Sandbox Code Playgroud)

基本上上面两个是c ++支持的两种类型的字符串.第二个叫做c string,第一个叫做std string或(c ++ string).我建议用c ++ string,很容易处理.


Ker*_* SB 24

在现代C++中:

std::string s("Hello world");

for (char & c : s)
{
    std::cout << "One character: " << c << "\n";
    c = '*';
}
Run Code Online (Sandbox Code Playgroud)

在C++ 98/03中:

for (std::string::iterator it = s.begin(), end = s.end(); it != end; ++it)
{
    std::cout << "One character: " << *it << "\n";
    *it = '*';
}
Run Code Online (Sandbox Code Playgroud)

对于只读迭代,您可以std::string::const_iterator在C++ 98中使用,for (char const & c : s)或者for (char c : s)在C++ 11中使用.

  • 这里引用char的作用是什么(`char&c`)?是否只是允许在需要的情况下修改字符值? (2认同)

0xB*_*F00 11

这是另一种使用标准算法的方法.

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
   std::string name = "some string";
   std::for_each(name.begin(), name.end(), [] (char c) {
      std::cout << c;
   });
}
Run Code Online (Sandbox Code Playgroud)


dem*_*key 7

const char* str = "abcde";
int len = strlen(str);
for (int i = 0; i < len; i++)
{
    char chr = str[i];
    //do something....
}
Run Code Online (Sandbox Code Playgroud)

  • (过时的评论,这仍然可能与OP相关:)在循环条件中使用`strlen`不被认为是好的形式,因为它需要对每次迭代的字符串进行O(n)操作,从而形成整个循环O(n ^ 2)的大小为字符串.如果字符串在循环期间发生变化,则可以调用循环条件中的`strlen`,但是应该为实际需要*的情况保留. (4认同)

10S*_*Tom 6

我没有看到任何使用基于范围的带有“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);
}
Run Code Online (Sandbox Code Playgroud)

不相关但 int 数组示例

int ia[] = {1,2,3,4,5,6};

for (int& i : ia) {
    printf("%d", i);
}
Run Code Online (Sandbox Code Playgroud)