944,117 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 9846
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 22nd, 2005
0

vector<string> - any way to find longest string?

Expand Post »
If I've got a vector of strings, is there a quick one liner that gets me the longest (ie .size() ) of the string inside (or conversely, the shortest?)

The alternatives I've thought of are as follows, but they all seem inefficient:
- Keep a variable of the longest size as I insert each element and compare
- Iterate through the vector and compare

Any other ideas would be appreciated.
Last edited by winbatch; Dec 22nd, 2005 at 6:10 pm. Reason: (removed dumb alternative...)
Similar Threads
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Dec 22nd, 2005
0

Re: vector<string> - way to find longest string?

>The alternatives I've thought of are as follows, but they all seem inefficient
They seem inefficient, or they are inefficient? Chances are good that there's no performance issue and your time is better spent elsewhere.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 22nd, 2005
0

Re: vector<string> - way to find longest string?

As long as there is no built in function that I'm not aware of, I'll iterate through...
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Dec 23rd, 2005
0

Re: vector<string> - way to find longest string?

>As long as there is no built in function that I'm not aware of, I'll iterate through...
Don't confuse my comment with there not being another solution. However, you'll find that the pretty abstractions that you might use do exactly the same thing internally, so your concern about performance is pretty much moot.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 23rd, 2005
0

Re: vector<string> - way to find longest string?

Quote originally posted by Narue ...
>Don't confuse my comment with there not being another solution.
Ok, bug I'd rather not rewrite something that already exists, so are you aware of an existing solution provided by vector or algorithm in general?
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Dec 23rd, 2005
0

Re: vector<string> - way to find longest string?

Why not use a Set and specify the string size as a sorting criterion? That way, when you want to find the longest or shortest string it would be a one liner.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Dec 23rd, 2005
0

Re: vector<string> - way to find longest string?

This is my try at it. It may not be very good, but it works.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <string>
  4. #include <set>
  5. using std::cout;
  6. using std::endl;
  7.  
  8.  
  9. class Sort_By_Length
  10. {
  11. public:
  12. enum sort_mode {ascending, descending};
  13.  
  14. //constructor for sorting criterion
  15. //-default criterion uses value descending
  16. Sort_By_Length(sort_mode m=descending): mode(m) { }
  17.  
  18. //comparison of the elements
  19. bool operator() (const string& s1, const string& s2) const
  20. {
  21. return (mode == descending ? s1.length() > s2.length() : s2.length() > s2.length());
  22. }
  23.  
  24. //comparison of the sorting criterion
  25. bool operator== (const Sort_By_Length& sbl)
  26. {
  27. return mode == sbl.mode;
  28. }
  29.  
  30.  
  31. private:
  32. sort_mode mode;
  33. };
  34.  
  35.  
  36.  
  37. typedef set<string, Sort_By_Length> String_Set;
  38. void fill_set(String_Set& set);
  39.  
  40. int main()
  41. {
  42. //create, fill, and print set with normal element order
  43. //-uses default sorting criterion
  44. String_Set coll;
  45. fill_set(coll);
  46.  
  47. copy(coll.begin(), coll.end(), //range
  48. ostream_iterator<string>(cout, "\n")); //destination
  49. cout << endl;
  50. }
  51.  
  52. void fill_set(String_Set& set)
  53. {
  54. set.insert("reallylarge");
  55. set.insert("small");
  56. set.insert("medium");
  57. }
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Dec 23rd, 2005
0

Re: vector<string> - way to find longest string?

>so are you aware of an existing solution provided by vector or algorithm in general?
If you have to use a vector, you can sort by length and then take the last string as your longest:
C++ Syntax (Toggle Plain Text)
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. struct length {
  9. bool operator() ( const string& a, const string& b )
  10. {
  11. return a.size() < b.size();
  12. }
  13. };
  14.  
  15. int main()
  16. {
  17. string init[] = {
  18. "This","is","a","test","and","aaaaa"
  19. };
  20. vector<string> v ( init, init + 6 );
  21.  
  22. sort ( v.begin(), v.end(), length() );
  23.  
  24. cout<< v.back() <<'\n';
  25. }
Alternatively, because you might not need the vector sorted as well and a simple search might be more than enough, you can use max_element with the length predicate:
C++ Syntax (Toggle Plain Text)
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. struct length {
  9. bool operator() ( const string& a, const string& b )
  10. {
  11. return a.size() < b.size();
  12. }
  13. };
  14.  
  15. int main()
  16. {
  17. string init[] = {
  18. "This","is","a","test","and","aaaaa"
  19. };
  20. vector<string> v ( init, init + 6 );
  21.  
  22. cout<< *max_element ( v.begin(), v.end(), length() ) <<'\n';
  23. }
If you're doing more searching than anything else, a vector might not be an appropriate data structure to begin with.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 23rd, 2005
0

Re: vector<string> - way to find longest string?

Quote ...
cout<< *max_element ( v.begin(), v.end(), length() ) <<'\n';
That's pretty sweet. I guess mine was blown way out of proportion.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Dec 23rd, 2005
0

Re: vector<string> - way to find longest string?

Thanks for your attempts Narue and SC. In the end though, it looks like the solutions still iterate through each element and see if it's the largest or not. I guess the only way this would not have been in the case was if vector automatically had already stored the longest and shortest values as they were inserted. (Although I guess that's not likely/practical given that the container might be used to store data of any kind and therefore longest/shortest may not have any meaning in all contexts).

thanks again guys.
Winbatch

This is what I ended up using/writing:
Quote ...
void General::getMinimumAndMaximumLengths( vector<string> & vec, int & min, int & max, string & minStr, string & maxStr )
{
if ( vec.empty() )
{
min=max=0;
minStr.clear();
maxStr.clear();
}
else
{


for (unsigned int i=0;i< vec.size();i++)
{

unsigned int current = vec[i].size();
if ( i==0 )
{
min=max=current;
minStr=maxStr = vec[i];
}

if ( current <=min )
{
min = current;
minStr = vec[i];
}

if ( current >=max )
{
max = current;
maxStr = vec[i];
}
}

}

}
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: File array question
Next Thread in C++ Forum Timeline: Windows Sockets





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC