0

I have a vector<string> container but the strings are all numbers

EDIT: I tried this:

so now the logic seems to be borked

cleaned this up but experimenting with various attempts to cast std::string to an int is tough lamba was useless and I have run out of ideas for casting std::string without some bug surfacing

template<typename Iterator>void bubbleSort(Iterator first, Iterator last){
    Iterator i, j;
    for (i = first; i != last; i++)
        for (j = first; j < i; j++)
            if (*i < *j)
                std::iter_swap(i, j); // or std::swap(*i, *j);
}

My code to read the source data is

void loadgames(void) { // read the game app id's
ifstream inFile;
ofstream outFile;
string s;
inFile.open("game-list.txt");
if (inFile.is_open()) {
    while (std::getline(inFile, s)) {
        if(s.length() > 0)
            gamelist.push_back(s); 
    };
    inFile.close();
}
//  bubbleSort(gamelist.begin(),gamelist.end());
outFile.open("game-list.txt");
if (outFile.is_open()) {
    for (i = gamelist.begin(); i != gamelist.end(); i++) {
        outFile << *i << endl;
    }
}
outFile.close();

}

The call to is the problem of sorting my vector

bubbleSort(gamelist.begin(),gamelist.end());
13
  • do i need to use a custom function to compare? using std::sort ? Yes, but it would be a fairly trivial function. Commented Feb 8, 2022 at 18:09
  • 1
    "but the strings are all numbers" - then why not use std::vector<int> instead, and simply parse the strings with std::stoi() or equivalent when you are inserting them into the vector? Commented Feb 8, 2022 at 18:11
  • 3
    The nasty part is which goes first: 1 or 10? With a number, the ordering is obvious. As a string you might be surprised. Commented Feb 8, 2022 at 18:18
  • 1
    @HardcoreGames Please don't keep changing the question, adding code that you got from the answer. That makes this Q&A really hard to get help from in the future. I suggest you revert your changes where you've copied code from the answer - at least the latest edit. If something is borked, it's not in the code we now see in the question. Note: If the conversion from string to int fails, stoi throws an exception. Have you verified that the data in the vector<string> is correct? Commented Feb 8, 2022 at 20:05
  • 1
    @HardcoreGames You can ADD new information to your question, but please don't CHANGE past information, it invalidates prior comments/answers and makes it difficult to follow the conversation. Commented Feb 8, 2022 at 20:25

1 Answer 1

6

As you already know, you can use std::sort() with a custom comparator. The problem is your compare is setup wrong. Its parameters need to be the container's value_type, not the container type. And you have an erroneous . in it.

Try this instead:

std::vector<std::string> gamelist;
// fill gamelist as needed...

auto compare = [](const std::string &a, const std::string &b){
    return std::stoi(a) < std::stoi(b);
};

std::sort(gamelist.begin(), gamelist.end(), compare);

Online Demo

However, I would suggest using std::vector<int> instead (or appropriate integer type, depending on your string contents), and simply parse the std::strings into integers with std::stoi() (or equivalent) before inserting them into the std::vector, eg:

std::vector<int> gamelist;
// fill gamelist as needed...
gamelist.push_back(std::stoi(someString));
...
std::sort(gamelist.begin(), gamelist.end());

Online Demo

This will consume less memory and sort much faster, and it will reduce the complexity of the sort since you will be incurring the overhead of converting strings to integers only one time up front, not on every iteration of the sort algorithm (potentially parsing the same strings over and over).

Sign up to request clarification or add additional context in comments.

25 Comments

after sorting I was intending to write the text file back as notepad friendly text
I have already written a lot of code based on the std::string but I simply wanted to sort the list as (int)
Nothing stops you from converting a vector of strings into a vector of ints before sorting, and then converting back afterwards. But whatever. My answer already shows you how to sort a vector of strings
@HardcoreGames std::sort needs O(nlogn) comaparisons and the proposed solution transforms the strings for every single comparison. I would not be surprised if transforming to a std::vector<int>, sorting, then transforming it back, is less expensive, because it only needs O(n) transformations
@HardcoreGames You can't type-cast a string to an int like that, you must use a conversion function instead, like std::stoi(), as shown in my answer. Also, your syntax is wrong, as it is missing a comma after end(), a -> before bool, and the } is in the wrong place.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.