vector<string> - way to find longest string?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

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

 
0
  #1
Dec 22nd, 2005
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...)
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,751
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 740
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #2
Dec 22nd, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

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

 
0
  #3
Dec 22nd, 2005
As long as there is no built in function that I'm not aware of, I'll iterate through...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,751
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 740
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #4
Dec 23rd, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

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

 
0
  #5
Dec 23rd, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

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

 
0
  #6
Dec 23rd, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

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

 
0
  #7
Dec 23rd, 2005
This is my try at it. It may not be very good, but it works.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,751
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 740
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #8
Dec 23rd, 2005
>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:
  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:
  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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

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

 
0
  #9
Dec 23rd, 2005
cout<< *max_element ( v.begin(), v.end(), length() ) <<'\n';
That's pretty sweet. I guess mine was blown way out of proportion.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

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

 
0
  #10
Dec 23rd, 2005
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:
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];
}
}

}

}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC