Skip to content

Instantly share code, notes, and snippets.

@tobbez
Created September 15, 2010 12:17
Show Gist options
  • Save tobbez/580654 to your computer and use it in GitHub Desktop.
Save tobbez/580654 to your computer and use it in GitHub Desktop.
TDDC76: Lab 1.4
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct word_entry {
string word;
int count;
};
int main (int argc, char **argv)
{
vector<word_entry> v;
string input;
while (cin >> input) {
vector<word_entry>::iterator it = v.begin();
while (it != v.end() && (*it).word < input) {
++it;
}
if (it != v.end() && (*it).word == input) {
(*it).count++;
} else {
word_entry w = {input, 1};
v.insert(it, w);
}
}
for (vector<word_entry>::const_iterator it = v.begin(); it != v.end(); ++it) {
cout << (*it).word << ":\t" << (*it).count << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment