Question

Frist some code, an example class:

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dis(1, 100);

class T
{
private:
   int x;
   int y;
public:
   T(): x(dis(gen)), y(dis(gen)) {}
   int getX() const { return x; }
   int getY() const { return y; }
};

A function object class:

class F
{
public:
   inline bool operator()(const T &a, const T &b) const
   {
      return (((a.getX() * a.getY()) > ( b.getX() * b.getY())) ? 1 : 0);
   }
};

I create objects of class T and then I try to sort them:

std::vector<T> myVec(10);
T *p = new T[10];
F f;

for(int i = 0; i < 10; ++i)
{
    myVec.push_back(p[i]);
}
std::sort(myVec.begin(), myVec.end(), f);

After using std::sort objects contain other values (x, y). Why does std::sort change values of the objects ? What is wrong ?

Was it helpful?

Solution

At first you created a vector of 10 elements

std::vector<T> myVec(10);

Then you appended 10 new elements to the vector

for(int i = 0; i < 10; ++i)
{
    myVec.push_back(p[i]);
}

Now the vector has 20 elements. And you sorted this vector of 20 elements.

Maybe you meant the following

std::vector<T> myVec;
myVec.reserve( 10 );

//...
for(int i = 0; i < 10; ++i)
{
    myVec.push_back(p[i]);
}

OTHER TIPS

You may check as following way, but my pc isn't show any error. I'm using Visual Studio 2010.

#include <algorithm>
#include <string>
#include <vector>
#include <random>


std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int> dis(1, 100);

class T
{
private:
   int x;
   int y;
public:
   T(): x(dis(gen)), y(dis(gen)) {}
   int getX() const { return x; }
   int getY() const { return y; }
};
class F
{
public:
   inline bool operator()(const T &a, const T &b) const
   {
      return (((a.getX() * a.getY()) > ( b.getX() * b.getY())) ? 1 : 0);
   }
};


int main()
{
    //freopen("output.txt","w",stdout);
    std::vector<T> myVec(10);
    T *p = new T[10];
    F f;

    for(int i = 0; i < 10; ++i)
    {
        myVec.push_back(p[i]);
    }

    //printf("Before sorting:\t");
    //for(int i = 0; i < myVec.size(); ++i)
    //  printf("%c(%d, %d [%d])",(i != 0 ? ',' : ' '), myVec[i].getX(), myVec[i].getY() , i);
    //printf("\n");

    std::vector< std::pair<int,int> > cache;
    for(int i = 0; i < myVec.size(); ++i)
        cache.push_back( std::make_pair( myVec[i].getX(), myVec[i].getY() ) );


    std::sort(myVec.begin(), myVec.end(), f);

    //printf("After sorting:\t");
    for(int i = 0; i < myVec.size(); ++i)
    {
        auto p = std::make_pair( myVec[i].getX(), myVec[i].getY() );
        auto t = std::find( std::begin(cache), std::end(cache), p);
        //int pos = std::distance( std::begin(cache), t);
        if ( t == std::end(cache) )
            printf("Error: Not found ( %d, %d ) pair\n", p.first, p.second);
        //printf("%c(%d, %d [%d])",(i != 0 ? ',' : ' '), p.first, p.second, pos );
    }
    //printf("\n");

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top