C++學習筆記(十七)(STL常用算法)

本筆記主要來源於教程https://www.bilibili.com/video/av41559729?p=1

5 STL- 常用算法

概述:

  • 算法主要是由頭文件<algorithm> <functional> <numeric>組成。
  • <algorithm>是所有STL頭文件中最大的一個,範圍涉及到比較、 交換、查找、遍歷操作、複製、修改等等
  • <numeric>體積很小,只包括幾個在序列上面進行簡單數學運算的模板函數
  • <functional>定義了一些模板類,用以聲明函數對象。

5.1 常用遍歷算法

學習目標:

  • 掌握常用的遍歷算法

算法簡介:

  • for_each //遍歷容器
  • transform //搬運容器到另一個容器中

5.1.1 for_each

功能描述:

  • 實現遍歷容器

函數原型:

  • for_each(iterator beg, iterator end, _func);

// 遍歷算法 遍歷容器元素

// beg 開始迭代器

// end 結束迭代器

// _func 函數或者函數對象

示例:

#include 
#include 

//常用的遍歷算法for_each
//普通函數
void print01(int val)
{
    cout << val << " ";
}
//函數對象
class print02
{
public:
    void operator()(int val)
    {
        cout << val << " ";
    }
};


//for_each算法基本用法
void test01() 
{

    vector v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }

    //遍歷算法
    for_each(v.begin(), v.end(), print01);
    cout << endl;

    for_each(v.begin(), v.end(), print02());
    cout << endl;
}

int main() 
{

    test01();

    system("pause");

    return 0;
}

總結:for_each在實際開發中是最常用遍歷算法,需要熟練掌握

5.1.2 transform

功能描述:

  • 搬運容器到另一個容器中

函數原型:

  • transform(iterator beg1, iterator end1, iterator beg2, _func);

//beg1 源容器開始迭代器

//end1 源容器結束迭代器

//beg2 目標容器開始迭代器

//_func 函數或者函數對象//可以邏輯運算後搬運

示例:

#include
#include

//常用遍歷算法  搬運 transform

class TransForm
{
public:
    int operator()(int val)//可以在這裏做邏輯運算
    {
        return val;
    }

};

class MyPrint
{
public:
    void operator()(int val)
    {
        cout << val << " ";
    }
};

void test01()
{
    vectorv;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }

    vectorvTarget; //目標容器

    vTarget.resize(v.size()); // 目標容器必需要提前開闢空間

    transform(v.begin(), v.end(), vTarget.begin(), TransForm());

    for_each(vTarget.begin(), vTarget.end(), MyPrint());
    cout << endl;
}

int main() 
{

    test01();

    system("pause");

    return 0;
}

總結: 搬運的目標容器必須要提前開闢空間,否則無法正常搬運

5.2 常用查找算法

學習目標:

  • 掌握常用的查找算法

算法簡介:

  • find         //查找元素
  • find_if         //按條件查找元素
  • adjacent_find         //查找相鄰重複元素
  • binary_search         //二分查找法
  • count         //統計元素個數
  • count_if         //按條件統計元素個數

5.2.1 find

功能描述:

  • 查找指定元素,找到返回指定元素的迭代器,找不到返回結束迭代器end()

函數原型:

  • find(iterator beg, iterator end, value);

// 按值查找元素,找到返回指定位置迭代器,找不到返回結束迭代器位置

// beg 開始迭代器

// end 結束迭代器

// value 查找的元素

示例:

#include 
#include 
#include 

//常用查找算法
//find

//查找  內置數據類型
void test01() {

    vector v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i + 1);
    }
    //查找容器中是否有 5 這個元素
    vector::iterator it = find(v.begin(), v.end(), 5);
    if (it == v.end())
    {
        cout << "沒有找到!" << endl;
    }
    else
    {
        cout << "找到:" << *it << endl;
    }
}

