在C / C迭代字母++ [英] Iterate alphabet in C/C++

查看:159
本文介绍了在C / C迭代字母++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何下code转换成C / C ++?

 串字母=ABCDEFGHIJKLMNOPQRSTUVWXYZ;的foreach(以字母字符C)
{
    //做一些字母
}

我要循环到字母和打印时,按钮pressed每个字符。怎么样,你输入一个字符使用的Xbox /的 PS3 控制器。您滚动到整组字母表,然后preSS输入的按钮。

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


解决方案

 的#include<串GT;
#包括LT&;&算法GT;无效DoSomething的(常量字符和放大器;);诠释主(){
  性病::串字母=ABCDEFGHIJKLMNOPQRSTUVWXYZ;
  的std :: for_each的(alphabet.begin(),alphabet.end(),放大器; DoSomething的);
}

 的#include<串GT;
#包括LT&;升压/ foreach.hpp>诠释主(){
  性病::串字母=ABCDEFGHIJKLMNOPQRSTUVWXYZ;
  BOOST_FOREACH(字符C,字母){
    //做一点事
  }
}

使用C ++ 2011

 的#include<串GT;
#包括LT&;&算法GT;诠释主(){
  性病::串字母=ABCDEFGHIJKLMNOPQRSTUVWXYZ;
  的std :: for_each的(alphabet.begin(),alphabet.end()[](为const char和C){/ *做某事* /});
}

甚至

 的#include<串GT;诠释主(){
  性病::串字母=ABCDEFGHIJKLMNOPQRSTUVWXYZ;
  对于(字符C:字母){
    //做一点事
  }
}

编辑:
你在你的编辑提到这在嵌入式环境中工作的愿望

  INT的main(){
  为const char *字母=ABCDEFGHIJKLMNOPQRSTUVWXYZ;
  对于(字符* PTR =字母; * PTR ='\\ 0';!++ PTR){
    //做一些事情(* PTR)
  }
}

EDIT2:

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

  INT的main(){
  对于(字符L ='A'; L< ='Z'; ++ L){
    //做一些事情(L)
  }
}

How do I translate the following code into 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/PS3 controller. You scroll into the entire set of alphabet and then press a button for input.

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++.

解决方案

#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

#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!='\0'; ++ptr) {
    //do something (*ptr)
  }
}

Edit2:

If you know that you're using something like ASCII

int main() {
  for( char l = 'a'; l<='z'; ++l) {
    //do something (l)
  }
}

这篇关于在C / C迭代字母++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