C++中使用pair和make_pair在vector, sort -- ZZ

class SampleReducer: public mapreduce::BasicReducer {
 public:
  static bool ordering(const std::pair<std::string, int64> a,
      const std::pair<std::string, int64> b) {
    return a.second > b.second;
  }
  virtual void Reduce(mapreduce::ReduceContext* context) {
    vector< pair<string, int64> > bad_package_list;
    appsearch::AppSearMergedDoc app_doc;
    bool is_has_bad_package = false;
    while (context->NextValue()) {
      if (base::FromStringToThrift(context->GetInputValue(), &app_doc)) {
        if (0 == base::StrCaseCmp(
              app_doc.apk_analysis_prop.packageName.c_str(),
              FLAGS_package_name.c_str())) {
          is_has_bad_package = true;
        }
        string package_name = app_doc.apk_analysis_prop.packageName;
        string time_str;
        Time::FromTimeT(app_doc.info_crawl_doc.timestamp)
                                .ToLocalDateString(&time_str);
        string tmp_value =
          app_doc.app_name + "\t" + package_name + "\t" + time_str + "\t" + app_doc.url;
        bad_package_list.push_back(
            make_pair<string, int64>
            (tmp_value,app_doc.info_crawl_doc.timestamp));
      }  // if
    } // end while
    if (is_has_bad_package) {
      sort(bad_package_list.begin(), bad_package_list.end(), &SampleReducer::ordering);
      string value = bad_package_list.begin()->first;
      if (value.find(FLAGS_package_name)==string::npos) return;
      context->Emit(context->GetInputKey(), bad_package_list.begin()->first);
    }
  }
};

bool strict_weak_ordering(const std::pair<int, std::string> a, const std::pair<int, std::string> b) { return a.first < b.first; } int main() { using namespace std; vector<pair<int, string> > vec; vec.push_back(make_pair<int, string>(5, "hello")); vec.push_back(make_pair<int, string>(4, "hell")); vec.push_back(make_pair<int, string>(6, "hello,")); sort(vec.begin(), vec.end(), strict_weak_ordering); vector<pair<int, string> >::iterator it = vec.begin(), end = vec.end(); for(; it != end; ++it) cout<<it->second<<endl; }


sort ( ; ; compare)

compare 在内部类函数时要注意,链接问题。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值