输入地图值的标准输入

Enter standard input for values of map

本文关键字:标准输入 地图 输入地 输入      更新时间:2023-10-16

我正在尝试编写一个映射,该映射以一个整数为键,然后通过标准输入提供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)); 

编辑:为了澄清,以防我引起一些困惑,我试图用标准输入的数字填充地图。

我建议您浏览此处的std::map文档。然后看一下insert()方法文档中提供的示例。

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

#include <map>
using namespace std;
map<int,int> myMap;

如果您想要插入密钥/值对:

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

上面的内容可以用一些typedefs做得更简洁一些:

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));
}
#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";
}
}

这很好用。

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

解决方案这里我提供了代码,它将接受std输入并将其存储到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;
}`

这可能很有帮助。

#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
#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;
}