0

Just a tought came in mind till now i have make maps of strings and vectors Like this map<int,int> m; map <int,vector<int>> m; map <string,vector<int>> m; and various combinations are possible with other data types also.

But what will happen If I do map <vector<int>,vector<int>> m; or map <vector<int>,vector<vector<int>>> m; etc.

I was solving a question leetcode in which this format could be helpfull https://leetcode.com/contest/biweekly-contest-90/problems/odd-string-difference/ I tried like this

class Solution {
public:
    string oddString(vector<string>& words) {
        map <vector<int>,vector<string>> m;
        for(auto i:words)
        {
            // m[{int(i[0]-i[1]), int(i[1]-i[2])}].push_back(i);
            vector<int> v;
            for(int j=1;j<i.size();j++)
            {
                v.push_back(int(i[j]-i[j-1]));
            }
        }
        for(auto i:m)
        {
            if(i.second.size() ==1)
            {
                return i.second[0];
            }
        }
        return "";
    }
};
  • 1
    What happened when you tried? You probably want to add an `int main()` so others can test. A Solution class alone is not a [mcve] – drescherjm Nov 04 '22 at 13:52
  • For what would it be helpful? What would it allow you to do? How would you use it and select one of the mapped vectors? – Yunnosch Nov 04 '22 at 13:53
  • The value type basically is irrelevant; You may not be able to use the `[]` in some cases though. As for the key type: As long as the type is comparable with objects of the same type, you can use `std::map` with the default compare... `std::vector` implements the comparison operators... – fabian Nov 04 '22 at 13:55
  • 1
    Nothing special will happen, you will just have `std::vector` as a key in a map. Default `operator<` for vector just compares all elements lexicographically. Doesn't sound efficient to me, but by all means you are allowed to use it. – pptaszni Nov 04 '22 at 13:55
  • This question's code/phrasing very likely came from one of many countless coding challenge/puzzle websites. They take advantage of people who want to learn C++ by offering coding puzzles based on arcane knowledge or programming tricks; combined with a claim that solving those useless coding puzzles makes anyone a C++ expert. This is untrue, of course, but these coding puzzles (that have no learning or real-world value) are unsolvable without certain, specific coding or math tricks. Everyone eventually figures out this scam, but only after a long time, with nothing to show it. – Sam Varshavchik Nov 04 '22 at 13:55
  • You're overcomplicating this. It's the same problem as "given a vector of integers where all except one are the same, find the odd one out", but with "difference vectors" instead of integers. – molbdnilo Nov 04 '22 at 14:10
  • *But what will happen If I do ... etc?* Assuming the parameter types meets all the `map` requirements, **it'll just work.** – Eljay Nov 04 '22 at 14:12
  • @sweenish The thing about C++ is that undefined behaviour means "I tried it, and got the result I expected" *doesn't* mean that what you did is supported by the language – Caleth Nov 04 '22 at 14:14
  • @Caleth That's a good point. I suppose what I'd prefer is a question more along the lines of "Should I do this?" over "Can I do this?" – sweenish Nov 04 '22 at 15:22

2 Answers2

3

It's perfectly fine to use std::vector<int> as a key in std::map but you'll need take a look at the requirements for a given type to be used as a key in std::map.

From the documentation:

std::map is a sorted associative container that contains key-value pairs with unique keys. Keys are sorted by using the comparison function Compare. Search, removal, and insertion operations have logarithmic complexity. Maps are usually implemented as red-black trees.

Emphasis mine

So the Key type must meet the Compare requirements.

By default, std::map uses std::less<Key>. std::less<std::vector<int>> or in other words std::vector::operator<() lexicographically compares the values in the vector.
If you want something else, you will have to provide your own compare function when instantiating the map:

std::map<std::vector<int>, my_value_type, my_compare_function> my_map; // Note the third parameter is the compare function.

Note: That being said, when we start to use containers as key type for a map, it often is a flag indicating a wrongly designed algorithm/software architecture.


Regarding the value type in a std::map, you can have anything you want.

Fareanor
  • 5,900
  • 2
  • 11
  • 37
1

Yes

The documentation for std::map states that you can use any type you provide a Compare() function for it of that it has the operator<.

This is the case for vector, so it will work.

That said, you need to think about the problem you are trying to solve. I can't think about any problem where having a map of vector keys would be a good solution for.

Mickaël C. Guimarães
  • 1,020
  • 2
  • 14
  • 32