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 在内部类函数时要注意,链接问题。