c++ 如何在std::map中循环?

q3aa0525  于 2024-01-09  发布在  其他
关注(0)|答案(8)|浏览(158)

我想遍历map<string, int>中的每个元素,而不知道它的任何string-int值或键。
到目前为止,我有:

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

字符串

j8yoct9x

j8yoct9x1#

你可以像下面这样实现:

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

字符串
对于 C++11(及以后),

  1. for (auto const& x : symbolTable)
  2. {
  3. std::cout << x.first // string (key)
  4. << ':'
  5. << x.second // string's value
  6. << std::endl;
  7. }


C++17(及以后)中,

  1. for (auto const& [key, val] : symbolTable)
  2. {
  3. std::cout << key // string (key)
  4. << ':'
  5. << val // string's value
  6. << std::endl;
  7. }

展开查看全部
hrysbysz

hrysbysz2#

尝试以下

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

字符串
同样的代码也可以用普通的for循环来写

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


请注意,std::map的value_type是通过以下方式定义的

  1. typedef pair<const Key, T> value_type


因此,在我的示例中,p是对value_type的常量引用,其中Key是std::string,T是int
另外,如果将函数声明为

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

展开查看全部
nom7f22z

nom7f22z3#

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

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

字符串
或者使用C++11,使用基于范围的:

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

展开查看全部
umuewwlo

umuewwlo4#

由于P0W已经为每个C++版本提供了完整的语法,我想通过查看您的代码来添加更多要点

  • 始终将const &作为参数,以避免同一对象的额外副本。
  • 使用unordered_map,因为它总是使用起来更快。参见this discussion

下面是一个示例代码:

  1. #include <iostream>
  2. #include <unordered_map>
  3. using namespace std;
  4. void output(const auto& table)
  5. {
  6. for (auto const & [k, v] : table)
  7. {
  8. std::cout << "Key: " << k << " Value: " << v << std::endl;
  9. }
  10. }
  11. int main() {
  12. std::unordered_map<string, int> mydata = {
  13. {"one", 1},
  14. {"two", 2},
  15. {"three", 3}
  16. };
  17. output(mydata);
  18. return 0;
  19. }

字符串

展开查看全部
hwamh0ep

hwamh0ep5#

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

  1. typedef pair<const Key, T> value_type

字符串
这意味着如果你想用一个更明确的类型说明符来替换关键字auto,那么你可以这样做;

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


只是为了理解在这种情况下auto将转换为什么。

sg24os4d

sg24os4d6#

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

  1. typedef std::map<int, int> Map;
  2. Map mymap;
  3. mymap['a']=50;
  4. mymap['b']=100;
  5. mymap['c']=150;
  6. mymap['d']=200;
  7. bool itexist = false;
  8. int sizeMap = static_cast<int>(mymap.size());
  9. auto it = mymap.begin();
  10. for(int i = 0; i < sizeMap; i++){
  11. std::cout << "Key: " << it->first << " Value: " << it->second << std::endl;
  12. it++;
  13. }

字符串

展开查看全部
vngu2lb8

vngu2lb87#

如果你只是想在不改变值的情况下覆盖内容,请执行:

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

字符串
如果你想修改map的内容,删除const并保留&(如果你想直接修改容器内的内容)。如果你想使用容器值的 * 副本 *,也删除&符号;之后,你可以通过在“variable_name”上使用.first.second来访问它们。

t2a7ltrp

t2a7ltrp8#

其他方式:

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

字符串

相关问题