class Person {
public:
    Person(string name, int age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }
    //重載==
    bool operator==(const Person& p)
    {
        if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
        {
            return true;
        }
        return false;
    }

public:
    string m_Name;
    int m_Age;
};

//查找 自定義數據類型
void test02() {

    vector v;

    //創建數據
    Person p1("aaa", 10);
    Person p2("bbb", 20);
    Person p3("ccc", 30);
    Person p4("ddd", 40);

    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);

    vector::iterator it = find(v.begin(), v.end(), p2);
    if (it == v.end())
    {
        cout << "沒有找到!" << endl;
    }
    else
    {
        cout << "找到姓名:" << it->m_Name << " 年齡: " << it->m_Age << endl;
    }
}
int main()
{
    test01();
    test02();

    system("pause");

    return 0;

}

總結: 利用find可以在容器中找指定的元素,返回值是迭代器

5.2.2 find_if

功能描述:

  • 按條件查找元素

函數原型:

  • find_if(iterator beg, iterator end, _Pred);

// 按值查找元素,找到返回指定位置迭代器,找不到返回結束迭代器位置

// beg 開始迭代器

// end 結束迭代器

// _Pred 函數或者謂詞(返回bool類型的仿函數)

示例:

#include 
#include 
#include 

//常用查找算法find_if
//1、內置數據類型
class GreaterFive
{
public:
    bool operator()(int val)
    {
        return val > 5;
    }
};

void test01() {

    vector v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i + 1);
    }

    vector::iterator it = find_if(v.begin(), v.end(), GreaterFive());
    if (it == v.end()) {
        cout << "沒有找到!" << endl;
    }
    else {
        cout << "找到大於5的數字:" << *it << endl;
    }
}

//2、查找自定義數據類型
class Person {
public:
    Person(string name, int age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }
public:
    string m_Name;
    int m_Age;
};

class Greater20
{
public:
    bool operator()(Person &p)
    {
        return p.m_Age > 20;
    }

};

void test02() {

    vector v;

    //創建數據
    Person p1("aaa", 10);
    Person p2("bbb", 20);
    Person p3("ccc", 30);
    Person p4("ddd", 40);

    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);

//找年齡大於20的人
    vector::iterator it = find_if(v.begin(), v.end(), Greater20());
    if (it == v.end())
    {
        cout << "沒有找到!" << endl;
    }
    else
    {
        cout << "找到姓名:" << it->m_Name << " 年齡: " << it->m_Age << endl;
    }
}

int main() {

    test01();

    test02();

    system("pause");

    return 0;
}

總結:find_if按條件查找使查找更加靈活,提供的仿函數可以改變不同的策略

5.2.3 adjacent_find

功能描述:

  • 查找相鄰重複元素

函數原型:

  • adjacent_find(iterator beg, iterator end);

// 查找相鄰重複元素,返回相鄰元素的第一個位置的迭代器

// beg 開始迭代器

// end 結束迭代器

示例:

#include 
#include 

void test01()
{
    vector v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(5);
    v.push_back(2);
    v.push_back(4);
    v.push_back(4);
    v.push_back(3);

    //查找相鄰重複元素
    vector::iterator it = adjacent_find(v.begin(), v.end());
    if (it == v.end()) {
        cout << "找不到!" << endl;
    }
    else {
        cout << "找到相鄰重複元素爲:" << *it << endl;
    }
}

int main() {

    test01();

    system("pause");

    return 0;
}

總結:面試題中如果出現查找相鄰重複元素,記得用STL中的adjacent_find算法

5.2.4 binary_search

功能描述:

  • 查找指定元素是否存在

函數原型:

  • bool binary_search(iterator beg, iterator end, value);

// 查找指定的元素,查到 返回true 否則false

// 注意: 在無序序列中不可用

// beg 開始迭代器

// end 結束迭代器

// value 查找的元素

示例:

#include 
#include 

