Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <fstream>
- #include <string>
- #include <set>
- #include <map>
- typedef std::vector<std::string> VStr;
- typedef std::multimap<std::string, std::string> MStr;
- std::string WordSort(std::string word)
- {
- std::sort(word.begin(), word.end());
- return word;
- }
- int main()
- {
- std::ifstream stream("words.txt");
- VStr all;
- std::string line;
- while (std::getline(stream, line))
- {
- all.push_back(line);
- }
- std::cout << "----- Anagram List -----" << std::endl << std::endl;
- VStr anagramWords;
- MStr sortedWords;
- //creation of the multimap with sorted words, will be used to check for the existance of anagrams
- for (VStr::iterator it = all.begin(); it != all.end(); ++it)
- {
- std::string value = *it;
- std::string key = WordSort(value);
- sortedWords.insert ( std::pair<std::string, std::string>(key, value));
- }
- //checking for anagrams
- for (VStr::iterator it = all.begin(); it != all.end(); ++it)
- {
- std::string searchFor = *it;
- searchFor = WordSort(searchFor);
- if (sortedWords.count(searchFor) > 1)
- {
- std::pair <std::multimap<std::string, std::string>::iterator, std::multimap<std::string, std::string>::iterator> ref;
- ref = sortedWords.equal_range(searchFor);
- for (MStr::iterator tick = ref.first; tick != ref.second; ++tick)
- {
- std::cout << ' ' << tick->second << std::endl;
- }
- MStr::iterator id = sortedWords.find(searchFor);
- std::cout << "\n";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement