简体   繁体   English

使用pair作为映射中的键(C++/STL)

[英]Using pair as key in a map (C++ / STL)

I want to use a pair from STL as a key of a map.我想使用 STL 中的一对作为映射的键。

#include <iostream>
#include <map>

using namespace std;

int main() {

typedef pair<char*, int> Key;
typedef map< Key , char*> Mapa;

Key p1 ("Apple", 45);
Key p2 ("Berry", 20);

Mapa mapa;

mapa.insert(p1, "Manzana");
mapa.insert(p2, "Arandano");

return 0;

}

But the compiler throw a bunch of unreadable information and I'm very new to C and C++.但是编译器会抛出一堆不可读的信息,我对 C 和 C++ 很陌生。

How can I use a pair as a key in a map?如何在地图中使用一对作为键? And in general How can I use any kind of structure (objects, structs, etc) as a key in a map?总的来说,我如何使用任何类型的结构(对象、结构等)作为映射中的键?

Thanks!谢谢!

std::map::insert takes a single argument: the key-value pair, so you would need to use: std::map::insert接受一个参数:键值对,因此您需要使用:

mapa.insert(std::make_pair(p1, "Manzana"));

You should use std::string instead of C strings in your types.您应该在类型中使用std::string而不是 C 字符串。 As it is now, you will likely not get the results you expect because looking up values in the map will be done by comparing pointers, not by comparing strings.就像现在一样,您可能不会得到预期的结果,因为在映射中查找值将通过比较指针来完成,而不是通过比较字符串来完成。

If you really want to use C strings (which, again, you shouldn't), then you need to use const char* instead of char* in your types.如果您真的想使用 C 字符串(同样,您不应该这样做),那么您需要在类型中使用const char*而不是char*

And in general How can I use any kind of structure (objects, structs, etc) as a key in a map?总的来说,我如何使用任何类型的结构(对象、结构等)作为映射中的键?

You need to overload operator< for the key type or use a custom comparator.您需要为键类型重载operator<或使用自定义比较器。

Here's a working rewrite of the code in question:这是对相关代码的有效重写:

#include <map>
#include <string>

class Key
{
  public: 
    Key(std::string s, int i)
    {
      this->s = s;
      this->i = i;
    }
    std::string s;
    int i;
    bool operator<(const Key& k) const
    {
      int s_cmp = this->s.compare(k.s);
      if(s_cmp == 0)
      {
        return this->i < k.i;
      }
      return s_cmp < 0;
    }
};

int main()
{


  Key p1 ("Apple", 45);
  Key p2 ("Berry", 20);

  std::map<Key,std::string> mapa;

  mapa[p1] = "Manzana";
  mapa[p2] = "Arandano";

  printf("mapa[%s,%d] --> %s\n",
    p1.s.c_str(),p1.i,mapa.begin()->second.c_str());
  printf("mapa[%s,%d] --> %s\n",
    p2.s.c_str(),p2.i,(++mapa.begin())->second.c_str());

  return 0;
}

Alternatively to what James McNellis stated:替代詹姆斯麦克内利斯所说的:

mapa.insert(std::make_pair(p1, "Manzana"));

you could use mapa.insert({p1, "Manzana"});你可以使用mapa.insert({p1, "Manzana"});

This is a similar version of what you want to do, just change the data types, that's all.这是您想要做的类似版本,只需更改数据类型,仅此而已。 Also, use a c++ string, not the one we use in c.另外,使用 c++ 字符串,而不是我们在 c 中使用的字符串。

#include<bits/stdc++.h>
using namespace std;
#define  ll long long int
typedef pair<ll,ll> my_key_type;
typedef map<my_key_type,ll> my_map_type;
int  main()
{
    my_map_type m;
    m.insert(make_pair(my_key_type(30,40),6));
}   

std::map::emplace是你的朋友。

mapa.emplace(p1, "Manzana");

This is will do exactly what you want这将完全符合您的要求

#include<bits/stdc++.h>
using namespace std;
int main()
{
    map<pair<string, long long int>, string> MAP;
    pair<string, long long int> P;
    MAP.insert(pair<pair<string, long long int>, string>(pair<string, long long int>("Apple", 45), "Manzana"));
    MAP.insert(pair<pair<string, long long int>, string>(pair<string, long long int>("Berry", 20), "Arandano"));
    P = make_pair("Berry", 20);
    //to find berry, 20
    cout<<MAP[P]<<"\n";
    return 0;
}

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

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