return type of v[i].size() where vector v is vector<string>

i was making a programme in which i had a vector<string> i need to return the string in this vector having longest length so i simply write this loop
1
2
3
4
5
int max=-1;
for(int i=0;i<v.size();i++){
   if(v[i].size()>max){ max=v[i].size(); sol=v[i];}
}
return sol;


but is was not entering in the if statement
thn i tried this statement
int num=-1;
if(v[0].size()>num) cout<<"we";
and it gives this error "invalid use of member (did you forget the ‘&’ "
can anyon please tell me where is the error
return type of v[i].size() is int and i am simply comparing it with int.where is the problem??
Last edited on
vector::size return a size_t, which is an unsigned int. You need to declare max and i as size_t.
closed account (z05DSL3A)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    vector<string> v(4,"test");
    string sol;

    size_t max = 0;
    for(size_t i=0; i < v.size(); i++)
    {
        if(v[i].size() > max)
        { 
            max=v[i].size();
            sol = v[i];
        }
    }
    std::cout << sol << " (" << max << ")" << std::endl;

    return 0; 
}


Edit:
slow again.
Last edited on
vector<>::size() returns a vector<>::size_type, which might be a size_t.

However, OP's code was v[0].size(). This is attempting to call size() on string.
The correct function call is v[0].length(). Note that string::length() returns a
string::size_type.
closed account (z05DSL3A)
http://www.cplusplus.com/reference/string/string/size/

The OP has not given the full story anyway, as it is obviously a function (with return sol;) and we don't know the full details of v.

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


std::string LongestIn(std::vector<std::string> const &vec) 
{
    std::string sol;
    std::string::size_type max = 0;

    for(std::vector<std::string>::const_iterator itr = vec.begin(); itr != vec.end(); ++itr)
    {
        if(itr->size() > max)
        { 
            max = itr->size();
            sol = *itr;
        }
    }
    return sol;
}

int main()
{
    std::vector<std::string> aStringVector;

    aStringVector.push_back("Mary");
    aStringVector.push_back("had");
    aStringVector.push_back("a");
    aStringVector.push_back("little");
    aStringVector.push_back("lamb");

    std::string longestString = LongestIn(aStringVector);
    
    std::cout << longestString << " (" << longestString.length() << ")" << std::endl;

    return 0; 
}
Last edited on
My obligatory one-liner:

1
2
3
4
std::vector<std::string>::const_iterator largest =
    std::max_element( aStringVector.begin(), aStringVector.end(),
    boost::bind( &std::string::length, _1 ) < boost::bind( &std::string::length, _2 ) );
std::cout << *largest << " (" << largest->length() << ")" << std::endl;


EDIT: of course the cout assumes largest != aStringVector.end(), which would be the case if the vector were empty to begin with.
Last edited on
closed account (z05DSL3A)
Now that's just showing off. :0)
Topic archived. No new replies allowed.