在散列映射中搜索,其中键可以是单个字符串或空格分隔的字符串

时间:2016-01-11 12:42:15

标签: c++ string stl hashmap

我必须创建一个hashmap,其密钥以'firstname lastname'或'firstname'的格式存储,值为整数。然后我必须在收到输入后直到文件结束时执行搜索操作。如果您看到,请在我的程序中建议编辑处理空格分隔的字符串并指出任何其他错误。

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


int main() {
    long n;
    map<string,int> m;
    cin>>n;
    for(long i=0;i<n;i++){
        string name;
        int phone;
        cin>>name;
        cin>>phone;
        m.insert(pair<string,int>(name,phone));
    }
    string query;
   while(cin>>query){
        map<string,int>::iterator p;
        p=m.find(query);
        if(p!=m.end())
            cout<<p->first<<"="<<p->second<<"\n";
        else
            cout<<"Not found\n";
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

很明显,您只需要检查第二个输入是否为整数。

所以你可以使用它:

constructor(){
    this.contentSms = this.form.find(".content-sms > p");

    this.contentSms.on("keyup", this.onTypingEditor);
}

//event on keyUp of editor 
onTypingEditor(event){
    event.preventDefault();
    let keyCode = event.keyCode;

    //if space typed
    if (keyCode === 32) {
        //get the selected node
        let selectedNode = this.getElementNodeSelected();
        //if selected node is a span tag
        if (this.isHighLightNode(selectedNode)){
            //execute this method
            this.selectAfterNode(selectedNode);
        }
    }
}

/*
here is the method that i create to try select the next node.
there is an object this.rangeEditor, it is the range object of
my editor
*/
selectAfterNode(node){
    let sel = rangy.getSelection();

    this.rangeEditor.selectNode(node);
    let nextNode = this.rangeEditor.commonAncestorContainer.nextSibling;
    this.rangeEditor.selectNode(nextNode);
    this.rangeEditor.collapse(1);

    sel.addRange(this.rangeEditor);
}

请注意,通常phoneNumbers不是整数,因此您可能希望将字符串用于phoneNumbers。