简体   繁体   English

我尝试在问题中使用 map 但似乎错误我找不到问题。 你可以帮帮我吗?

[英]I tried to use map in problem but it seems wrong i can t find problem. Could you help me?

#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    map<string,string> mym;
    int n;
    string s,a,r;
    cin >> n;
    for(int i=0; i<n; i++){
        getline(cin,s);
        getline(cin,a);
        mym.insert(pair<string,string>(s,a));
        s.erase();
        a.erase();
    } 
    for(int i=0; i<n; i++){
        getline(cin,r);
        if(mym.count(r)!=0){
            cout << r << '=' << mym.at(r) <<endl;
        }
        else cout << "Not found" << endl;
        r.clear();
    } 
   return 0;
}

I tried to use map in problem but it seems wrong i can t find problem.我尝试在问题中使用 map 但似乎错误我找不到问题。 Could you help me?你可以帮帮我吗?

https://www.hackerrank.com/challenges/30-dictionaries-and-maps/problem https://www.hackerrank.com/challenges/30-dictionaries-and-maps/problem

input输入

sam 99912222 tom 11122222 harry 12299933 sam edward harry山姆 99912222 汤姆 11122222 哈利 12299933 山姆·爱德华·哈利

my result我的结果

Not found =sam 99912222 =sam 99912222未找到 =sam 99912222 =sam 99912222

This line:这一行:

cin >> n;辛 >> n;

Will consume the number of the input, but won't consume the subsequent end of line that follows before the next string.将消耗输入的数量,但不会消耗下一个字符串之前的后续行尾。

So instead of cin >> n , do this:所以代替cin >> n ,这样做:

string tmp;
getline(cin, tmp);
n = atoi(tmp.c_str());

There's probably other ways to do this.可能还有其他方法可以做到这一点。

Some other code cleanup as follows:其他一些代码清理如下:

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    map<string,string> mym;
    int n;
    string tmp;

    cin >> tmp;
    n = atoi(tmp.c_str());

    for(int i=0; i<n; i++) {
        string s, a;
        getline(cin,s);
        getline(cin,a);
        mym[s] = a;
    } 
    for(int i=0; i<n; i++) {
        string r;
        getline(cin,r);
        auto itor = mym.find(r);
        if(itor != mym.end())
            cout << r << '=' << itor->second <<endl;
        }
        else {
           cout << "Not found" << endl;
        }
    } 
   return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我找不到什么问题。 莫吉烤,unicode - i can't find what is problem. mojibake, unicode 您好,我的代码中有语法问题。 你能帮助我吗? - Greetings, I have a syntax problem in my code. Can you help me? C++ 无法解决我的问题。 我有控制台错误 - C++ can't solve my problem. I have console error 无法理解问题的要求。 (ProjectEuler 问题:11) - Can't understand the requirement of the problem. (ProjectEuler problem: 11) 链表 - AppendLastNToFirst (C++),我只写了问题的逻辑。 请告诉我更正 - Linked list - AppendLastNToFirst (C++), I have written only the logic for the problem. Please tell me the correction C ++递归段错误。 你能帮我看看我做错了什么吗? - C++ recursion segfault. Can you help me see what I'm doing wrong? Objective C和c ++导入问题。 很简单,但我不明白 - Objective C and c++ import problem. Simple but I don't get it JNI - Class 加载问题。 DefineClass function 似乎不起作用 - JNI - Class loading problem. DefineClass function doesn't seems to work 我不确定这个问题上的几件事。 - I'm not sure about a few things on this problem. 提交 Kattis “OddGnome”问题时出错。 我在本地得到正确的输出,我的代码有什么问题? C++ - Getting error on submission for Kattis "OddGnome" problem. I am getting the right output locally, what is wrong with my code? C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM