// ============================================================================ // p1c09_04_03 — 4.3 内建函数对象 // 来源:川大 C++/LinuxC wiki「02.Qt方向」 // 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用 // // --- 原文片段 [pageId:blockIdx] --- 标注出处。 // 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。 // ============================================================================ #include using namespace std; #include #include #include // --- 原文片段 [58954498:2] 4.3 内建函数对象 --- //取反仿函数 void test01() { negate n; cout << n(50) << endl; } //加法仿函数 void test02() { plus p; cout << p(10, 20) << endl; } //大于仿函数 void test03() { vector v; srand((unsigned int)time(NULL)); for (int i = 0; i < 10; i++){ v.push_back(rand() % 100); } for (vector::iterator it = v.begin(); it != v.end(); it++){ cout << *it << " "; } cout << endl; sort(v.begin(), v.end(), greater()); for (vector::iterator it = v.begin(); it != v.end(); it++){ cout << *it << " "; } cout << endl; } // --- main --- int main() { test01(); test02(); test03(); return 0; }