main.cpp

// Give your class a < operator and define a vector of HasPtrs. Give that vector
// some element and then sort the vector.

std::vector<HasPtr> vecHP;
std::string val;
while (std::cin >> val) {
    HasPtr temp(val);
    vecHP.push_back(temp);
}
std::sort(vecHP.begin(), vecHP.end());

HasPtr.h

bool operator<(const HasPtr& rhs) {
    return (*this->ps < *rhs.ps);
}

private:
    std::string *ps;
    int i;

i could've used supplementary compare function for the third param of sort, but I'm sure the question aimed to use the default sort's compare function which mean that I need to adjust the operator< to perform the appropriate task. So, how I can access both value, returned the appropriate binary value and then process the sorting without getting this error?

ERROR

PS: the operators works on "normal" comparison like if (Obj1 < Obj2) and this exercise is present on sub-chapter about swap and it's use in copy-assignment operator for the classes. (probably unrelated, well who knows?)

This quick example may get you started on the right track ...

// sortVecPointers.cpp //

#include <iostream>
#include <vector>
#include <algorithm> // for sort of vector elements ...

using namespace std;



ostream& operator << ( ostream& os,  vector< int* > v )
{
    vector< int* >::const_iterator it;
    for( it = v.begin(); it != v.end() ; ++ it )
        os << *(*it) << ' ';
    return os; 
}

bool myCmp( const int* a, const int* b )
{
    return *a < *b;
}

int main()
{
    vector< int* > myPts;

    for( int j = 10; j > 0; -- j )
    {
        int* i = new int;
        *i = j;
        myPts.push_back( i );
    }

    cout << "Before sort ...\n";
    cout << "values at addresses " << myPts << endl;

    sort( myPts.begin(), myPts.end(), myCmp );

    cout << "After sort ...\n";
    cout << "values at addresses " << myPts << endl;


    cout << "\nPress 'Enter' to continue/exit ... " << flush;
    cin.get() ;
}

Don't forget to delete each new element ... when (finally) done with it.

Edited 2 Years Ago by David W: fixed typo ... added comment

I don't know where u're getting me at, but as I said before, "I nid to use default sort's compare function", can I do it without the "myCmp" function? by adjusting the class operator< as shown above.(but that one give me weird error)

or u basically saying that there are no possible algorithm to NOT use the sort's third parameter? if so, What's the relation between the sorting vector and modifying my own < operator? basically, I can use the sort's third param + member function to accesss appropriate member without the need to bother about the < operator...

You know, the great thing about PC's is that YOU can quickly do some code changes and 'see' ...

// sortVecPointers.cpp //

#include <iostream>
#include <vector>
#include <algorithm> // for sort of vector elements ...

using namespace std;



ostream& operator << ( ostream& os,  vector< int* > v )
{
    vector< int* >::const_iterator it;
    for( it = v.begin(); it != v.end() ; ++ it )
        os << *(*it) << ' ';
    return os; 
}

/*
bool myCmp( const int* a, const int* b )
{
    return *a < *b;
}
*/

void showAddresses ( ostream& os,  vector< int* > v )
{
    vector< int* >::const_iterator it;
    for( it = v.begin(); it != v.end() ; ++ it )
        os << (*it) << ' '; 
}

int main()
{
    vector< int* > myPts;

    for( int j = 10; j > 0; -- j )
    {
        int* i = new int;
        *i = j;
        myPts.push_back( i );
    }

    cout << "Before sort ...\n";
    cout << "values at addresses " << myPts << endl;
    cout << "addresses \n"; 
    showAddresses( cout, myPts );

    //sort( myPts.begin(), myPts.end(), myCmp );

    sort( myPts.begin(), myPts.end() ); // Note: sorts address only ...

    cout << "\n\nAfter sort ...\n";
    cout << "values at addresses " << myPts << endl;
    cout << "addresses \n"; 
    showAddresses( cout, myPts );


    cout << "\n\n\nPress 'Enter' to continue/exit ... " << flush;
    cin.get() ;
}

Edited 2 Years Ago by David W: added words

This article has been dead for over six months. Start a new discussion instead.