C++算法库 不修改内容的序列操作函数


/* adjacent_find  查找两个相邻的等价元素*/

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

/* adjacent_find  查找两个相邻的等价元素*/
bool myfunction(int i , int j)
{
    return (i == j);
}

void adjacentFineFunction()
{
    int myints[] = {5,5,51,30,30,20,10,10,20};
    vector<int> myvector (myints,myints+8);
    vector<int> ::iterator it;
    
    //using default comparison
    it = adjacent_find(myvector.begin(), myvector.end());
    if (it != myvector.end())
        cout<<"the first pair of repeated elements are : "<<*it <<endl;
    
    
    //using predicate comparison:
    it = adjacent_find(++it, myvector.end(), myfunction);
    
    if (it != myvector.end()) {
        cout<<"the second pair of repeated elements are: "<<*it <<endl;
        
    }
    
    
}


/*all_of 检测在给定范围中是否所有元素都满足给定的条件*/


int main(int argc, const char * argv[])
{
    /*adjacent_find  查找两个相邻的等价元素*/
    adjacentFineFunction();
    return 0;
}

输出结果

the first pair of repeated elements are : 5
the second pair of repeated elements are: 30


/*all_of 检测在给定范围中是否所有元素都满足给定的条件*/

#include <iostream>
#include <algorithm>
#include <array>
using namespace std;

/*all_of 检测在给定范围中是否所有元素都满足给定的条件*/

int main(int argc, const char * argv[])
{


    array<int, 8> foo = {3,5,7,11,13,17,19,13};
    if (all_of(foo.begin(), foo.end(), [](int i){ return i % 2;})) {
        cout <<"All the elements are odd numbers"<<endl;
    }
    return 0;
}

输出结果

All the elements are odd numbers


/*any_of 检测在给定范围中是否存在元素满足给定条件*/

#include <iostream>
#include <algorithm>
#include <array>
using namespace std;

/*any_of 检测在给定范围中是否存在元素满足给定条件*/
int main(int argc, const char * argv[])
{
    array<int, 7> foo = {0,1,-1,3,-3,5,-5};
    if (any_of(foo.begin(), foo.end(), [](int i){return i< 0;})) {
        cout<<"There are negative elements in the range."<<endl;
    }
    return 0;
}

输出结果:

There are negative elements in the range.


/*count 返回值等价于给定值的元素的个数*/

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

/*count 返回值等价于给定值的元素的个数*/
int main(int argc, const char * argv[])
{
    int myints[] = {10,20,30,30,20,10,10,20}; //8 elements
    int mycount = (int)count(myints, myints + 8, 10);
    cout<< "10 appears " <<mycount<<" times."<<endl;
    
    //counting elements in container:
    vector<int> myvector(myints,myints + 8);
    mycount = (int)count(myvector.begin(), myvector.end(), 20);
    cout<< "20 appears " << mycount <<" times."<<endl;
    return 0;
}

输出结果:

10 appears 3 times.
20 appears 3 times.

/*count_if 返回值满足给定条件的元素的个数*/

/*count_if 返回值满足给定条件的元素的个数*/
#include <iostream>     //std::count
#include <algorithm>    //std::count_if
#include <vector>       //std::vector

using namespace std;

bool IsOdd(int i){return ((i % 2) == 1);}
int main(int argc, const char * argv[])
{
    vector<int> myvector;
    for (int i = 1; i < 10; i++) {
        myvector.push_back(i); // myvector : 1 2 3 4 5 6 7 8 9
    }
    
    int mycount  = (int)count_if(myvector.begin(), myvector.end(), IsOdd);
    cout<<"myvector contains "<<mycount << "odd values."<<endl;
   
    return 0;
}

输出结果:

myvector contains 5odd values.


/*equal 返回两个范围是否相等*/

/*equal 返回两个范围是否相等*/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool mypredicate(int i, int j)
{
    return (i == j);
}
int main(int argc, const char * argv[])
{
    int myints[] = {20,40,60,80,100};
    vector<int> myvector(myints , myints + 5);
    
    //using default comparison:
    if (equal(myvector.begin(), myvector.end(), myints)) {
        cout <<"The contents of both sequences are equal."<<endl;
    }
    else{
        cout<<"The contents of both sequences differ."<<endl;
    }
    return 0;
}

输出结果:

The contents of both sequences are equal.

/*find 返回第一个值等价于给定值得元素*/

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main(int argc, const char * argv[])
{
    int myints[] ={ 10, 20, 30 ,40 };
    int* p;
    p = find(myints, myints + 4, 30);
    ++p;
    cout<<"The element following 30 is " <<*p<<endl;
    
    vector<int> myvector(myints,myints + 4);
    vector<int>::iterator it;
    
    it = find(myvector.begin(), myvector.end(), 30);
    ++it;
    cout<<"The element following 30 is " << *it <<endl;
    
        return 0;
}



输出结果:

The element following 30 is 40
The element following 30 is 40

/*find_end 查找范围 A 中与范围 B等价的子范围最后出现的位置*/

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;


bool myfunction(int i,int j)
{
    return (i == j);
}
int main(int argc, const char * argv[])
{
    int myints[] = {1,2,3,4,5,1,2,3,4,5};
    vector<int> haystack(myints,myints + 10);
    int needle1[] = {2,3,4};
    
    //using default comparison:
    vector<int>::iterator it;
    it = find_end(haystack.begin(), haystack.end(), needle1, needle1 + 3);
    
    if (it != haystack.end())
        cout<< "needle1 last found at position " <<(it - haystack.begin())<<endl;
    
    
    int needle2[] = {5,1,2};
    
    //using predicate comparison
    it = find_end(haystack.begin(), haystack.end(), needle2, needle2 + 3, myfunction);
    
    if (it != haystack.end())
        cout<<"needle2 last found at position " <<(it - haystack.begin()) <<endl;
        return 0;
}

