arranging maps from greatest to least based on their stored values in C++ -
ok kindof have 2 questions think they're sortof easy experienced programmer , similar. if bothers you, me 1 question & not other. have map(char, int) associates number of times character appears in string int value. problem i'm having can't figure out how print out associated values occuring least occuring. example, if type aabbbcddddd. a:2 b:3 c:1 d:5. i'm trying d:5 b:3 a:2 c:1. hope i'm explaining okay...
second question. wondering how 1 go making maps same thing above series of letters or numbers. example: string: 'aabbb001c1 ddd'... "aabbb", "c", , "ddd" seperate words. "001" , "1" numbers, not equal. tried using 2 seperate map(string, int) (one words 1 numbers), series cutting off when character that's not of "type" appeared, nothings working. technique or advice nice. here's code have far.
#include <iostream> #include <string> #include <sstream> #include <map> #include <stdio.h> #include <ctype.h> #include <algorithm> using namespace std; int main() { string word; getline(cin, word); map<char,int> charcount; map<string, int> strcount; map<string, int> numcount; //turning characters lower case transform(word.begin(), word.end(), word.begin(), ::tolower); //for loop count recurring characters (unsigned int i=0; i<word.size(); i++){ charcount[word[i]]++; } //having trouble here. i'm doing series of words & numbers string temp; string temp2; (unsigned int j=0; j<word.size(); j++){ if (isalpha(word[j])) temp = temp + word[j]; else{ wordcount[temp]++; temp2.clear(); } if (isdigit(word[j])) temp2 = temp2 + word[j]; else{ numcount[temp2]++; temp2.clear(); } } //print out chars (map<char, int>::iterator = charcount.begin(); != charcount.end(); ++it) cout << it->first << ": " << it->second << endl; //print out words (map<string, int>::iterator = wordcount.begin(); != wordcount.end(); ++it) cout << it->first << ": " << it->second <<endl; //print out numbers (map<string, int>::iterator = numcount.begin(); != numcount.end(); ++it) cout << it->first << ": " << it->second << endl; return 0; }
a std::map
sorted it's key, not it's value, , neither sorting mechanism nor values of keys can changed after map has been instantiated.
so map sorted char
, want display value -- different sorting order.
the simplest thing construct std::multimap
key number of occurrences (the value original map) , value character. using multimap
opposed map
allows have multiple characters same number of occurrences. when you're ready display values, copy map
keys , values multimap
, display contents of multimap
.
here's example (live demo):
#include <iostream> #include <iomanip> #include <map> #include <string> #include <utility> using namespace std; int main() { const string data = "foofaaster"; // populate map map <char, int> chars; (auto c = data.begin(); c != data.end(); ++c) chars [*c]++; // display occurances cout << "original data: '" << data << "'" << endl; multimap <int, char, greater <int>> counts; (auto c = chars.begin(); c != chars.end(); ++c) counts.insert (make_pair (c->second, c->first)); cout << "character counts:" << endl; (auto = counts.begin(); != counts.end(); ++it) cout << "\t" << it->first << ": '" << it->second << "'" << endl; return 0; }
as second question, if map's keys static , there's 1 value per key, can use std::map <std::string, int>
. otherwise, if subelements of key have thier own values, might consider data structure more appropriate task, such trie.
Comments
Post a Comment