STL之算法

来源:互联网 发布:vscode md转html 编辑:程序博客网 时间:2024/05/21 11:28

STL之查找算法

adjacent_find()
在iterator对标识元素范围内,查找一对相邻重复元素,找到则返回指向这对元素的第一个元素的迭代器。否则返回past-the-end。

vector<int> vecInt;vecInt.push_back(1);vecInt.push_back(2);vecInt.push_back(2);vecInt.push_back(4);vecInt.push_back(5);vecInt.push_back(5);vector<int>::iterator it = adjacent_find(vecInt.begin(), vecInt.end());     //*it == 2

binary_search
在有序序列中查找value,找到则返回true。注意:在无序序列中,不可使用。

set<int> setInt;setInt.insert(3);setInt.insert(1);setInt.insert(7);setInt.insert(5);setInt.insert(9);bool bFind = binary_search(setInt.begin(),setInt.end(),5);

count()
利用等于操作符,把标志范围内的元素与输入值比较,返回相等的个数。

vector<int> vecInt;vecInt.push_back(1);vecInt.push_back(2);vecInt.push_back(2);vecInt.push_back(4);vecInt.push_back(2);vecInt.push_back(5);int iCount = count(vecInt.begin(),vecInt.end(),2);  //iCount==3

count_if()
假设vector vecIntA,vecIntA包含1,3,5,7,9元素

//先定义比较函数bool GreaterThree(int iNum){        if(iNum>=3)        {            return true;        }        else        {            return false;        }}int iCount = count_if(vecIntA.begin(), vecIntA.end(), GreaterThree);//此时iCount == 4

find()
find: 利用底层元素的等于操作符,对指定范围内的元素与输入值进行比较。当匹配时,结束搜索,返回该元素的迭代器。
equal_range: 返回一对iterator,第一个表示lower_bound,第二个表示upper_bound。

vector<int> vecInt;vecInt.push_back(1);vecInt.push_back(3);vecInt.push_back(5);vecInt.push_back(7);vecInt.push_back(9);vector<int>::iterator it = find(vecInt.begin(), vecInt.end(), 5);       //*it == 5

find_if()
find_if: 使用输入的函数代替等于操作符执行find。返回被找到的元素的迭代器。 假设vector vecIntA,vecIntA包含1,3,5,3,9元素

vector<int>::it = find_if(vecInt.begin(),vecInt.end(),GreaterThree);//此时 *it==3, *(it+1)==5, *(it+2)==3, *(it+3)==9

综合代码

void main44_adjacent_find(){    vector<int> v1;    v1.push_back(1);    v1.push_back(2);    v1.push_back(2);    v1.push_back(3);    v1.push_back(5);    vector<int>::iterator it =  adjacent_find(v1.begin(), v1.end() );    if (it == v1.end())    {        cout << "没有找到 重复的元素" << endl;    }    else    {        cout << *it << endl;    }    int index = distance(v1.begin(), it);    cout << index << endl;}// 0 1  2  3 ......n-1//二分法 1K = 1024  10次  速度快void main45_binary_search(){    vector<int> v1;    v1.push_back(1);    v1.push_back(3);    v1.push_back(5);    v1.push_back(7);    v1.push_back(9);    bool b = binary_search(v1.begin(), v1.end(), 7);    if (b == true)    {        cout << "找到了" << endl;    }    else    {        cout << "没到了" << endl;    }}void main46_count(){    vector<int> v1;    v1.push_back(1);    v1.push_back(3);    v1.push_back(5);    v1.push_back(7);    v1.push_back(7);    v1.push_back(9);    v1.push_back(7);    int num = count(v1.begin(), v1.end(), 7);    cout << num << endl;}bool GreatThree(int iNum){    if (iNum > 3)    {        return true;    }    return false;}void main46_countif(){    vector<int> v1;    v1.push_back(1);    v1.push_back(3);    v1.push_back(5);    v1.push_back(7);    v1.push_back(7);    v1.push_back(9);    v1.push_back(7);    int num = count_if(v1.begin(), v1.end(), GreatThree);    cout << "num:" << num << endl;}void main47_find_findif(){    vector<int> v1;    v1.push_back(1);    v1.push_back(3);    v1.push_back(5);    v1.push_back(7);    v1.push_back(7);    v1.push_back(9);    v1.push_back(7);    vector<int>::iterator it =  find(v1.begin(), v1.end(), 5);    cout << "*it:" << *it << endl;     //第一个大于3的位置    vector<int>::iterator it2 =  find_if(v1.begin(), v1.end(), GreatThree);    cout << "*it2:" << *it2 << endl; }

STL之排序算法

1.merge()
以下是排序和通用算法:提供元素排序策略
merge: 合并两个有序序列,存放到另一个序列。
例如:
vecIntA,vecIntB,vecIntC是用vector声明的容器,vecIntA已包含1,3,5,7,9元素,vecIntB已包含2,4,6,8元素

vecIntC.resize(9);  //扩大容量merge(vecIntA.begin(),vecIntA.end(),vecIntB.begin(),vecIntB.end(),vecIntC.begin());

