简体   繁体   English

“->second”的含义及自编注释

[英]Meaning of "->second" and self-made comments

I am trying to write a description of each of the actions in the code by commenting on things performed in the code.我试图通过评论代码中执行的事情来编写代码中每个操作的描述。

Could you please help me understand what does "createdCode->second" do and re-check if my made comments about the other parts are correct?您能否帮助我理解"createdCode->second"的作用并重新检查我对其他部分的评论是否正确?

Here is the code:这是代码:

#include <map>

#include <string>

#include <iostream>

int main() {

  // Stores everything that is that specific std namespace and dumps it in the global namespace as characters in key-value pairs and declares them as strings.

  std::map<char, std::string> natoAlphabet = {

    {'A', "Alfa"},   {'B', "Bravo"},    {'C', "Charlie"},
    {'D', "Delta"},  {'E', "Echo"},     {'F', "Foxtrot"},
    {'G', "Golf"},   {'H', "Hotel"},    {'I', "India"},
    {'J', "Juliet"}, {'K', "Kilo"},     {'L', "Lima"},
    {'M', "Mike"},   {'N', "November"}, {'O', "Oscar"},
    {'P', "Papa"},   {'Q', "Quebec"},   {'R', "Romeo"},
    {'S', "Sierra"}, {'T', "Tango"},    {'U', "Uniform"},
    {'V', "Victor"}, {'W', "Whiskey"},  {'X', "Xray"},
    {'Y', "Yankee"}, {'Z', "Zulu"},     {'0', "Zero"}
  };

  // Declare the input & Open Terminal

  char ch;

  std::cin >> ch;

  // Created a variable that has a complicated type and find the element from declared characters in the array as strings.

  auto createdCode = natoAlphabet.find(ch);

  // Execute the If Statement that checks if inputed values will match the information in the natoAlphabet array.

  // If it does not match - output "Not recognized:".

  if (createdCode == natoAlphabet.end())

    std::cout << "Not recognized." << std::endl;

  else

    std::cout << createdCode->second << std::endl;
  
  return 0;
  
}

find returns an iterator. find返回一个迭代器。 When you are confused by types, I suggest to stay away from auto for a moment.当您对类型感到困惑时,我建议您暂时远离auto auto is not to ignore what the actual type is (at least to my understanding). auto不是忽略实际类型是什么(至少在我的理解中)。

std::map<char,std::string>::iterator iter = natoAlphabet.find(ch);

If find cannot find the element then it returns natoAlphabet.end() , the end iterator that refers to an element one past the last element.如果find找不到该元素,则返回natoAlphabet.end() ,即指向最后一个元素之后的元素的结束迭代器。 So actually it does not refer to an element in the map.所以实际上它并不是指 map 中的元素。 It is just used to denote the end of the map.它仅用于表示 map 的结束。 find uses it to indicate that the element was not found: find使用它来指示未找到该元素:

if (iter == natoAlphabet.end())
    std::cout << "Not recognized." << std::endl;

When the element is found, then the iterator refers to a std::pair<const char,std::string> , because thats the type of elements in a std::map<char,std::string> .当找到元素时,迭代器引用std::pair<const char,std::string> ,因为那是std::map<char,std::string>中元素的类型。 You get a reference to the key via iter->first and a reference to the mapped value via iter->second .您通过iter->first获得对键的引用,并通过iter->second获得对映射值的引用。 Hence,因此,

else
    std::cout << iter->second << std::endl;

Prints the std::string for the key ch when there is an element with that key in the map.当 map 中存在具有该键的元素时,打印键chstd::string

For more details I refer you to https://en.cppreference.com/w/cpp/container/map有关更多详细信息,请参阅https://en.cppreference.com/w/cpp/container/map

PS: Unless you need the map to be sorted you shoud rather use std::unorderd_map . PS:除非您需要对 map 进行排序,否则您应该使用std::unorderd_map

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM