1

I have a function that does some computing and set the value returned into my vector.

#include <vector>
#include <algorithm>
#include <iostream>

void VectorLoop(vector<ClassName>& Vector)
{
float TempFloatVariable = 0.0;
int count = 0;
int update = 0;

while (count != Vector.size())
{
    if (Vector[count].getValue() == 100.0) //I hardcode this so i can check if the Value is empty or not
    {           
            //code that set all of the variable from the vector's memory's object

            TempFloatVariable = AnotherClass.Formula //does the computing and return the value
            //code that gets value from object for overloading 
            //code that gets value from object for overloading 
            //code that gets value from object for overloading

            Vector[count].setValue(TempFloatVariable);
            update++;
    }
    else
    {
        count++;
    }
}

cout << "Computation completed! (" << update << " record(s) were updated)" << endl;
}

After all of the computing is done, I want to sort them from highest to lowest, base on the Value, but I have no idea how to do so, i tried to hard code it, pulling values out manually 1 by 1 to do comparison, but kept failing. And that would defeat the purpose of using vector, there are many examples of sorting using vector, but 90% of it are int values stored in the vectors.

14
  • 2
    std::sort(Vector.begin(), Vector.end(), [](const PointTwoD& a, const PointTwoD& b) { return a.getcivIndex() > b.getcivIndex(); } ); Commented Apr 25, 2015 at 20:08
  • what about the cout part ? @IgorTandetnik Commented Apr 25, 2015 at 20:20
  • Once you've fixed the code (maybe you have already) let me advise that you try to get the code reviewed (e.g., on CodeReview). It looks to me like quite a lot could be said about this code that's not related to the task at hand, but might do quite a bit to improve your understanding of C++. Commented Apr 25, 2015 at 21:45
  • @IgorTandetnik I tried your code but i face some compiling errors, i am guessing your code requires C++11 to do so, but because this is an assignment and my teacher actually limited us on using it on an ubuntu 9.10, and we have to use the terminal to compile using g++ commands Commented Apr 26, 2015 at 10:36
  • @JerryCoffin I will do so once all my functions are able to do their basic requirements Commented Apr 26, 2015 at 10:37

2 Answers 2

2

As IgorTandetnik said: you could do

std::sort(Vector.begin(), Vector.end(), [](const PointTwoD& a, const PointTwoD& b)
{ 
    return a.getcivIndex() > b.getcivIndex();
});

which will use the result from the lambda to sort the vector and should do what you want.

To print the objects in the vector you should do something like this:

for(int i = 0; i < Vector.size(); i++)
{
    std::cout << Vector[i] << std::endl; //Or whatever your printing function is.
}

Which will iterate over all the objects in the vector and, as they should already be in descending order from the sort, it will print them out in descending order like you wanted.

Edit:

For non C++11 users: you can define a function that does the comparison

bool compare(const PointTwoD& a, const PointTwoD& b)
{
    return a.getcivIndex() > b.getcivIndex();
}

and use it instead of the lambda. Like this:

std::sort(Vector.begin(), Vector.end(), compare);
Sign up to request clarification or add additional context in comments.

Comments

1

Use std::sort:

template <class RandomAccessIterator>
  void sort (RandomAccessIterator first, RandomAccessIterator last);

The first argument is an iterator to the first element you want to sort. The second argument is the final iterator position after sorting (in other words, one past the final element you want to sort). So to sort you would do this:

std::sort(Vector.begin(), Vector.end());

To determine how the sorting is actually performed, you need to define a < operator for PointTwoD that compares the civ index. Alternately you can create an external function that takes two PointTwoDs as parameters and returns a boolean, and specify it through the third argument to std::sort. If the sort function needs to access private members you'll need to declare it as a friend function in the class definition, though.

You can also inline the compare function in the std::sort call using a lambda if you want, like Phantom posted in his answer.

4 Comments

Thanks for suggesting, but i know nuts about iterators, so i decided to stick with what i know
You don't need to know anything about iterators. begin() and end() return the exact iterators you need. There's no need to mess with them further than that for a simple sort.
but i thought you actually need to know where they point and how to use them correctly, else they will screw your data up ?
sure, but begin() points to the beginning of your data and end() points to end. It covers the entire vector. The only reason you would need to know anything else is if you wanted to sort some subset of your data rather than the entire thing, which is not the case from the sounds of it. If you wanted to use other iterator-related functions you might want to learn more, but for sorting this is really all you need to know.

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.