You can sort the vector without splitting it. call std::sort to do the sorting. All you have to do is to expand the resource number from 1 to 2 digits, such as "resource1" to "resource01" when you read them from the file. That way std::sort (or any other sort algorithm) will sort the vector correctly.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>> Is there a way to get these numbers to have zeros prefixed to them?
Not that I know of
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Another option is to supply your comparison method to std::sort:
int getDigitFromLine(const std::string& line){
const string digits = "0123456789";
size_t beg = line.find_first_of(digits);
size_t end = line.find_first_of(digits);
string digitString = line.substr(beg, end-beg + 1);
return atoi( digitString.c_str() );
}
bool mycomp(const std::string& lhs, const std::string& rhs){
int lhsNumber = getDigitFromLine(lhs);
int rhsNumber = getDigitFromLine(rhs);
return lhsNumber < rhsNumber;
}
int main(){
//populate vector
std::sort( vec.begin(), vec.end(), mycomp);
}
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
That was my original thought too, but I dismissed it because it would be horribly sloooow.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
That was my original thought too, but I dismissed it because it would be horribly sloooow.
But adding leading 0's infront assumes you know the maximum number of resource
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608