is this unconventional or bad form? (cont..)

The previous thread wouldn't let me edit so here is the code I was posting.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <iostream>
#include <vector>
#include <string>
using namespace std;

int mostFreq(vector<string> v) {
	int num1 = 1;
	int max = 1;
	for(unsigned int i = 0; i < v.size(); ++i) {
		unsigned int d = 0;
		if (num1 > max)
			max = num1;
		num1 = 1;
		while(d < v.size() ) {
			if(i != d) {
				if(v[i] == v[d]) {
					num1 += 1;
					v.erase(v.begin()+d);
					d = 0;
				}
			} ++d;
			if (v.size() == 1)
				if (num1 > max)
					max = num1;
		}
	}
	if (num1 > max)
		max = num1;
	return max;
}

vector<string> ddVect(vector<string> v){
	for (unsigned int i = 0; i < v.size(); ++i){
		unsigned int d = 0;
		while (d < v.size() ){
			if (i != d) {
				while (v[i] == v[d] && d < v.size() ){
					v.erase (v.begin()+d);
				}
			} ++d;
		}
	}
	return v;
}


int main() {
	string s;
	vector<string> word;
	cout << "Enter a sentence: ";
	getline(cin, s);
	size_t found;
		size_t found1;
		unsigned int spacecheck = 0;
		found = s.find_first_not_of(" ");
		for(unsigned int i = 0; i < s.length(); ++i){
			if(s.substr(i,1) == " ")
				break;
			else
				spacecheck += 1;
				if (spacecheck == s.length() )
					word.push_back(s);
		}
		if (word.size() == 0) {
			found1 = s.find_first_of(" ");
			while(found1 != string::npos){
				word.push_back(s.substr(found,found1-found) );
				found = found1 + 1;
				found1 = s.find_first_of(" ", found1 + 1);
			}
			found1 = string::npos;
			word.push_back(s.substr(found, found1) );
		}

	vector<string> word1 (word);
	vector<string> word2 (word);
	int MAX = mostFreq(word1);
	word1 = ddVect(word1);
	unsigned int const col = MAX;
	unsigned int const row = word1.size();
	string snt[row][col];
	for(unsigned int r = 0; r < row; ++r){
		int count = 1;
		unsigned int d = 0;
			while(d < word2.size() ){
				if(r != d){
					if(word2[r] == word2[d]){
						count += count;
						word2.erase(word2.begin()+d);
						d = 0;
					}
				}++d;
			}
	int i = 0;
	while (i < count){
		snt[r][i] = word2[r] + " ";
		++i;
		}
	}

	for(unsigned int r = 0; r < row; ++r) {
		for (unsigned int c = 0; c < col; ++c) {
			cout << snt[r][c];
		}
		cout << endl;
	}


	return 0;
}
Could you explain what are your program doing?
Sure thing. I wanted to count the frequency of each unique word in the input. The way I initially tried to solve this was to move the words into a two dimensional array.

The problem I had was that I didn't know you couldn't have mixed data types in the array! I ended up being able to print out my strings or my counts. Since that was no good I built a simple struct that contained a string and an int and put it in a 1d array.

I posted this code because I was replying to the old thread here: http://www.cplusplus.com/forum/beginner/78018/

At the bottom, the end of my code was cut off and it wouldn't let me edit it so I decided to post it here.

This code was simply the product of me reaching a dead end, but I was posting it in response to someone who was helping me out a great deal.
The simplest way is to use standard container std::map<std::string, int>

For example

1
2
3
4
5
6
7
8
9
10
11
12
13
std::string s;

std::cout << "Enter a sentence: ";
std::getline( std::cin, s );

std::istringstream is( s );

std::string word;

std::map<std::string, int> m;

while ( is >> word ) m[word]++;



So now the map contains frequencies of all words in the sentence.

If to add printing of the result

1
2
3
4
5
std::for_each( m.cbegin(), m.cend(),
	           []( const std::pair<const std::string, int> &p )
               { 
				   std::cout << p.first << ' ' << p.second << std::endl;
               } );


then the following output could be

Enter a sentence: 1 1 1 2 2 3 4 4 5 5 5 6 6 6 6 7
1 3
2 2
3 1
4 2
5 3
6 4
7 1
Last edited on
Everyday, I am surprised by the courtesy of everyone on these forums.

Thank you very much for showing me that Vlad. I will study what you have taught me and become proficient at it.

You have always been a tremendous help to me, and I very much appreciate it!
Topic archived. No new replies allowed.