Sorting a vector of custom objects

后端 未结 13 2932
既然无缘
既然无缘 2020-11-21 05:14

How does one go about sorting a vector containing custom (i.e. user defined) objects.
Probably, standard STL algorithm sort along with a predicate (a fu

13条回答
  •  误落风尘
    2020-11-21 05:54

        // sort algorithm example
        #include      // std::cout
        #include     // std::sort
        #include        // std::vector
        using namespace std;
        int main () {
            char myints[] = {'F','C','E','G','A','H','B','D'};
            vector myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33
            // using default comparison (operator <):
            sort (myvector.begin(), myvector.end());           //(12 32 45 71)26 80 53 33
            // print out content:
            cout << "myvector contains:";
            for (int i=0; i!=8; i++)
                cout << ' ' <

提交回复
热议问题