Tuesday, 3 September 2013

Insert each word of string into vector and calculating occurrence of each word

Insert each word of string into vector and calculating occurrence of each
word

I am trying to insert words of string into vector and then getting it's
occurrence. I have done significant work here. Problem is inplace of
calculating word below code count each character occurrence.
Probably I am inserting characters into vector rather word. I tried to get
solution from google but not getting what I want.
#include <iostream>
#include <algorithm>
#include <map>
#include <cstring>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <unordered_map>
std::pair < int, std::string > mytransform(const std::pair < std::string,
int >p)
{
return std::pair < int, std::string > (p.second, p.first);
}
int main(int argc, char *argv[])
{
std::string result_string=argv[1];
std::istringstream iss;
std::multimap < int, std::string, std::greater < int >>dst;
std::multimap < int, std::string >::iterator rec;
typedef std::unordered_map<std::string,int> occurrences;
occurrences s1;
std::vector<std::string> most;
std::map < std::string, int >src;
std::vector<std::string> writable(result_string.size() + 1);
std::copy(result_string.begin(), result_string.end(), writable.begin());
for (std::vector < std::string >::iterator it = writable.begin(); it
!= writable.end(); it++)
++src[*it];
std::transform(src.begin(), src.end(), std::inserter(dst,
dst.begin()), mytransform);
std::multimap < int, std::string >::iterator it = dst.begin();
for (it = dst.begin(); it != dst.end(); ++it)
std::cout << it->second << ":" << it->first << std::endl;
return 0;
}
Output:
technoworld@ubuntu:~/Videos/LinSocket/Modular$ ./q "one one two"
o:3
:2
e:2
n:2
t:1
w:1
:1
Rather it should be:
technoworld@ubuntu:~/Videos/LinSocket/Modular$ ./q "one one two"
one:2
two:1

No comments:

Post a Comment