输入地图值的标准输入
Enter standard input for values of map
我正在尝试编写一个映射,该映射以一个整数为键,然后通过标准输入提供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;
}
相关文章:
- 从标准输入读取文件后读取用户输入
- 如何从标准输入中检测格式化输入错误?
- C++ - 标准输入环路
- 快速读取标准输入缓冲区,无需检查
- 将标准输入中的双精度存储在矢量中
- 如何将 getline 与标准输入一起使用?
- 从 C++ 中的标准输入迭代读取字节
- C++计算标准输入的字数
- 如何从 c++ 中的给定标准输入中获取每个字符的偏移量?
- 为什么此代码从标准输入中少一个输入?
- 如何在C++中从标准输入中获取变量输入
- C++ 从标准输入读取文件名
- 从标准输入读取多个类型时出现问题
- 如何在execl中使用标准输入'<'?
- 如何在 eclipse 中将标准输入从控制台更改为另一个程序
- 流对象来表示文件输入,然后是标准输入?
- 标准输入输出操作如何C++完成
- 从标准输入读取时zmq_poll不侦听信号
- 是否可以从标准输入指向指针的地址
- 输入地图值的标准输入