此时vecIntC就存放了按顺序的1,2,3,4,5,6,7,8,9九个元素
2.sort()
sort: 以默认升序的方式重新排列指定范围内的元素。若要改排序规则,可以输入比较函数。

//学生类Class CStudent:{public:    CStudent(int iID, string strName)    {        m_iID=iID;          m_strName=strName;     }public:                int m_iID;    string m_strName;}//学号比较函数bool Compare(const CStudent &stuA,const CStudent &stuB){         return (stuA.m_iID<strB.m_iID);}void main(){vector<CStudent> vecStu;vecStu.push_back(CStudent(2,"老二"));vecStu.push_back(CStudent(1,"老大"));vecStu.push_back(CStudent(3,"老三"));vecStu.push_back(CStudent(4,"老四"));sort(vecStu.begin(),vecStu.end(),Compare);//此时,vecStu容器包含了按顺序的"老大对象","老二对象","老三对象","老四对象"}

3.random_shuffle()
random_shuffle: 对指定范围内的元素随机调整次序。
srand(time(0)); //设置随机种子

vector<int> vecInt;vecInt.push_back(1);vecInt.push_back(3);vecInt.push_back(5);vecInt.push_back(7);vecInt.push_back(9);string str("itcastitcast ");random_shuffle(vecInt.begin(), vecInt.end());   //随机排序,结果比如:9,7,1,5,3random_shuffle(str.begin(), str.end());        //随机排序,结果比如:" itstcasticat "

4.reverse()

vector<int> vecInt;vecInt.push_back(1);vecInt.push_back(3);vecInt.push_back(5);vecInt.push_back(7);vecInt.push_back(9);reverse(vecInt.begin(), vecInt.end());      //{9,7,5,3,1}

5.综合代码