//常用查找算法binary_search
void test01()
{
    vectorv;

    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    //二分查找
    //注意:容器必須是有序的序列  如果是無序序列,結果未知!
    bool ret = binary_search(v.begin(), v.end(), 9);
    if (ret)
    {
        cout << "找到了" << endl;
    }
    else
    {
        cout << "未找到" << endl;
    }
}

int main() {

    test01();

    system("pause");

    return 0;
}

總結:二分查找法查找效率很高,值得注意的是查找的容器中元素必須的有序序列

5.2.5 count

功能描述:

  • 統計元素個數

函數原型:

  • count(iterator beg, iterator end, value);

// 統計元素出現次數

// beg 開始迭代器

// end 結束迭代器

// value 統計的元素

示例:

#include 
#include 

//常用查找算法 count
//1、統計內置數據類型
void test01()
{
    vector v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(4);
    v.push_back(5);
    v.push_back(3);
    v.push_back(4);
    v.push_back(4);

    int num = count(v.begin(), v.end(), 4);

    cout << "4的個數爲: " << num << endl;
}

//2、統計自定義數據類型
class Person
{
public:
    Person(string name, int age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }
    bool operator==(const Person & p)
    {
        if (this->m_Age == p.m_Age)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    string m_Name;
    int m_Age;
};

void test02()
{
    vector v;

    Person p1("劉備", 35);
    Person p2("關羽", 35);
    Person p3("張飛", 35);
    Person p4("趙雲", 30);
    Person p5("曹操", 25);

    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);

    Person p("諸葛亮", 35);

    int num = count(v.begin(), v.end(), p);
    cout << "num = " << num << endl;
}
int main() {

    test01();

    test02();

    system("pause");

    return 0;
}

總結: 統計自定義數據類型時候,需要配合重載 operator==

5.2.6 count_if

功能描述:

  • 按條件統計元素個數

函數原型:

  • count_if(iterator beg, iterator end, _Pred);

// 按條件統計元素出現次數

// beg 開始迭代器

// end 結束迭代器

// _Pred 謂詞

示例:

#include 
#include 

//常用的查找算法 count_if
class Greater4
{
public:
    bool operator()(int val)
    {
        return val >= 4;
    }
};

//內置數據類型
void test01()
{
    vector v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(4);
    v.push_back(5);
    v.push_back(3);
    v.push_back(4);
    v.push_back(4);

    int num = count_if(v.begin(), v.end(), Greater4());

    cout << "大於4的個數爲: " << num << endl;
}

//自定義數據類型
class Person
{
public:
    Person(string name, int age)
    {
        this->m_Name = name;
        this->m_Age = age;
    }

    string m_Name;
    int m_Age;
};

class AgeLess35
{
public:
    bool operator()(const Person &p)
    {
        return p.m_Age < 35;
    }
};
void test02()
{
    vector v;

    Person p1("劉備", 35);
    Person p2("關羽", 35);
    Person p3("張飛", 35);
    Person p4("趙雲", 30);
    Person p5("曹操", 25);

    v.push_back(p1);
    v.push_back(p2);
    v.push_back(p3);
    v.push_back(p4);
    v.push_back(p5);

    int num = count_if(v.begin(), v.end(), AgeLess35());
    cout << "小於35歲的個數:" << num << endl;
}


int main() {

    test01();

    test02();

    system("pause");

    return 0;
}

總結:按值統計用count,按條件統計用count_if

5.3常用排序算法

學習目標:

  • 掌握常用的排序算法

算法簡介:

  • sort         //對容器內元素進行排序
  • random_shuffle         //洗牌 指定範圍內的元素隨機調整次序
  • merge         // 容器元素合併,並存儲到另一容器中
  • reverse         // 反轉指定範圍的元素

5.3.1 sort

功能描述:

  • 對容器內元素進行排序

函數原型:

  • sort(iterator beg, iterator end, _Pred);

// 按值查找元素,找到返回指定位置迭代器,找不到返回結束迭代器位置

// beg 開始迭代器

// end 結束迭代器

// _Pred 謂詞

示例:

#include 
#include 
#include 

//常用排序算法 sort

