4
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by Andreas5
0

Think about it first. If you have a string like so :
string sentence = "This is a sentence that needs to be splitted";
how would you go ahead and get word by word? How would you start? Use this as an exercise.

0

Map is a kind of hashtable isnt it? Anyway
I wanted to try so I came up with this:

#include "stdafx.h"
#include <map>
#include <string>
#include <iostream>
#include <fstream>

int main()
{
	using namespace std;
	map<string, int> m;
	string s = "Hello my name is Andreas.";
	for(string::const_iterator first = s.begin(), last = s.begin(); last != s.end(); last++) {
		if(*last == ' ' || last == s.end()-1) {
			if(*first == ' ') first++;
			string a(first, last);
			++m[a];
			first = last;
		}
	}

	for(map<string, int>::const_iterator it = m.begin(); it != m.end(); it++)
		cout << it->first << "\t" << it->second << endl;

	cin.get();
	return 0;
}

Edited by Andreas5: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.