void main_merge(){    vector<int> v1;    v1.push_back(1);    v1.push_back(3);    v1.push_back(5);    vector<int> v2;    v2.push_back(2);    v2.push_back(4);    v2.push_back(6);    vector<int> v3;    v3.resize(v1.size() + v2.size() );    merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin() );    printV(v3);}class Student{public:    Student(string name, int id)    {        m_name = name;        m_id = id;    }    void printT()    {        cout << "name: " << m_name << " id " << m_id << endl;    }public:    string  m_name;    int     m_id;};bool CompareS(Student &s1, Student &s2){    return (s1.m_id < s2.m_id);}void main_sort(){    Student s1("老大", 1);    Student s2("老二", 2);    Student s3("老三", 3);    Student s4("老四", 4);    vector<Student> v1;    v1.push_back(s4);    v1.push_back(s1);    v1.push_back(s3);    v1.push_back(s2);    for (vector<Student>::iterator it=v1.begin(); it!=v1.end(); it++)    {        it->printT() ;    }    //sort 根据自定义函数对象 进行自定义数据类型的排序     //替换 算法的统一性 (实现的算法和数据类型的分离) ===>技术手段函数对象    sort(v1.begin(), v1.end(), CompareS );    for (vector<Student>::iterator it=v1.begin(); it!=v1.end(); it++)    {        it->printT() ;    }}void main_random_shuffle(){    vector<int> v1;    v1.push_back(1);    v1.push_back(3);    v1.push_back(5);    v1.push_back(7);    random_shuffle(v1.begin(), v1.end());    printV(v1);    string str = "abcdefg";    random_shuffle(str.begin(), str.end());    cout << "str: " << str << endl;}void main_reverse(){    vector<int> v1;    v1.push_back(1);    v1.push_back(3);    v1.push_back(5);    v1.push_back(7);    reverse(v1.begin(), v1.end());    printV(v1);}

STL之拷贝和替换算法

1.copy()

vector<int> vecIntA;vecIntA.push_back(1);vecIntA.push_back(3);vecIntA.push_back(5);vecIntA.push_back(7);vecIntA.push_back(9);vector<int> vecIntB;vecIntB.resize(5);          //扩大空间copy(vecIntA.begin(), vecIntA.end(), vecIntB.begin());  //vecIntB: {1,3,5,7,9}

2.replace()
replace(beg,end,oldValue,newValue): 将指定范围内的所有等于oldValue的元素替换成newValue。

 vector<int> vecIntA;        vecIntA.push_back(1);        vecIntA.push_back(3);        vecIntA.push_back(5);        vecIntA.push_back(3);        vecIntA.push_back(9);        replace(vecIntA.begin(), vecIntA.end(), 3, 8);      //{1,8,5,8,9}

3.replace_if()
replace_if : 将指定范围内所有操作结果为true的元素用新值替换。
用法举例:
replace_if(vecIntA.begin(),vecIntA.end(),GreaterThree,newVal)
其中vecIntA是用vector声明的容器
GreaterThree 函数的原型是 bool GreaterThree(int iNum)

//把大于等于3的元素替换成8vector<int> vecIntA;vecIntA.push_back(1);vecIntA.push_back(3);vecIntA.push_back(5);vecIntA.push_back(3);vecIntA.push_back(9);replace_if(vecIntA.begin(), vecIntA.end(), GreaterThree, 8);        // GreaterThree的定义在上面。

4.swap()
swap: 交换两个容器的元素

vector<int> vecIntA;vecIntA.push_back(1);vecIntA.push_back(3);vecIntA.push_back(5);vector<int> vecIntB;vecIntB.push_back(2);vecIntB.push_back(4);swap(vecIntA, vecIntB);  //交换

5.综合代码

void main52_copy(){    vector<int> v1;    v1.push_back(1);    v1.push_back(3);    v1.push_back(5);    v1.push_back(7);    vector<int> v2;    v2.resize(v1.size() );    copy(v1.begin(), v1.end(), v2.begin());    printV(v2);}bool great_equal_5(int &n){    if (n>=5)    {        return true;    }    return false;}void main53_replace_replaceif(){    vector<int> v1;    v1.push_back(1);    v1.push_back(3);    v1.push_back(5);    v1.push_back(7);    v1.push_back(3);    replace(v1.begin(), v1.end(), 3, 8);    // >=5    replace_if(v1.begin(), v1.end(), great_equal_5, 1);    printV(v1);}void main54_swap(){    vector<int> v1;    v1.push_back(1);    v1.push_back(3);    v1.push_back(5);    vector<int> v2;    v2.push_back(2);    v2.push_back(4);    v2.push_back(6);    swap(v1, v2);    printV(v1);}

STL之算术和生成算法

1.accumulate()
accumulate: 对指定范围内的元素求和,然后结果再加上一个由val指定的初始值。

#include<numeric>
  vector<int> vecIntA;        vecIntA.push_back(1);        vecIntA.push_back(3);        vecIntA.push_back(5);        vecIntA.push_back(7);        vecIntA.push_back(9);        int iSum = accumulate(vecIntA.begin(), vecIntA.end(), 100);     //iSum==125

2.fill()
fill: 将输入值赋给标志范围内的所有元素。

vector<int> vecIntA;vecIntA.push_back(1);vecIntA.push_back(3);vecIntA.push_back(5);vecIntA.push_back(7);vecIntA.push_back(9);fill(vecIntA.begin(), vecIntA.end(), 8);        //8, 8, 8, 8, 8``

3.综合代码

void main55_accumulate(){    vector<int> v1;    v1.push_back(1);    v1.push_back(3);    v1.push_back(5);    int tmp = accumulate(v1.begin(), v1.end(), 100);    cout << tmp << endl;}void main56_fill(){    vector<int> v1;    v1.push_back(1);    v1.push_back(3);    v1.push_back(5);    fill(v1.begin(), v1.end(), 8);    printV(v1);}

STL之集合算法

set_union(),set_intersection(),set_difference()
set_union: 构造一个有序序列,包含两个有序序列的并集。
set_intersection: 构造一个有序序列,包含两个有序序列的交集。
set_difference: 构造一个有序序列,该序列保留第一个有序序列中存在而第二个有序序列中不存在的元素。

vector<int> vecIntA;vecIntA.push_back(1);vecIntA.push_back(3);vecIntA.push_back(5);vecIntA.push_back(7);vecIntA.push_back(9);vector<int> vecIntB;vecIntB.push_back(1);vecIntB.push_back(3);vecIntB.push_back(5);vecIntB.push_back(6);vecIntB.push_back(8);vector<int> vecIntC;vecIntC.resize(10);//并集set_union(vecIntA.begin(), vecIntA.end(), vecIntB.begin(), vecIntB.end(), vecIntC.begin());     //vecIntC : {1,3,5,6,7,8,9,0,0,0}//交集fill(vecIntC.begin(),vecIntC.end(),0);set_intersection(vecIntA.begin(), vecIntA.end(), vecIntB.begin(), vecIntB.end(), vecIntC.begin());      //vecIntC: {1,3,5,0,0,0,0,0,0,0}//差集fill(vecIntC.begin(),vecIntC.end(),0);set_difference(vecIntA.begin(), vecIntA.end(), vecIntB.begin(), vecIntB.end(), vecIntC.begin());    //vecIntC: {7,9,0,0,0,0,0,0,0,0}

STL之遍历算法

1.for_each()
for_each: 用指定函数依次对指定范围内所有元素进行迭代访问。该函数不得修改序列中的元素。

void show(const int &iItem){    cout << iItem;}main(){    int iArray[] = {0,1,2,3,4};    vector<int> vecInt(iArray,iArray+sizeof(iArray)/sizeof(iArray[0]));    for_each(vecInt.begin(), vecInt.end(), show);//结果打印出0 1 2 3 4}

2.transform()
transform: 与for_each类似,遍历所有元素,但可对容器的元素进行修改

int increase (int i)  {      return i+1;   }  main(){    vector<int> vecIntA;    vecIntA.push_back(1);    vecIntA.push_back(3);    vecIntA.push_back(5);    vecIntA.push_back(7);    vecIntA.push_back(9);    transform(vecIntA.begin(),vecIntA.end(),vecIntA.begin(),increase);      //vecIntA : {2,4,6,8,10}}
原创粉丝点击