void myPrint(int val)
{
    cout << val << " ";
}

void test01()
{
    vector v;
    v.push_back(10);
    v.push_back(30);
    v.push_back(50);
    v.push_back(20);
    v.push_back(40);

    //sort默認從小到大排序
    sort(v.begin(), v.end());
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;

    //從大到小排序
    sort(v.begin(), v.end(), greater());//內建函數對象
    for_each(v.begin(), v.end(), myPrint);
    cout << endl;
}

int main()
{

    test01();

    system("pause");

    return 0;
}

總結:sort屬於開發中最常用的算法之一,需熟練掌握

5.3.2 random_shuffle

功能描述:

  • 洗牌 指定範圍內的元素隨機調整次序

函數原型:

  • random_shuffle(iterator beg, iterator end);

// 指定範圍內的元素隨機調整次序

// beg 開始迭代器

// end 結束迭代器

示例:

#include 
#include 
#include 

//常用排序算法 random_shuffle
class myPrint
{
public:
    void operator()(int val)
    {
        cout << val << " ";
    }
};

void test01()
{
    srand((unsigned int)time(NULL));//不加的話每次打亂結果相同
    vector v;
    for (int i = 0; i < 10; i++)
    {
        v.push_back(i);
    }
    for_each(v.begin(), v.end(), myPrint());
    cout << endl;

    //打亂順序
    random_shuffle(v.begin(), v.end());
    for_each(v.begin(), v.end(), myPrint());
    cout << endl;
}

int main() {

    test01();

    system("pause");

    return 0;
}

總結:random_shuffle洗牌算法比較實用,使用時記得加隨機數種子

5.3.3 merge

功能描述:

  • 兩個容器元素合併,並存儲到另一容器中

函數原型:

  • merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

// 容器元素合併,並存儲到另一容器中

// 注意: 兩個容器必須是有序的

// beg1 容器1開始迭代器
// end1 容器1結束迭代器
// beg2 容器2開始迭代器
// end2 容器2結束迭代器
// dest 目標容器開始迭代器

示例:

#include 
#include 

//常用排序算法 merge
class myPrint
{
public:
    void operator()(int val)
    {
        cout << val << " ";
    }
};

void test01()
{
    vector v1;
    vector v2;
    for (int i = 0; i < 10; i++)
    {
        v1.push_back(i);
        v2.push_back(i + 1);
    }

    vector vtarget;
    //目標容器需要提前開闢空間
    vtarget.resize(v1.size() + v2.size());
    //合併  需要兩個有序序列
    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vtarget.begin());
    for_each(vtarget.begin(), vtarget.end(), myPrint());
    cout << endl;
}

int main() {

    test01();

    system("pause");

    return 0;
}

總結:merge合併的兩個容器必須是有序序列

5.3.4 reverse

功能描述:

  • 將容器內元素進行反轉

函數原型:

  • reverse(iterator beg, iterator end);

// 反轉指定範圍的元素

// beg 開始迭代器

// end 結束迭代器

示例:

#include 
#include 

//常用排序算法 reverse
class myPrint
{
public:
    void operator()(int val)
    {
        cout << val << " ";
    }
};

void test01()
{
    vector v;
    v.push_back(10);
    v.push_back(30);
    v.push_back(50);
    v.push_back(20);
    v.push_back(40);

    cout << "反轉前: " << endl;
    for_each(v.begin(), v.end(), myPrint());
    cout << endl;

    cout << "反轉後: " << endl;

    reverse(v.begin(), v.end());
    for_each(v.begin(), v.end(), myPrint());
    cout << endl;
}

int main() 
{

    test01();

    system("pause");

    return 0;
}

總結:reverse反轉區間內元素,面試題可能涉及到

5.4 常用拷貝和替換算法

學習目標:

  • 掌握常用的拷貝和替換算法

