Trying to compare the lenghts of strings in a vector

Hey everyone,

I realized you can get the length of a string in a vector by using something like:
1
2
3
4
5
6
	vector<string> words;
	words.push_back("cat");
	words.push_back("dog");
        words.push_back("mouse")
	for (unsigned int i = 0; i < words.size(); ++i)
		cout << word[i].length() << endl;


I tried writing a MAX function, but I can't seem to figure out which data types I should be using. I assumed at first that string.length() would yield an int, but when I wrote a function like:
1
2
3
4
5
6
int max(int w1, int w2) {
	if(w1 > w2)
		return w1;
	else
		return w2;
}


I couldn't call it with max(words[i].length(),words[i+1].length())

I've tried a few other formats and can't seem to get any to work. Is there a way to call a function to compare the lengths of different strings?

Thanks!

string::size_type is the type you're looking for.
While this is not required, the container size_types are usually equivalent to size_t (they are equivalent for standard containers used with standard allocators).
Last edited on
Thanks a lot Athar, I will try that and post my results.
Well I got the function to work, but for some reason my program terminates without error after my for loop when I use the function. Anybody know why this is happening? (When I comment out line 40, it runs to the end)
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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

size_t max(size_t w1, size_t w2) {
	if(w1 > w2)
		return w1;
	else
		return w2;
}


int main(){
	string s;
	vector<string> word;
	cout << "Enter a string: ";
	getline(cin, s);
	size_t found;
	size_t found1;
	found = s.find_first_not_of(" ");
	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;
	int count = 0;
	word.push_back(s.substr(found, found1));
	string sWord;
	cout << "What word would you like to search for?: ";
	cin >> sWord;
	size_t h;
	for (unsigned int i = 0; i < word.size(); ++i){
		if(word[i] == sWord)
			++count;
		h = max(word[i].length(), word[i+1].length());

	}
	cout << "The string with the most characters is " << word[h]<< endl;
cout << "\nThere are " << count << " instances of the word " << sWord << endl;

	return 0;
}
One thing I've noticed is my line 43 is wrong since h is the size of the string. I still wonder why it terminates after the for loop though...

Okay so here is my updated code. It will work great as long as it doesn't terminate after the "for loop". Why does it terminate? T^T

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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

bool isMax(size_t w1, size_t w2) {
	if(w1 > w2)
		return true;
	else
		return false;
}

int main(){
	string s;
	vector<string> word;
	cout << "Enter a string: ";
	getline(cin, s);
	size_t found;
	size_t found1;
	found = s.find_first_not_of(" ");
	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;
	int count = 0;
	word.push_back(s.substr(found, found1));
	string sWord;
	cout << "What word would you like to search for?: ";
	cin >> sWord;
	int max;
	for (unsigned int i = 0; i < word.size(); ++i){
		if(word[i] == sWord)
			++count;
		if (isMax(word[i].length(), word[i+1].length()))
			max = i;
		else
			max = i + 1;
	}
	cout << "The string with the most characters is " << word[max]<< endl;
	cout << "\nThere are " << count << " instances of the word " << sWord << endl;

	return 0;
}
Last edited on
Let's do this with an array to make it a bit simpler so that I can show you your problem:
1
2
3
int a[3] = { 1, 10, 100 };
for (int i = 0; i < 3; ++i)
  cout << a[i] << " , " << a[i+1] << endl;
1 , 10
10 , 100
100 , CRASH!

We're crashing because we're stepping outside of the bounds of the array.

The same thing is happening with your vector:
1
2
3
4
for (unsigned int i = 0; i < word.size(); ++i){
  if (isMax(word[i].length(), word[i+1].length())
    ...
}


If you're going to reference word[i+1], then you need to ensure that you don't overstep the limits of the array. Change your for loop to this:
for (unsigned int i = 0; i < word.size()-1; ++i){


Also, regarding your Max function. If you use a template function, then the type is completely irrelevant:
1
2
3
4
5
template <class T>
T isMax(T a, T b)
{
  return (a < b) ? b : a;
}


By using template<class T>, you've now defined a template. In the function following it, you can use T to represent any datatype, even custom classes!

The C version of this is to use macros like so: but those aren't really safe.
#define isMax(a,b) (((a)<(b))?(b):(a))
Stewbond, thank you so much for explaining that to me! I was at a total loss for what to do b/c I wasn't getting any compiler errors. Thanks to your detailed explanation, I totally understand now. I can't thank you enough.

Also, thank you cpp forums, you have aided my cpp learning tremendously!

I will be a great programmer one day so I can give back.

Topic archived. No new replies allowed.