Given a sequence of words , print all anagrams together using STL | C ++ program for finding all anagram pairs in the given array ; Utility function for printing anagram list ; Utility function for storing the vector of strings into HashMap ; sort the string ; make hash of a sorted string ; print utility function for printing all the anagrams ; Driver code ; initialize vector of strings ; utility function for storing strings into hashmap
#include <algorithm> NEW_LINE #include <iostream> NEW_LINE #include <unordered_map> NEW_LINE #include <vector> NEW_LINE using namespace std ; void printAnagram ( unordered_map < string , vector < string > > & store ) { for ( auto it : store ) { vector < string > temp_vec ( it . second ) ; int size = temp_vec . size ( ) ; for ( int i = 0 ; i < size ; i ++ ) cout << temp_vec [ i ] << " ▁ " ; cout << " STRNEWLINE " ; } } void storeInMap ( vector < string > & vec ) { unordered_map < string , vector < string > > store ; for ( int i = 0 ; i < vec . size ( ) ; i ++ ) { string tempString ( vec [ i ] ) ; sort ( tempString . begin ( ) , tempString . end ( ) ) ; store [ tempString ] . push_back ( vec [ i ] ) ; } printAnagram ( store ) ; } int main ( ) { vector < string > arr ; arr . push_back ( " geeksquiz " ) ; arr . push_back ( " geeksforgeeks " ) ; arr . push_back ( " abcd " ) ; arr . push_back ( " forgeeksgeeks " ) ; arr . push_back ( " zuiqkeegs " ) ; arr . push_back ( " cat " ) ; arr . push_back ( " act " ) ; arr . push_back ( " tca " ) ; storeInMap ( arr ) ; return 0 ; }