算法簡介:

  • copy       // 容器內指定範圍的元素拷貝到另一容器中
  • replace      // 將容器內指定範圍的舊元素修改爲新元素
  • replace_if      // 容器內指定範圍滿足條件的元素替換爲新元素
  • swap      // 互換兩個容器的元素

5.4.1 copy

功能描述:

  • 容器內指定範圍的元素拷貝到另一容器中

函數原型:

  • copy(iterator beg, iterator end, iterator dest);

// 按值查找元素,找到返回指定位置迭代器,找不到返回結束迭代器位置

// beg 開始迭代器

// end 結束迭代器

// dest 目標起始迭代器

示例:

#include 
#include 

//常用拷貝和替換算法 copy
class myPrint//用函數也可以
{
public:
    void operator()(int val)
    {
        cout << val << " ";
    }
};

void test01()
{
    vector v1;
    for (int i = 0; i < 10; i++) {
        v1.push_back(i + 1);
    }
    vector v2;
    v2.resize(v1.size());
    copy(v1.begin(), v1.end(), v2.begin());

    for_each(v2.begin(), v2.end(), myPrint());
    cout << endl;
}

int main()
{

    test01();

    system("pause");

    return 0;
}

總結:利用copy算法在拷貝時,目標容器記得提前開闢空間

5.4.2 replace

功能描述:

  • 將容器內指定範圍的舊元素修改爲新元素

函數原型:

  • replace(iterator beg, iterator end, oldvalue, newvalue);

// 將區間內舊元素 替換成 新元素

// beg 開始迭代器

// end 結束迭代器

// oldvalue 舊元素

// newvalue 新元素

示例:

#include 
#include 

//常用拷貝和替換算法 replace
class myPrint
{
public:
    void operator()(int val)
    {
        cout << val << " ";
    }
};

void test01()
{
    vector v;
    v.push_back(20);
    v.push_back(30);
    v.push_back(20);
    v.push_back(40);
    v.push_back(50);
    v.push_back(10);
    v.push_back(20);

    cout << "替換前:" << endl;
    for_each(v.begin(), v.end(), myPrint());
    cout << endl;

    //將容器中的20 替換成 2000
    cout << "替換後:" << endl;
    replace(v.begin(), v.end(), 20, 2000);
    for_each(v.begin(), v.end(), myPrint());
    cout << endl;
}

int main()
{

    test01();

    system("pause");

    return 0;
}

總結:replace會替換區間內滿足條件的元素

5.4.3 replace_if

功能描述:

  • 將區間內滿足條件的元素,替換成指定元素

函數原型:

  • replace_if(iterator beg, iterator end, _pred, newvalue);

// 按條件替換元素,滿足條件的替換成指定元素

// beg 開始迭代器

// end 結束迭代器

// _pred 謂詞

// newvalue 替換的新元素

示例:

#include 
#include 

//常用拷貝和替換算法 replace_if
class myPrint
{
public:
    void operator()(int val)
    {
        cout << val << " ";
    }
};

class ReplaceGreater30
{
public:
    bool operator()(int val)
    {
        return val >= 30;
    }

};

void test01()
{
    vector v;
    v.push_back(20);
    v.push_back(30);
    v.push_back(20);
    v.push_back(40);
    v.push_back(50);
    v.push_back(10);
    v.push_back(20);

    cout << "替換前:" << endl;
    for_each(v.begin(), v.end(), myPrint());
    cout << endl;

    //將容器中大於等於的30 替換成 3000
    cout << "替換後:" << endl;
    replace_if(v.begin(), v.end(), ReplaceGreater30(), 3000);
    for_each(v.begin(), v.end(), myPrint());
    cout << endl;
}

int main()
{

    test01();

    system("pause");

    return 0;
}

總結:replace_if按條件查找,可以利用仿函數靈活篩選滿足的條件

5.4.4 swap

功能描述:

  • 互換兩個容器的元素

函數原型:

  • swap(container c1, container c2);

// 互換兩個容器的元素

// c1容器1

// c2容器2

示例:

#include 
#include 

