vector in C++ can be used as the key value instance code for map

  • 2020-05-26 09:45:56
  • OfStack

Since the project needs to find a corresponding result according to the state, the structure of map is adopted, but the state itself is relatively complex and exists in an vector. The last experience with map was that custom class types must be overloaded as key values < Operator, because map's quick lookup is based on a red-black tree build, the key values must be comparable to each other. So worried that vector might cause some errors as a key value of a class type, I wrote an example test. It turns out that vector can be used directly as the key value of map.


#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
int main()
{
  map<vector<int>, vector<int>> mm;
  vector<int> a, b, c;
  a.push_back(1);
  b.push_back(2);
  c.push_back(3);
  mm.insert(map<vector<int>, vector<int>>::value_type(a, b));
  mm.insert(map<vector<int>, vector<int>>::value_type(b, b));
  mm.insert(map<vector<int>, vector<int>>::value_type(c, b));
  mm.insert(map<vector<int>, vector<int>>::value_type(a, a));
  return 0;
}

The above is the site to introduce you C++ vector can be used as map key value example code, I hope to help you, if you have any questions welcome to leave me a message, this site will timely reply to you!


Related articles: