-
Sort 2D Vector
Hi guys,
Is there a way I can use std::sort() to sort a 2D vector by the value of its first member?
So, for instance:
Code:
std::vector< std::vector<char*> > ProductKeyInfo;
ProductKeyInfo.push_back( vector<char*>() );
ProductKeyInfo[0].push_back( "Microsoft Windows" );
ProductKeyInfo[0].push_back( "DigitalProductId" );
ProductKeyInfo[0].push_back( "DecodeMicrosoftKey" );
ProductKeyInfo.push_back( vector<char*>() );
ProductKeyInfo[1].push_back( "Microsoft Office 2003 - Professional" );
ProductKeyInfo[1].push_back( "DigitalProductId" );
ProductKeyInfo[1].push_back( "DecodeMicrosoftKey" );
I can use std::sort(ProductKeyInfo.begin(), ProductKeyInfo.end()) which sorts by the indexes but can't work out how to sort by the alphabetic order of the strings for the fist member of each. So, i'd like to sort by ProductKeyInfo[i][0] Is there a simple way to do this?
I guess I could write my own bubble sort method to do this, but I don't want to re-invent the wheel :)
Cheers guys!
Dave
-
Code up a comparator routine and pass the routine as a third parm.
-
Or make it a struct/class, and implement your own operator< for that, which only uses the field(s) you want to actually sort on.
I think you will also find that solution more practical for any other code you are using, as you can easily reference the different portions of your struct, rather than indexing [x][1] for the "DigtalProductId", for example.
--
Mats
-
Thanks guys.
I was looking at going down the custom routie option for std::sort(), but am unsure of how to pass the child to the predicate. I've looked arond (honest) but can't find anything suitable.
I've got it working to sort a 1D vector of strings, how do I pass the members to the sort function though?
Code:
std::sort( outVector.begin(), outVector.end(), sortVectorStrings() );
Thanks,
Dave
-
If sortVectorStrings is a free function, you would write:
Code:
std::sort( outVector.begin(), outVector.end(), sortVectorStrings );
-
Why aren't you using a vector of std::strings?
Code:
std::vector< std::string > ProductKeyInfo;
-
Thanks guys.
cpjust - Because a load of APIs I'm calling require char*, so rather than store as a std::string and then convert I figured it best to just store as char* in the first place.
-
If they require only const char*, then there's c_str().
-
In fact, that should be const char* anyway, since using char* for string literals is deprecated and dangerous. If you never modify those strings then it will probably ok, but it shouldn't hurt to be safer.
-
And if they require char*, yet do not modify the strings, you can remove the const-ness with a const_cast.
If they do modify the strings, you're out of luck. For std::strings, anyway.
-
I'd be more likely to help craft a solution if you posted your code, working or not, that was a start.
Todd
-
Thanks for your input guys, i'm a hobbyist programmer, i'm just going to re-write a section of the code to try and illustrate what i'm trying to achieve rather than post a lot on relavant code :)
Thanks for all the input, it's greatly appreciatred!