Vector values in an ascending order

Hi, I am writing a program that takes all the values in a vector, arranges them from highest to lowest and writes them into a file.

The following code freezes my console if I set i>=0 in the for loop. The code seems perfectly fine and reasonable, but it doesn't work.

1
2
3
4
5
6
7
8
void write(const std::vector<double>& Numbers)
{
    std::ofstream w("Results.txt");
    w << std::fixed << std::setprecision(2);
    w << "Values in ascending order: ";
    for (unsigned int i=(Numbers.size()-1);i>=0;i--)
        w << Numbers[i] << " ";
}


The following code doesn't freeze the console, although it doesn't write the last (Numbers[0]) value to the file.

1
2
3
4
5
6
7
8
void write(const std::vector<double>& Numbers)
{
    std::ofstream w("Results.txt");
    w << std::fixed << std::setprecision(2);
    w << "Values in ascending order: ";
    for (unsigned int i=(Numbers.size()-1);i>0;i--)
        w << Numbers[i] << " ";
}


If I want to write the numbers in descending order, the following code works perfectly fine:

1
2
3
4
5
6
7
8
void write(const std::vector<double>& Numbers)
{
    std::ofstream w("Results.txt");
    w << std::fixed << std::setprecision(2);
    w << "Values in descending order: ";
    for (unsigned int i=0;i<Numbers.size();i++)
        w << Numbers[i] << " ";
}


(Notice how i=0 works perfectly without freezing the console. Either something fishy is going on or I'm missing something extremely obvious...) Can anybody figure out why this is the case? Thanks!
when you do i >= 0 at some point i will be 0. At this point you will subtract 1 from i. Since i is an unsigned type and therefore cannot be negative. The value of i then becomes the maximum value that an unsigned type can hold on your computer which is probably not a valid vector index.

You might want to try the vector at method which will throw a out of bounds exception in this case.


FYI this is a bug Numbers.size()-1 vector size returns an unsigned type if the vectors size is zero. Same problem as above.
Last edited on
Well, I just tried i>=1 and by that logic at some point I will subtract 1 from i and i will be equal to 0, therefore it should write the biggest value at Numbers[0]... It doesn't. The effect is the same as i>0. Either I don't understand something or this logic is flawed.

EDIT:

Fixed the problem by setting the i to int instead of unsigned int. Doesn't seem to give me any problems, is this a good thing to do? Doesn't seem possible otherwise.
Last edited on

Fixed the problem by setting the i to int instead of unsigned int. Doesn't seem to give me any problems, is this a good thing to do?

Not really. You should be using a size_t, not an int or unsigned int because this is what std::vector.size() returns. And remember that the size_t could be any unsigned type and that the value returned could be much larger than what can be held in an int.

To solve this problem you could use the reverse iterators instead. Something like:

1
2
3
4
5
6
7
void write(const std::vector<double>& Numbers)
{
    cout << "Values in ascending order: ";
    for(auto itr = Numbers.rbegin(); itr != Numbers.rend(); ++itr)
        cout << *itr << " ";
    cout << endl;
}

1
2
3
4
5
6
7
8
void write(const std::vector<double>& Numbers)
{
    std::ofstream w("Results.txt");
    w << std::fixed << std::setprecision(2);
    w << "Values in ascending order: ";
    for (size_t i=(Numbers.size()-1);i>=0;i--)
        w << Numbers[i] << " ";
}


This still freezes the console. I'll look into the reverse iterators later, thanks.

EDIT:

Ended up using the reverse iterators, thank you for the help!
Last edited on
Topic archived. No new replies allowed.