//常用拷貝和替換算法 swap
class myPrint
{
public:
    void operator()(int val)
    {
        cout << val << " ";
    }
};

void test01()
{
    vector v1;
    vector v2;
    for (int i = 0; i < 10; i++) {
        v1.push_back(i);
        v2.push_back(i + 100);
    }

    cout << "交換前: " << endl;
    for_each(v1.begin(), v1.end(), myPrint());
    cout << endl;
    for_each(v2.begin(), v2.end(), myPrint());
    cout << endl;

    cout << "交換後: " << endl;
    swap(v1, v2);
    for_each(v1.begin(), v1.end(), myPrint());
    cout << endl;
    for_each(v2.begin(), v2.end(), myPrint());
    cout << endl;
}

int main() 
{

    test01();

    system("pause");

    return 0;
}

總結:swap交換容器時,注意交換的容器要同種類型

5.5 常用算術生成算法

學習目標:

  • 掌握常用的算術生成算法

注意:

  • 算術生成算法屬於小型算法,使用時包含的頭文件爲 #include <numeric>

算法簡介:

  • accumulate      // 計算容器元素累計總和
  • fill      // 向容器中添加元素

    5.5.1 accumulate

    功能描述:

    • 計算區間內 容器元素累計總和

    函數原型:

  • accumulate(iterator beg, iterator end, value);
  • // 計算容器元素累計總和

    // beg 開始迭代器

    // end 結束迭代器

    // value 起始值
    示例

    #include 
    #include 
    
    
    //常用算術生成算法
    //計算區間內 容器元素累計總和  accumulate
    void test01()
    {
        vector v;
        for (int i = 0; i <= 100; i++) {
            v.push_back(i);
        }
        //參數3,起始累加值
        int total = accumulate(v.begin(), v.end(), 10);
    
        cout << "total = " << total << endl;
    }
    
    int main() 
    {
    
        test01();
    
        system("pause");
    
        return 0;
    }
    

    總結:accumulate使用時頭文件注意是 numeric,這個算法很實用

    5.5.2 fill

    功能描述:

    • 向容器中填充指定的元素

    函數原型:

  • fill(iterator beg, iterator end, value);
  • // 向容器中填充元素

    // beg 開始迭代器

    // end 結束迭代器

    // value 填充的值

    示例:

    #include 
    #include 
    #include 
    
    //常用算數生成算法 fill 填充
    class myPrint
    {
    public:
        void operator()(int val)
        {
            cout << val << " ";
        }
    };
    
    void test01()
    {
    
        vector v;
        v.resize(10);
    
        //後期重新填充
        fill(v.begin(), v.end(), 100);
    
        for_each(v.begin(), v.end(), myPrint());
        cout << endl;
    }
    
    int main() {
    
        test01();
    
        system("pause");
    
        return 0;
    }
    

    總結:利用fill可以將容器區間內元素填充爲 指定的值

    5.6常用集合算法

    學習目標:

    • 掌握常用的集合算法

    算法簡介:

    • set_intersection         // 求兩個容器的交集
    • set_union         // 求兩個容器的並集
    • set_difference         // 求兩個容器的差集

    5.6.1 set_intersection

    功能描述:

    • 求兩個容器的交集

    函數原型:

  • set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
  • // 求兩個集合的交集

    // 注意:兩個集合必須是有序序列

    // beg1 容器1開始迭代器
    // end1 容器1結束迭代器
    // beg2 容器2開始迭代器
    // end2 容器2結束迭代器
    // dest 目標容器開始迭代器

    #include 
    #include //min
    
    //常用集合算法  set_intersection
    class myPrint
    {
    public:
        void operator()(int val)
        {
            cout << val << " ";
        }
    };
    
    void test01()
    {
        //原容器必須是有序序列
        vector v1;
        vector v2;
        for (int i = 0; i < 10; i++)
        {
            v1.push_back(i);    //0~9
            v2.push_back(i + 5);    //5~14
        }
    
        vector vTarget;
    
        //取兩個裏面較小的值給目標容器開闢空間
        vTarget.resize(min(v1.size(), v2.size()));
    
        //返回目標容器的最後一個元素的迭代器地址
        vector::iterator itEnd =
            set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());//返回最後位置的迭代器
    
    //  for_each(vTarget.begin(), vTarget.end(), myPrint());這種會有0填滿容器 因爲開闢的容量可能會大一些
        for_each(vTarget.begin(), itEnd, myPrint());
        cout << endl;
    }
    
    int main() {
    
        test01();
    
        system("pause");
    
        return 0;
    }
    

    總結:

    • 求交集的兩個集合必須的有序序列
    • 目標容器開闢空間需要從兩個容器中取小值
    • set_intersection返回值是交集中最後一個元素的位置

    5.6.2 set_union

    功能描述:

    • 求兩個集合的並集

    函數原型:

    • set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

    // 求兩個集合的並集

    // 注意:兩個集合必須是有序序列

    // beg1 容器1開始迭代器
    // end1 容器1結束迭代器
    // beg2 容器2開始迭代器
    // end2 容器2結束迭代器
    // dest 目標容器開始迭代器

    示例:

    #include 
    #include 
    
    //常用集合算法 set_union
    class myPrint
    {
    public:
        void operator()(int val)
        {
            cout << val << " ";
        }
    };
    
    void test01()
    {
        vector v1;
        vector v2;
        for (int i = 0; i < 10; i++) {
            v1.push_back(i);
            v2.push_back(i + 5);
        }
    
        vector vTarget;
        //取兩個容器的和給目標容器開闢空間
        vTarget.resize(v1.size() + v2.size());
    
        //返回目標容器的最後一個元素的迭代器地址
        vector::iterator itEnd =
            set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
    
        for_each(vTarget.begin(), itEnd, myPrint());
        cout << endl;
    }
    
    int main() 
    {
    
        test01();
    
        system("pause");
    
        return 0;
    }
    

    總結:

    • 求並集的兩個集合必須的有序序列
    • 目標容器開闢空間需要兩個容器相加
    • set_union返回值是並集中最後一個元素的位置

    5.6.3 set_difference

    功能描述:

    • 求兩個集合的差集

    函數原型:

    • set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);

    // 求兩個集合的差集

    // 注意:兩個集合必須是有序序列

    // beg1 容器1開始迭代器
    // end1 容器1結束迭代器
    // beg2 容器2開始迭代器
    // end2 容器2結束迭代器
    // dest 目標容器開始迭代器

    #include <vector>
    #include <algorithm>
    
    //常用集合算法 set_difference
    class myPrint
    {
    public:
        void operator()(int val)
        {
            cout << val << " ";
        }
    };
    
    void test01()
    {
        vector<int> v1;
        vector<int> v2;
        for (int i = 0; i < 10; i++) {
            v1.push_back(i);
            v2.push_back(i + 5);
        }
    
        vector<int> vTarget;
        //取兩個裏面較大的值給目標容器開闢空間
        vTarget.resize(max(v1.size(), v2.size()));
    
        //返回目標容器的最後一個元素的迭代器地址
        cout << "v1與v2的差集爲: " << endl;
        vector<int>::iterator itEnd =
            set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());
        for_each(vTarget.begin(), itEnd, myPrint());
        cout << endl;
    
    
        cout << "v2與v1的差集爲: " << endl;
        itEnd = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());
        for_each(vTarget.begin(), itEnd, myPrint());
        cout << endl;
    }
    
    int main()
    {
    
        test01();
    
        system("pause");
    
        return 0;
    }
    

    總結:

    • 求差集的兩個集合必須的有序序列
    • 目標容器開闢空間需要從兩個容器取較大值
    • set_difference返回值是差集中最後一個元素的位置

     
    最後歡迎大家訪問我的個人博客青蛙聽禪的個人博客

    發表評論
    所有評論
    還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
    相關文章