Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // split function
- vector<string> split(const string& s)
- {
- vector<string> ret;
- typedef string::size_type string_size;
- string_size i = 0;
- // invariant: we have processed characters [original value of i, i)
- while (i != s.size()) {
- // ignore leading blanks
- // invariant: characters in range [original i, current i) are all spaces
- while (i != s.size() && isspace(s[i]))
- ++i;
- // find end of next word
- string_size j = i;
- // invariant: none of the characters in range [original j, current j)is a space
- while (j != s.size() && !isspace(s[j]))
- j++;
- // if we found some nonwhitespace characters
- if (i != j) {
- // copy from s starting at i and taking j - i chars
- ret.push_back(s.substr(i, j - i));
- i = j;
- }
- }
- return ret;
- }
- The main should be something like :
- #include <iostream>
- #include <map>
- using namespace std;
- vector<string> split(const string& s);
- int main()
- {
- string s;
- map<string, int> counters; // store each word and an associated counter
- // read the input, keeping track of each word and how often we see it
- while (cin >> s)
- ++counters[s];
- // write the words and associated counts
- for (map<string, int>::const_iterator it = counters.begin(); it != counters.end(); ++it) {
- cout << it->first << "\t" << it->second << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement