1. 程式人生 > 其它 >VSCode 配置 C++ 開發環境

VSCode 配置 C++ 開發環境

一、介紹
std::not1 和 std::not2 是用來把,符合某種特殊條件的『函式物件』轉換為反義「函式物件」的函式。

具體的:

not1是構造一個與謂詞結果相反的一元函式物件。
not2是構造一個與謂詞結果相反的二元函式物件。

二、用法
not1

// not1 example
#include <iostream>     // std::cout
#include <functional>   // std::not1
#include <algorithm>    // std::count_if
 
struct IsOdd {//是否為奇數
  bool operator
() (const int& x) const {return x%2==1;} typedef int argument_type; }; int main () { int values[] = {1,2,3,4,5}; int cx = std::count_if (values, values+5, std::not1(IsOdd()));//計算不為奇數的個數 std::cout << "There are " << cx << " elements with even values.\n"; return 0; }


not2

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

using namespace std;

int main()
{

std::vector<int> nums = { 5, 3, 4, 9, 1, 7, 6, 2, 8 };

//升序
std::function<bool(int, int)> ascendingOrder = [](int a, int b) { return a < b; };

// 排序,不是按升序,而是按降序
std::sort(nums.begin(), nums.end(), std::not2(ascendingOrder));

for (int i : nums) { std::cout << i << " "; } return 0 ; }