【问题标题】:Enter standard input for values of map输入地图值的标准输入
【发布时间】:2018-04-28 09:10:06
【问题描述】:

我正在尝试编写一个映射,该映射以整数作为键,然后通过标准输入提供给它的 x 数量的 int 值。这是我第一次使用地图,所以我遇到了几个问题,想知道是否有人可以帮助更好地理解我做错了什么。通常使用向量/数组可以正常工作:

int standardInput;
for (int i = 1; i<=n; i++){
    cin standardInput;
    array[i] = standardInput;
}

在使用地图时,我无法让它以类似方式工作:

int numberToCompare = 4;
map<int numberToCompare, int>myMap;
    int standardInput;
    cout << "Enter numbers: " << endl;
    for (int i = 1; i <= n; i++){
        cin standardInput;
        myMap.insert(standardInput);
    }

我仍在尝试了解键和值。我在阅读地图时所理解的方式是,与多地图不同,地图的键是唯一的。我不知道该怎么做是允许用户输入来填充地图的其余部分。我在网上看到很多示例,其中人们在代码中手动输入所有输入,执行以下操作(这与我想要完成的事情背道而驰)。

portMap_.insert(pair<string,int>("fourth", 4444)); 
portMap_.insert(pair<string,int>("fifth", 5555)); 

编辑:为了澄清以防我引起一些混乱,我试图用通过标准输入给出的数字填充地图。

【问题讨论】:

  • 你能更清楚地解释你想要做什么吗?
  • @melak47 几乎只是想用 cin 给出的数字填充地图。我上面写的内容可以很好地填充数组/向量,但我不知道如何为地图做到这一点。

标签: c++ input map


【解决方案1】:

我建议您浏览找到 herestd::map 的文档。那就看看insert()方法文档here提供的例子吧。

声明一个map对象,你需要通过提供类型名称作为模板参数来指定键的类型和值的类型:

#include <map>
using namespace std;

map<int,int> myMap;

如果你想插入一个键/值对:

int myKey = 10;
int myVal = 100;
myMap.insert(pair<int,int>(myKey, myVal));

上面的内容可以通过一些 typedef 变得更简洁:

typedef map<int,int> IntMap;
typedef pair<int,int> IntPair;

IntMap myMap;
myMap.insert(IntPair(10, 100));

如果您希望用户输入提供键/值对,只需编写一个简单的循环来接受来自标准输入的值并将值插入到您的地图中。

这里有大量资源可用于从标准输入中读取值。像下面这样的东西可以解决问题:

// pseudo-code
while (did the user quit?)
{
    int key   = 0;
    int value = 0;

    cin >> key >> value;

    // maybe if the user enters -1, then you quit, otherwise:

    myMap.insert(pair<int,int>(key, value));
}

【讨论】:

    【解决方案2】:
    #include <bits/stdc++.h>
    using namespace std;
    
    int main() {
        int n;
        scanf("%d", &n);
        map<int, string> m;
        for(int i = 1; i <= n; i++) {
            string s = "abracadabra";
            m.insert(pair<int, string>(i, s));
        }
        for(auto it = m.begin(); it != m.end(); it++) {
            cout << it->first << " " << it->second <<"\n";
        }
    }
    

    这很好用。

    【讨论】:

      【解决方案3】:

      问题:编写一个以整数为键的映射,然后是通过标准输入提供给它的 x 数量的 int 值

      解决方案在这里我提供了代码,它将接受标准输入并将其存储到 MAP, 在此处输入代码

      `#include <cmath>
      #include <cstdio>
      #include <vector>
      #include <iostream>
      #include <algorithm>
      #include <map>
      #include <string>
      
      
      using namespace std;
      
      int main()
      {
          int n;
          int key;
          long x;
          cin >> n;//std input for number of entries in to MAP
          map <int, long> map_book;
          for (int i = 0; i < n; i++) {
              cin >> x;//std input for VALUE
              cin >> key;//std input for KEY
              map_book[key] = x;
          }
      
          //here am searching the x and pinting if it is there in MAP or Else priniting it is not found
          while(cin >> x) {
              if (map_book.find(x) != map_book.end()) {
                  cout << x << "=" << map_book.find(x)->second << endl;
              } else {
                  cout << "Not found" << endl;
              }
          }
          return 0;
      }`
      

      【讨论】:

        【解决方案4】:

        这会很有帮助。

        #include<iostream>
        #include<map>
        using namespace std;
        int main()
        {
            map<char,int>mp;
            char a;
            int b,n;
            cin>>n;
            for(int i = 0; i<n; i++){
                cin>>a>>b;
                mp.insert(pair<char,int>(a,b));
            }
            cout<<endl;
            for(auto&x:mp)
            {
                cout<<x.first<<" "<<x.second<<endl;
            }
            return 0;
        }
        

        输出:

        3
        a 1
        b 2
        c 3
        
        a 1
        b 2
        c 3
        

        【讨论】:

          【解决方案5】:
          #include <iostream>
          #include <map>
          
          int main ()
          {
            std::map<char,int> first;
          
            first['x']=8;
            first['y']=16;
            first['z']=32;
          
            for(map<char,int>::iterator it = first.begin(); it != first.end(); ++it) {
                  cout << it->first <<" "<< it->second<<endl;
              }
          
            return 0;
          }
          

          【讨论】:

            猜你喜欢
            • 2011-10-06
            • 2013-09-18
            • 1970-01-01
            • 2012-11-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多