结构输出:

needle1 last found at position 6
needle2 last found at position 4

/*find_first_of  查找范围 A 中第一个与范围 B 中任一元素等价的元素的位置*/

#include <iostream>
#include <algorithm>
#include <vector>
#include <cctype>

using namespace std;
bool comp_case_insensitive(char c1,char c2){
    return (tolower(c1) == tolower(c2));
}
int main(int argc, const char * argv[])
{
    
    
    int mychars[] = {‘M‘,‘b‘,‘c‘,‘B‘,‘N‘,‘C‘};
    vector<char> haystack(mychars,mychars + 6);
    vector<char>::iterator it;
    
    int needle[] = {‘B‘,‘9‘,‘C‘};
    
    //using default comparison:
    it = find_first_of(haystack.begin(), haystack.end(), needle, needle + 3);
    
    if (it != haystack.end())
        cout<<"The first match is: " << *it<<endl;
    
    //using predicate comparison
    it = find_first_of(haystack.begin(), haystack.end(), needle, needle + 3, comp_case_insensitive);
    
    if (it != haystack.end())
        cout<<"The first match is: " <<*it<<endl;
    
    return 0;
}



/*for_each 对范围中的每个元素调用指定函数*/

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

void myfunction(int i){
    cout<<" " <<i;
}

struct myclass {
    void operator() (int i){cout<<" " <<i;}
}myobject;




int main(int argc, const char * argv[])
{
    vector<int> myvector;
    myvector.push_back(10);
    myvector.push_back(20);
    myvector.push_back(30);
    cout<<"myvector contains: ";
    for_each(myvector.begin(), myvector.end(), myfunction);
    cout<<endl;
    
    cout<<"myvector contains: ";
    for_each(myvector.begin(), myvector.end(), myobject);
    cout<<endl;
    
    
    return 0;
}


/*mismatch 返回两个范围中第一个元素不等价的位置*/

#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>

using namespace std;

bool mypredicate(int i ,int j){
    return  (i == j);
}
int main(int argc, const char * argv[])
{
    
    
    vector<int> myvector;
    for (int i = 1; i < 6; i++)
        myvector.push_back(i * 10);
    int myints[] = {10,20,80,320,1024};
    pair<vector<int>::iterator, int*> mypair;
    
    //using default xomparison:
    mypair = mismatch(myvector.begin(), myvector.end(), myints);
    cout<<"First mismatching elements: "<<*mypair.first;
    cout<< " and " <<*mypair.second<<endl;
    
    ++mypair.first;++mypair.second;
    
    //using predicate comparison:
    mypair = mismatch(mypair.first, myvector.end(), mypair.second, mypredicate);
    cout<<"Second mismatching elements: " << *mypair.first <<" and "<<*mypair.second<<endl;
    
        return 0;
}

/*none_of 检测在给定范围中是否不存在元素满足给定的条件*/

#include <iostream>
#include <algorithm>
#include <array>

using namespace std;


int main(int argc, const char * argv[])
{
    
    array<int, 8> foo = {1,2,4,8,16,32,64,128};
    if (none_of(foo.begin(), foo.end(), [](int i){return i < 60;})) {
        cout<<"There are no negative elements in the range."<<endl;
    }
    
    return 0;
}

/*search 在范围 A 中查找第一个与范围 B 等价的子范围的位置*/

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool mypredicate(int i,int j){
    return (i == j);
}


int main(int argc, const char * argv[])
{
    vector<int> haystack;
    for (int i = 1; i < 10; i++)
        haystack.push_back(i * 10);
    
    
    //using default comparison
    int needle1[] = {20,30,40,50};
    vector<int>::iterator it;
    it = search(haystack.begin(), haystack.end(), needle1, needle1 + 4);
    
    if (it != haystack.end())
        cout<<"needle1 found at position " << (it - haystack.begin()) <<endl;
    else
        cout<<"needle1 not found"<<endl;
    
    //using predicate comparison:
    int needle2[] = {20,30,50};
    it = search(haystack.begin(), haystack.end(), needle2, needle2 + 3, mypredicate);
    
    if (it != haystack.end())
        cout<<"needle2 found at position " <<(it - haystack.begin()) <<endl;
    else
        cout<<"needle2 not found"<<endl;
        
        
    return 0;
}

/*search_n 在给定范围中查找第一个连续 n 个元素都等价于给定值的子范围的位置*/

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool mypredicate(int i,int j){
    return (i == j);
}


int main(int argc, const char * argv[])
{
    int myints[] = {10,20,30,30,20,10,10,20};
    vector<int> myvector(myints,myints + 8);
    vector<int>::iterator it;
    
    //using default comparison:
    it = search_n(myvector.begin(), myvector.end(), 2, 30);
    
    if (it != myvector.end())
        cout<<"two 30s found at position "<<(it -myvector.begin())<<endl;
    else
        cout<<"match not found"<<endl;
    
    
    //using predicate comparison:
    it = search_n(myvector.begin(), myvector.end(), 2, 10,mypredicate);
    
    if (it != myvector.end())
        cout<<"two 10s found at position "<<int(it - myvector.begin())<<endl;
    else
        cout<<"match not found"<<endl;
    return 0;
}







C++算法库 不修改内容的序列操作函数

上一篇:sql server 使用链接服务器连接Oracle,openquery查询数据


下一篇:Python的静态方法和类成员方法都可以被类或实例访问,两者概念不容易理清,但还是有区别的