I am sorry that I do not answer your question regarding the map approach.
But because I think that other approaches maybe better, I will show you a different solution.
I will use the standard approach to find out, if a word is an anagram of another. The standard procedure is: Sort the letters in words. If 2 words with sorted letters are the same, then we have a anagram.
Very simple example:
gartner --> aegnrrt
granter --> aegnrrt
So, what to do: We will make a copy of the original data. Additionally we store the index to the original data. Then, we sort the letters for all words in the copied list. And then, sort the list of words (with the sorted letters). As a result, the anagrams will be adjacent to each other.
In an additional loop, we will check the adjacent values and set a flag, if we found an anagram.
Then we show the result to the user.
Please see the attached code. Please note: It is one of many possibilities . . .
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
#include <iterator>
#include <iomanip>
// Random Word list
std::vector<std::string> words{"Lorem", "ipsum", "dolor", "sit", "amet", "consetetur", "sadipscing", "elitr", "sed", "diam", "nonumy", "eirmod", "tempor", "invidunt", "ut", "labore", "et", "dolore", "magna", "aliquyam", "erat", "sed", "diam", "voluptua", "At", "vero", "eos", "et", "accusam", "et", "justo", "duo", "dolores", "et", "ea", "rebum", "Stet", "clita", "kasd", "gubergren", "no", "sea", "takimata", "sanctus", "est", "Lorem", "ipsum", "dolor", "sit", "amet", "Lorem", "ipsum", "dolor", "sit", "amet", "consetetur", "sadipscing", "elitr", "sed", "diam", "nonumy", "eirmod", "tempor", "invidunt", "ut", "labore", "et", "dolore", "magna", "aliquyam", "erat", "sed", "diam", "voluptua", "At", "vero", "eos", "et", "accusam", "et", "justo", "duo", "dolores", "et", "ea", "rebum", "Stet", "clita", "kasd", "gubergren", "no", "sea", "takimata", "sanctus", "est", "Lorem", "ipsum", "dolor", "sit", "amet", "Lorem", "ipsum", "dolor", "sit", "amet", "consetetur", "sadipscing", "elitr", "sed", "diam", "nonumy", "eirmod", "tempor", "invidunt", "ut", "labore", "et", "dolore", "magna", "aliquyam", "erat", "sed", "diam", "voluptua", "At", "vero", "eos", "et", "accusam", "et", "justo", "duo", "dolores", "et", "ea", "rebum", "Stet", "clita", "kasd", "gubergren", "no", "sea", "takimata", "sanctus", "est", "Lorem", "ipsum", "dolor", "sit", "amet", "Duis", "autem", "vel", "eum", "iriure", "dolor", "in", "hendrerit", "in", "vulputate", "velit", "esse", "molestie", "consequat", "vel", "illum", "dolore", "eu", "feugiat", "nulla", "facilisis", "at", "vero", "eros", "et", "accumsan", "et", "iusto", "odio", "dignissim", "qui", "blandit", "praesent", "luptatum", "zzril", "delenit", "augue", "duis", "dolore", "te", "feugait", "nulla", "facilisi"};
// We want to copy the words into a list along with its index for sorting
struct WordWithIndex {
WordWithIndex() {};
WordWithIndex(const std::string&s, size_t i) : word(s), index(i) {};
std::string word{};
size_t index{};
bool isAnagram{false};
};
int main()
{
// Create a new vector and initialize the size
std::vector<WordWithIndex> wordWithIndex(words.size());
// COpy the word list into new vector
std::transform(
words.begin(), words.end(), wordWithIndex.begin(),
[i=0U](const std::string& s) mutable { return WordWithIndex(s,i++);}
);
// Sort the letters in each word. Words with sorted letters, that are equal are an anagram
std::for_each(wordWithIndex.begin(),wordWithIndex.end(),
[](WordWithIndex& wwi) {std::sort(wwi.word.begin(),wwi.word.end());});
// Now sort the word list with sort criteria : Words with sorted letters
std::sort( wordWithIndex.begin(), wordWithIndex.end(),
[](WordWithIndex& wwi1, WordWithIndex& wwi2) {return wwi1.word < wwi2.word;}
);
// Find anagrams
std::vector<WordWithIndex>::iterator wIter = wordWithIndex.begin();
while (wIter != wordWithIndex.end()) {
std::vector<WordWithIndex>::iterator adjacentFound = std::adjacent_find(
wIter, wordWithIndex.end(),[](const WordWithIndex &a, const WordWithIndex &b){ return a.word == b.word;});
if (adjacentFound != wordWithIndex.end()) {
adjacentFound->isAnagram = true;
++adjacentFound;
adjacentFound->isAnagram = true;
}
wIter = adjacentFound;
}
// Show result
for (size_t i=0U; i < words.size(); ++i)
std::cout << std::left << std::setw(4) << i << "\t" << std::setw(25) << words[wordWithIndex[i].index] << "\t\tIs Anagram: " << std::boolalpha << wordWithIndex[i].isAnagram << "\n";
return 0;
}