IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++二分常用算法和排列算法 -> 正文阅读

[C++知识库]C++二分常用算法和排列算法

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
using namespace std;
/*
* binary_search:查找某个元素是否出现。
* lower_bound:查找第一个大于或等于某个元素的位置。
* upper_bound:查找第一个大于某个元素的位置。
* equal_range:查找某个元素出现的起止位置。注意,终止位置为最后一次出现的位置加一。
*/

// 查找第一个大于或等于某个元素的位置
void LowerBound()
{
    vector<int> v;
    vector<int>::iterator iter;
    for (int i = 1; i <= 20; i++) {
        v.push_back(i);
    }
    sort(v.begin(), v.end());
    cout << "array: ";
    copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
    cout << endl;

    /*  lower_bound */
    cout << "lower_bound function, value = 3: " << endl;
    int pos = lower_bound(v.begin(), v.end(), 3) - v.begin();

    cout << "value >= 3 pos: " << pos << " the value = " << *lower_bound(v.begin(), v.end(), 3) << endl;
    cout << endl;
}

// 查找第一个大于某个元素的位置
void UpperBound()
{
    vector<int> v;

    for (int i = 1; i <= 20; i++) {
        v.push_back(i);
    }
    sort(v.begin(), v.end());
    cout << "array: ";
    copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
    cout << endl;

    /*  upper_bound */
    cout << "upper_bound function, value = 3: " << endl;
    int pos = upper_bound(v.begin(), v.end(), 3) - v.begin();
    cout << "value >3 pos: " << pos << " the value = " << *upper_bound(v.begin(), v.end(), 3) << endl;
    cout << endl;
}

// 二分查找某个元素是否出现
void BinarySearch()
{
    vector<int> v;
    for (int i = 1; i <= 20; i++) {
        v.push_back(i);
    }
    sort(v.begin(), v.end());
    cout << "array: ";
    copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
    cout << endl;

    /*  binary_search */
    cout << "binary_search function value = 3: " << endl;
    cout << "3 is " << (binary_search(v.begin(), v.end(), 3) ? "" : "not ") << " in array" << endl;

    /*  binary_search */
    cout << "binary_search function value = 6: " << endl;
    cout << "6 is " << (binary_search(v.begin(), v.end(), 6) ? "" : "not ") << " in array" << endl;
    cout << endl;
}

bool Greater(int i, int j)
{
    return (i > j);
}

// 查找某个元素出现的起止位置
void EqualRange()
{
    int ints[] = { 10,20,30,30,20,10,10,20 };
    vector<int> v(ints, ints + 8);                         // 10 20 30 30 20 10 10 20
    pair<vector<int>::iterator, vector<int>::iterator> bounds;

    cout << "array: ";
    copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
    cout << endl;

    // using default comparison:
    sort(v.begin(), v.end());                              // 10 10 10 20 20 20 30 30
    bounds = equal_range(v.begin(), v.end(), 20);            //          ^        ^
    cout << "equal_range function, value = 20: " << endl;
    cout << "bounds at positions " << (bounds.first - v.begin());
    cout << " and " << (bounds.second - v.begin()) << endl;

    // using "Greater" as comp:
    sort(v.begin(), v.end(), Greater);                   // 30 30 20 20 20 10 10 10
    bounds = equal_range(v.begin(), v.end(), 20, Greater); //       ^        ^
    cout << "equal_range function, value = 20: " << endl;
    cout << "bounds at positions " << (bounds.first - v.begin());
    cout << " and " << (bounds.second - v.begin()) << endl;
    cout << endl;
}


// 当前排列的前一个排列
void PrevPermutation()
{
    vector<int> num(3, 0);
    num = { 1,2,3 };
    cout << "prev_permutation function, value = [1, 2, 3]: " << endl;
    do {
        cout << num[0] << " " << num[1] << " " << num[2] << endl;
    } while (prev_permutation(num.begin(), num.end()));
    cout << endl;
}

// 当前排列的下一个排列
void NextPermutation()
{
    vector<int> num(3, 0);
    num = { 1,2,3 };
    cout << "next_permutation function, value = [1, 2, 3]: " << endl;
    do {
        cout << num[0] << " " << num[1] << " " << num[2] << endl;
    } while (next_permutation(num.begin(), num.end()));
    cout << endl;
}

int main(int argc, char** argv)
{
    LowerBound();
    UpperBound();
    BinarySearch();
    EqualRange();
    PrevPermutation();
    NextPermutation();
    return 0;
}



  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-08-03 10:58:44  更:2021-08-03 11:00:07 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/9 19:37:48-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码