c++ - Count the number of occurrences in a string vector -
I have a type of string that contains the following:
tpNums includes: 2 15 38 43 50 5 14 16 29 53 I am trying to take the contents of the vector and calculates the number of the number of vector in this number, in this case they all appear only once. ; The following code: std :: map & lt; Std :: string, std :: size_t & gt; result; Std :: for_each (start (tpNums), end (tpNums), [& amp;] (std :: string const & amp; s) {++ results [s];});
My question is, how do I output the contents of the resulting map?
Is my problem easy to solve or is this the only way?
Edit:
I have tried to:
std :: map & lt; Std :: string, std :: size_t & gt; result; Std :: for_each (start (tpNums), end (tpNums), [& amp;] (std :: string const ...) Results [s]; cout & lt;
Output: 1112
I do not think I am doing the right thing, I have tried in different ways, but In order to dump the contents of the map, you can see the general / map :: Send "post-text" itemprop = "text" Code> Loop: for (auto = results.cbegin (); it! = Results.cend (); ++ this) std :: cout & lt; & Lt; It-> First & lt; & Lt; "= & Gt;" & Lt; & Lt; It-> Second & lt; & Lt; '\ N';
Or, as @leemes is mentioned, just for
(auto & amp; pair: result ) Std :: cout & lt; & Lt; Pair.first & lt; & Lt; "= & Gt;" & Lt; & Lt; Pair.Second & lt; & Lt; '\ N';
And if you have a pre-C ++ 11 compiler, then for the / map :: end :
(Std :: map & lt; string, size_t & gt; :: iterator = this = result.bugin (); it! = Result.end (); ++ this) std :: cout & lt; & Lt; It-> First & lt; & Lt; "= & Gt;" & Lt; & Lt; It-> Second & lt; & Lt; '\ N';
Comments
Post a Comment