排序包含类的'std :: vector' [英] sort the 'std::vector' containing classes

查看:138
本文介绍了排序包含类的'std :: vector'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用二进制谓词和 std :: sort

我需要此示例。 。

推荐答案

这里是使用两种不同谓词的示例的另一种修改。指定的谓词可以是函数指针或函数,它是一个定义operator()的类,以便实例化时的对象可以像函数一样使用。注意,我不得不添加一个标题包含到功能头。这是因为函子继承自在std库中定义的binary_function。

Here is another adaptation of the example that uses two different kinds of predicates. The predicate specified can be a function pointer or a functor which is a class that defines operator() so that the object when instantiated can be used just like a function would be. Notice that I had to add one more header inclusion to the functional header. This is because the functor inherits from binary_function which is defined within the std library.

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

 using namespace std;

 class MyData
 {
  public:
    static bool compareMyDataPredicate(MyData lhs, MyData rhs) { return (lhs.m_iData <                        rhs.m_iData); }
    // declare the functor nested within MyData.
      struct compareMyDataFunctor : public binary_function<MyData, MyData, bool>
   {
      bool operator()( MyData lhs, MyData rhs)
        {
            return (lhs.m_iData < rhs.m_iData);
         }
    };

   int m_iData;
      string m_strSomeOtherData;
   };


 int main()
{
  // Create a vector that contents elements of type MyData
     vector<MyData> myvector;

       // Add data to the vector
        MyData data;
       for(unsigned int i = 0; i < 10; ++i)
       {
          data.m_iData = i;
          myvector.push_back(data);
       }

    // shuffle the elements randomly
       std::random_shuffle(myvector.begin(), myvector.end());

       // Sort the vector using predicate and std::sort.  In this case the predicate is     a static
       // member function.
      std::sort(myvector.begin(), myvector.end(), MyData::compareMyDataPredicate);

      // Dump the vector to check the result
    for (vector<MyData>::const_iterator citer = myvector.begin();
        citer != myvector.end(); ++citer)
   {
        cout << (*citer).m_iData << endl;
     }

   // Now shuffle and sort using a functor.  It has the same effect but is just a different
       // way of doing it which is more object oriented.
         std::random_shuffle(myvector.begin(), myvector.end());

       // Sort the vector using predicate and std::sort.  In this case the predicate is a functor.
         // the functor is a type of struct so you have to call its constructor as the third argument.
       std::sort(myvector.begin(), myvector.end(), MyData::compareMyDataFunctor());

    // Dump the vector to check the result
        for (vector<MyData>::const_iterator citer = myvector.begin();
        citer != myvector.end(); ++citer)
    {     
           cout << (*citer).m_iData << endl;
      }
    return 1;
   }

这篇关于排序包含类的'std :: vector'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