在 C/C++ 中迭代字母表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18152136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:45:58  来源:igfitidea点击:

Iterate alphabet in C/C++

c++c

提问by user1735120

How do I translate the following code into C/C++?

如何将以下代码翻译成 C/C++?

string alphabet = "abcdefghijklmnopqrstuvwxyz";

foreach(char c in alphabet)
{
    // Do something with the letter
}

I want to loop into the alphabet and print each character when a button is pressed. Like how you input a character when using an Xbox/PS3controller. You scroll into the entire set of alphabet and then press a button for input.

我想循环到字母表中并在按下按钮时打印每个字符。就像使用Xbox/ PS3控制器时输入字符的方式一样。您滚动到整个字母表中,然后按一个按钮进行输入。

Basically this is being used in a microcontroller (mbed) environment. I just need to know how to create the correct logic in looping in C/C++.

基本上这是在微控制器 ( mbed) 环境中使用的。我只需要知道如何在 C/C++ 循环中创建正确的逻辑。

回答by KitsuneYMG

#include <string>
#include <algorithm>

void doSomething( const char& );

int main() {
  std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
  std::for_each( alphabet.begin(), alphabet.end(), &doSomething);
}

or

或者

#include <string>
#include <boost/foreach.hpp>

int main() {
  std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
  BOOST_FOREACH( char c, alphabet) {
    //do something
  }
}

With C++2011

使用 C++2011

#include <string>
#include <algorithm>

int main() {
  std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
  std::for_each( alphabet.begin(), alphabet.end(),[](const char& c){ /*do something*/} );
}

or even

甚至

#include <string>

int main() {
  std::string alphabet = "abcdefghijklmnopqrstuvwxyz";
  for( char c : alphabet) {
    //do something
  }
}

EDIT: You mentioned in your edit the desire for this to work in an embedded environment

编辑:您在编辑中提到希望在嵌入式环境中工作

int main() {
  const char* alphabet = "abcdefghijklmnopqrstuvwxyz";
  for( char* ptr = alphabet; *ptr!='
int main() {
  for( char l = 'a'; l<='z'; ++l) {
    //do something (l)
  }
}
'; ++ptr) { //do something (*ptr) } }

Edit2:

编辑2:

If you knowthat you're using something like ASCII

如果你知道你正在使用类似 ASCII 的东西

for (auto c : alphabet)
{
    //do something with letter
}

回答by Mike Seymour

You should decide which language you're using; C and C++ are two distinct languages, so there isn't a single answer for both languages.

您应该决定使用哪种语言;C 和 C++ 是两种不同的语言,因此这两种语言都没有统一的答案。

In modern C++:

在现代 C++ 中:

for (size_t i = 0; i < alphabet.size(); ++i)
{
    char c = alphabet[i];
    //do something with letter
}

In old-school C++, there are various options; the least ugly is probably

在老式 C++ 中,有多种选择;最不丑的可能是

char const * alphabet = "abcdefghijklmnopqrstuvwxyz";

In C, it depends what stringis, since there is no standard type with that name. If it's a conventional zero-terminated character array:

在 C 中,这取决于是什么string,因为没有具有该名称的标准类型。如果它是一个传统的以零结尾的字符数组:

for (char const * p = alphabet; *p; ++p)
{
    char c = *p;
    // do something with letter
}

then something like:

然后是这样的:

#include <iostream>
#include <string>

int main ()
{
  std::string str ("abcdefghijklmnopqrstuvwxyz");
  for ( std::string::iterator itr=str.begin(); it!=str.end(); ++it)
  {
  //do something
  }
  return 0;
}

回答by Thomas Wyrick

##代码##