简体   繁体   English

在地图中按向量搜索关键字

[英]Search for key by vector in map

So, we have a school-project in creating a phonebook where you should be able to look up phone numbers by searching for the name. 因此,我们有一个学校项目在创建电话簿,您应该可以通过搜索该名称来查找电话号码。 I decided to use a map with a string for the phone number and and a vector of strings for the name, due associated number should be able to have multiple names in it. 我决定使用带有字符串作为电话号码和字符串向量作为名称的映射,因为关联的号码应该可以在其中包含多个名称。

However, due to us jumping straight from Python to C++ without any explanation of the syntax or the language, I am having a hard time coming up with a way to look for the number by searching for names. 但是,由于我们直接从Python跳转到C ++,而没有任何语法或语言解释,因此我很难找到一种通过搜索名称来查找数字的方法。

The class I am using looks like this 我正在使用的班级看起来像这样

class Telefonbok
{
    public:
        void add(string namn, string nummer)
        {
            map<string, vector<string>>::iterator it = boken.find(nummer);
            if (it != boken.end())
            {
                cout << "This number already exists, please choose another";
            }
            else
            {
                namn_alias.push_back(namn);
                boken[nummer] = namn_alias;
            }
        }
        void lookup(string name)
        {
            for (map<string, vector<string>>::iterator sokning = boken.begin(); sokning != boken.end(); sokning++)
                cout << "Hello!";
        }
    private:
        vector<string> namn_alias;
        string nummer;
        map<string, vector<string>> boken;
};

What I am trying to do in lookup function is to search for a phone number by the names in the vector, but I am stumped on how to proceed with looking through the vector inside the for-loop. 我试图在查找功能中执行的操作是通过引导程序中的名称搜索电话号码,但是我对如何继续在for循环中查找引导程序感到困惑。

The plan was to go through the Map keys one by one to find the vector that contains the searched-for name. 计划是一个一个地通过Map键来查找包含搜索名称的向量。 Any tips on how to proceed or some functions I have missed that can be used for this? 关于如何进行操作的任何提示或我错过的一些功能可用于此目的?

Algirdas is correct, you should read up on C++. Algirdas是正确的,您应该阅读C ++。

Assuming you are mapping name to 1-or-more numbers, but only 1 number per name... 假设您将名称映射到一个或多个数字,但每个名称仅映射一个数字...

#include <cstddef>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using std::cout;
using std::endl;
using std::map;
using std::string;
using std::vector;

class Telefonbok
{
public:
  void add(string namn, string nummer) {
    auto it = nummer_namn.find(nummer);
    if (it != nummer_namn.end()) {
      cout << "This number already exists, please choose another" << endl;
    }
    else {
      nummer_namn[nummer] = namn;
      namn_nummer[namn].push_back(nummer);
    }
  }

  void lookup(string name) {
    auto it = namn_nummer.find(name);
    if (it == namn_nummer.end()) {
      cout << "Unable to find any numbers for " << name << ", sorry." << endl;
      return;
    }

    for (auto const& sokning : it->second)
      cout << name << " : " << sokning << endl;
  }
private:
  map<string, vector<string>> namn_nummer;
  map<string, string> nummer_namn;
};

int main() {
  Telefonbok bok;
  bok.add("Eljay", "789");
  bok.add("Eljay", "456");
  bok.add("Beaker", "123");

  bok.lookup("Eljay");
  bok.lookup("Beaker");
  bok.lookup("Bunsen Honeydew");

  return EXIT_SUCCESS;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM