本文介绍了C ++怪异的循环语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std::string decodeMorse(std::string morseCode) {
    // ToDo: Accept dots, dashes and spaces, return human-readable message
    std::string decoded;
    for( auto p : morseCode ) {
      if( p == '.' )
        decoded += MORSE_CODE[ "." ];
      else if( p == '-' )
        decoded += MORSE_CODE[ "-" ];
    }
    return decoded;
}

这是从 https://www.codewars.com

有人可以解释 for(auto p:morseCode)是什么意思吗?还是以更精细的方式重新编写这段代码?

Can somebody explain what for( auto p : morseCode ) means?Or perhaps re-write this piece of code in more elaborate way?

推荐答案

它迭代 morseCode 中的每个元素,从一个 begin 所引用的开始,并以 end 前一个.每个迭代元素的值都将复制到 p 中,其类型是取消引用的迭代器的类型.

It iterates over every element in morseCode, starting from the one begin refers to and ending with the one before end. The value of each iterated element is copied into p, whose type is the type of the dereferenced iterator.

这篇关于C ++怪异的循环语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 16:07