简体   繁体   中英

Sorting a list of mixed data structures on the basis of integer value and finding an element in it based on the string value in c++?

I'm programming in QtCreator and currently I'm using QList as shown in the following code:

#include <vector>
#include <map>
#include <algorithm>
#include <QDebug>


class mixed

{

public:

    int number;
    QString name;
    QString address;
    mixed(int n, QString s, QString a)
    {
        number = n;
        name = s;
        address = a;
    }
};


bool myfunction (mixed i,mixed j) { return (i.number<j.number); }

bool mySearch (mixed i, mixed j) {
  return (i.name==j.name);
}

int main()

{

    QList<mixed>myV;
    mixed object(100, "akkas", "100");
    myV.push_back(object);
    myV.push_back(mixed(2, "akkas1", "2"));
    myV.push_back(mixed(1111, "akkas2", "1111"));
    myV.push_back(mixed(-1, "akkas3", "-1"));
    myV.push_back(mixed(7, "akkas4", "7"));
    myV.push_back(mixed(0, "akkas0", "0"));
    myV.push_back(mixed(2, "akkas0", "21"));

    for(int i=0; i<myV.size(); i++)
    {
        qDebug()<<myV.at(i).number<<" "<<myV.at(i).name<<" "<<myV.at(i).address<<endl;
    }

    std::sort (myV.begin(), myV.end(), myfunction);

    for(int i=0; i<myV.size(); i++)
    {
        qDebug()<<myV.at(i).number<<" "<<myV.at(i).name<<" "<<myV.at(i).address<<endl;
    }
//   QList<mixed>::iterator it;
//    it = std::search(myV.begin(), myV.end, object, mySearch);
//    if (it!=myV.end())
//        qDebug() << "found at position " << (it-myV.begin()) << '\n';
//      else
//      qDebug() << "not found\n";

//    qDebug()<<myV.indexOf(object)<<endl;
    return 0;
}

But the problem is the commented out line

qDebug()<<myV.indexOf(object)<<endl;

fails because

no match for 'operator==' (operand types are 'mixed' and 'const mixed')
             if (n->t() == t)
                        ^

On the other hand I was trying to use std::search using predicate comparison where predicate is as below:

bool mySearch (mixed i, mixed j) {
  return (i.name==j.name);
}

But I can't understand why it gives the error

no matching function for call to 'search(std::vector<mixed>::iterator, <unresolved overloaded function type>, mixed&, bool (&)(mixed, mixed))'
     it = std::search(myV.begin(), myV.end, object, mySearch);
                                                            ^

I need to use something that can allow me easy sorting using the integer value of the mixed data type and finding any element using the string value of that type.

What mistake am I doing in my approach? What alternative do I have? Could you please give me some example with code?

Thanks.

EDIT

After the responses I have corrected end to end() , but even then there are errors. Now the error is:

error: no matching function for call to 'search(QList<mixed>::iterator, QList<mixed>::iterator, mixed&, bool (&)(mixed, mixed))'
     std::search(myV.begin(), myV.end(), object, mySearch);
                                                         ^
qDebug()<<myV.indexOf(object)<<endl;

For this to work you need to implement == operator for class mixed . Example:

 bool operator==(const mixed& rhs)
  {
    return name == rhs.name;
  }

For second error, As rightly pointed out in the comments, you need to correct end iterator.

std::search(myV.begin(), myV.end(), object, mySearch); 
                          ^^^^^^^^

UPDATE : However this is NOT correct use of std::search . Use std::search which relies on == operator.

std::find(myV.begin(), myV.end(), object); // Uses == operator to find match

Reference: http://www.cplusplus.com/reference/algorithm/find/

This should work.

For cases like this, where your find criterion may differ from call to call, I prefer find_if : it does not force me to overload operator== for my type.

auto it = std::find_if(begin(myVec), end(myVec),
    [](const auto &a, const auto &b) { return a.name == b.name; });

auto index = std::distance(begin(myVec), it);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM