C++循环通过映射

C++ Loop through Map

本文关键字:映射 循环 C++      更新时间:2023-10-16

我想遍历map<string, int>中的每个元素,而不知道它的任何字符串int值或键。

到目前为止我所拥有的:

void output(map<string, int> table)
{
       map<string, int>::iterator it;
       for (it = table.begin(); it != table.end(); it++)
       {
            //How do I access each element?  
       }
}

您可以实现以下目标:

map<string, int>::iterator it;
for (it = symbolTable.begin(); it != symbolTable.end(); it++)
{
    std::cout << it->first    // string (key)
              << ':'
              << it->second   // string's value 
              << std::endl;
}

对于C++11(及以后)

for (auto const& x : symbolTable)
{
    std::cout << x.first  // string (key)
              << ':' 
              << x.second // string's value 
              << std::endl;
}

对于C++17(及以后)

for (auto const& [key, val] : symbolTable)
{
    std::cout << key        // string (key)
              << ':'  
              << val        // string's value
              << std::endl;
}

尝试以下

for ( const auto &p : table )
{
   std::cout << p.first << 't' << p.second << std::endl;
} 

同样可以使用普通的for loop 写入

for ( auto it = table.begin(); it != table.end(); ++it  )
{
   std::cout << it->first << 't' << it->second << std::endl;
} 

考虑到std::map的value_type是通过以下方式定义的

typedef pair<const Key, T> value_type

因此,在我的示例中,p是对value_type的常量引用,其中Key是std::string,T是int

此外,如果该函数被声明为会更好

void output( const map<string, int> &table );

mapvalue_type是一个pair,它分别包含作为firstsecond成员的键和值。

map<string, int>::iterator it;
for (it = symbolTable.begin(); it != symbolTable.end(); it++)
{
    std::cout << it->first << ' ' << it->second << 'n';
}

或者使用C++11,使用基于范围的

for (auto const& p : symbolTable)
{
    std::cout << p.first << ' ' << p.second << 'n';
}

正如来自莫斯科的@Vlad所说,考虑到std::mapvalue_type定义方式如下:

typedef pair<const Key, T> value_type

这意味着,如果您希望用更明确的类型说明符替换关键字auto,那么您可以这样做;

for ( const pair<const string, int> &p : table ) {
   std::cout << p.first << 't' << p.second << std::endl;
} 

只是为了理解auto在这种情况下会翻译成什么。

由于P0W为每个C++版本提供了完整的语法,我想通过查看您的代码来补充几点

  • 始终将const &作为参数,以避免同一对象的额外副本
  • 使用CCD_ 15,因为它使用起来总是更快。查看此讨论

这是一个示例代码:

#include <iostream>
#include <unordered_map>
using namespace std;
void output(const auto& table)
{
   for (auto const & [k, v] : table)
   {
        std::cout << "Key: " << k << " Value: " << v << std::endl;
   }
}
int main() {
    std::unordered_map<string, int> mydata = {
        {"one", 1},
        {"two", 2},
        {"three", 3}
    };
    output(mydata);
    return 0;
}

它甚至可以用经典的for循环来完成
手动推进迭代器。

typedef std::map<int, int> Map;
Map mymap;
mymap['a']=50;
mymap['b']=100;
mymap['c']=150;
mymap['d']=200;
bool itexist = false;
int sizeMap = static_cast<int>(mymap.size());
auto it = mymap.begin();
for(int i = 0; i < sizeMap; i++){
    std::cout << "Key: " << it->first << " Value: " << it->second << std::endl;
    it++;
}

如果您只想在不更改值的情况下迭代内容do:

for(const auto & variable_name : container_name(//here it is map name)){
    cout << variable_name.first << " : " << variable_name.second << endl; 
} 

如果要修改映射的内容,请删除const并保留&(如果要直接修改容器内的内容)。如果要使用容器值的副本,也可以删除&符号;之后,您可以在"上使用.first.second来访问它们;variable_name";。

其他方式:

map <int, string> myMap = {
    { 1,"Hello" },
    { 2,"stackOverflow" }
};
for (auto iter = cbegin(myMap); iter != cend(myMap); ++iter) {
    cout << iter->second << endl;
}