对于字符串中的每个字符

发布于 2025-01-08 13:51:00 字数 36 浏览 0 评论 0原文

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

How would I do a for loop on every character in string in C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

人海汹涌 2025-01-15 13:51:00
  1. 循环遍历 std::string 的字符,使用基于范围的 for 循环(来自 C++11,最新版本的 GCC、clang 和 VC11 beta 已支持):

    std::string str = ???;
    for(char& c : str) {
        do_things_with(c);
    }
    
  2. 循环 a 的字符std::string 与迭代器:

    std::string str = ???;
    for(std::string::iterator it = str.begin(); it != str.end(); ++it) {
        do_things_with(*it);
    }
    
  3. 使用迭代器循环遍历 std::string 的字符老式的 for 循环:

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

    char* str = ???;
    for(char* it = str; *it; ++it) {
        do_things_with(*it);
    }
    
  1. 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);
    }
    
  2. 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);
    }
    
  3. 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]);
    }
    
  4. Looping through the characters of a null-terminated character array:

    char* str = ???;
    for(char* it = str; *it; ++it) {
        do_things_with(*it);
    }
    
娜些时光,永不杰束 2025-01-15 13:51:00

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 string,第一个称为std string或(c ++ string)。我建议使用c ++ string,更容易处理。

A for loop can be implemented like this:

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

This will print the string character by character. str[i] returns character at index i.

If it is a character array:

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

Basically above two are two type of strings supported by c++.
The second is called c string and the first is called std string or(c++ string).I would suggest use c++ string,much Easy to handle.

画中仙 2025-01-15 13:51:00

在现代 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 ,以及 for (char const & ; c : s) 或 C++11 中的 for (char c : s)

In modern C++:

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

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

In C++98/03:

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

For read-only iteration, you can use std::string::const_iterator in C++98, and for (char const & c : s) or just for (char c : s) in C++11.

破晓 2025-01-15 13:51:00

这是另一种方法,使用标准算法。

#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;
   });
}

Here is another way of doing it, using the standard algorithm.

#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;
   });
}
千里故人稀 2025-01-15 13:51:00
const char* str = "abcde";
int len = strlen(str);
for (int i = 0; i < len; i++)
{
    char chr = str[i];
    //do something....
}
const char* str = "abcde";
int len = strlen(str);
for (int i = 0; i < len; i++)
{
    char chr = str[i];
    //do something....
}
情绪操控生活 2025-01-15 13:51:00

我没有看到任何使用基于范围的 for 循环和“c string”的示例。

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);
}

I don't see any examples using a range based for loop with a "c string".

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);
}

not related but int array example

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

for (int& i : ia) {
    printf("%d", i);
}
烦人精 2025-01-15 13:51:00
for (int x = 0; x < yourString.size();x++){
        if (yourString[x] == 'a'){
            //Do Something
        }
        if (yourString[x] == 'b'){
            //Do Something
        }
        if (yourString[x] == 'c'){
            //Do Something
        }
        //...........
    }

字符串基本上是一个字符数组,因此您可以指定索引来获取字符。如果您不知道索引,那么您可以像上面的代码一样循环遍历它,但是当您进行比较时,请确保使用单引号(它指定一个字符)。

除此之外,上面的代码是不言自明的。

for (int x = 0; x < yourString.size();x++){
        if (yourString[x] == 'a'){
            //Do Something
        }
        if (yourString[x] == 'b'){
            //Do Something
        }
        if (yourString[x] == 'c'){
            //Do Something
        }
        //...........
    }

A String is basically an array of characters, therefore you can specify the index to get the character. If you don't know the index, then you can loop through it like the above code, but when you're making a comparison, make sure you use single quotes (which specifies a character).

Other than that, the above code is self explanatory.

世界和平 2025-01-15 13:51:00

对于 C 字符串 (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;
}

For C-string (char []) you should do something like this:

char mystring[] = "My String";
int size = strlen(mystring);
int i;
for(i = 0; i < size; i++) {
    char c = mystring[i];
}

For std::string you can use str.size() to get its size and iterate like the example , or could use an iterator:

std::string mystring = "My String";
std::string::iterator it;
for(it = mystring.begin(); it != mystring.end(); it++) {
    char c = *it;
}
赠佳期 2025-01-15 13:51:00

您可以使用 size() 方法来获取字符串的长度,并使用方括号运算符来访问每个单独的字符。

#include<bits/stdc++.h>
using namespace std;

int main()
{
   string s;
   cin >> s;
   int length = s.size();
   for(int i = 0; i < length; i++)
   {
      process(s[i]);
   }
}

You can use the size() method to get the lenght of the string and the square bracket operator to access each individual character.

#include<bits/stdc++.h>
using namespace std;

int main()
{
   string s;
   cin >> s;
   int length = s.size();
   for(int i = 0; i < length; i++)
   {
      process(s[i]);
   }
}
逆夏时光 2025-01-15 13:51:00

您可以使用字符串库的 at 函数获取字符串中的每个字符,就像我这样做一样,

string words;
    for (unsigned int i = 0; i < words.length(); i++)
        {
            if (words.at(i) == ' ')
            {
                spacecounter++;    // to count all the spaces in a string
                if (words.at(i + 1) == ' ')
                {
                    i += 1;
                }

这只是我的代码的一部分,但重点是您可以通过 stringname.at(index)< 访问字符/代码>

you can get every char in a string by using the at function of string library, like i did it like this

string words;
    for (unsigned int i = 0; i < words.length(); i++)
        {
            if (words.at(i) == ' ')
            {
                spacecounter++;    // to count all the spaces in a string
                if (words.at(i + 1) == ' ')
                {
                    i += 1;
                }

this is just a segment of my code but the point is you can access characters by stringname.at(index)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文