码迷,mamicode.com
首页 > 编程语言 > 详细

C++ STD Gems05

时间:2019-12-30 14:44:27      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:stream   sea   find   根据   func   char*   this   type   std   

find、find_if、find_first_of、mismatch、search、adjacent_find

#include <iostream>
#include <vector>
#include <string>
#include <iterator>
#include <algorithm>

template<class Container>
void write_to_cout(Container& container, const char* delimiter = " ")
{
    std::copy(container.begin(), container.end(),
        std::ostream_iterator<typename Container::value_type>(std::cout, delimiter) );
}

void test0()
{
    std::vector<int> a = {0, 1, 2, 3, 4, 5, 6, 7, 8};
    //std::vector<std::string> b = {"zero", "one", "two", "three", "four", "five", "six", "seven"};
    write_to_cout(a);
    std::cout << std::endl;

    //test algorithm
    //找到a中第一个元素为4的位置,返回该迭代器
    auto i = std::find(a.begin(), a.end(), 4);
    std::cout << i - a.begin() << std::endl;
}

void test1()
{
    // std::vector<int> a = {0, 1, 2, 3, 4, 5, 6, 7, 8};
    std::vector<std::string> b = {"zero", "one", "two", "three", "four", "five", "three", "seven"};
    write_to_cout(b);
    std::cout << std::endl;

    //test algorithm
    //找到b中最后一个three出现的位置,注意迭代器运算
    auto i = std::find(b.rbegin(), b.rend(), "three");
    std::cout << b.rend() - i - 1<< std::endl;
}


// 根据find_first_of写个分割函数函数
auto my_spilit( const std::string& string, const std::string& delimiter)
{
    std::vector<std::string> spilit;
    const auto findNext = [&](const auto i) 
    {
        return std::find_first_of( i, string.end(), delimiter.begin(), delimiter.end() );
    };

    for (std::string::const_iterator i, i_prev = string.begin(); ; i_prev = i + 1)
    {
        i = findNext(i_prev);
        spilit.emplace_back(i_prev, i);
        if (i == string.end() )
        {
            break;
        }
    }

    return spilit;
}

void test2()
{
    std::string a = "one;two,three.four";
    std::string delimit = ";,.";
    
    write_to_cout(a);
    std::cout << std::endl;
    write_to_cout(delimit);
    std::cout << std::endl;

    // test algorithm
    auto i = std::find_first_of(a.begin(), a.end(), delimit.begin(),delimit.end());
    std::cout << "index of first delimiter is: " << i - a.begin() << std::endl;


    auto spilit = my_spilit(a, delimit); // 测试函数my_spilit
    write_to_cout(spilit, " | ");
    std::cout << std::endl;
}

void test3()
{
    std::vector<std::string> b = {"0", "1", "2", "3", "4", "5", "6", "7"};
    std::vector<std::string> bp = {"0", "1", "2", "7", "6", "5"};

    write_to_cout(b);
    std::cout << std::endl;
    write_to_cout(bp);
    std::cout << std::endl;

    // test algorithm
    auto i = std::mismatch(b.begin(), b.end(), bp.begin()).first;  // 返回第一个失配的字符串位置,注意返回值是一个pair
    std::cout << i - b.begin() << std::endl << std::endl;
}

void test4()
{
    std::string s = "hey! it is my first time to use this function.";
    std::string needle = "y!";

    write_to_cout(s, "");
    std::cout << std::endl;
    write_to_cout(needle, "");
    std::cout << std::endl;

    //test algorithm
    //寻找与模式串适配的文本串位置
    auto i = std::search( s.begin(), s.end(), needle.begin(), needle.end() );
    std::cout << i - s.begin() << std::endl << std::endl;
}

void test5()
{
    std::vector<int> a = {2, 42, 61, 15, 30, 23};
    
    write_to_cout(a);
    std::cout << std::endl;

    // test algorithm
    auto i = std::adjacent_find ( a.begin(), a.end(), [](const int a, const int b){return b == 2 * a;} );
    std::cout << i - a.begin() << std::endl << std::endl; 
}

int main()
{
    test0();
    test1();
    test2();
    test3();
    test4();
    test5();

    return 0;
}

C++ STD Gems05

标签:stream   sea   find   根据   func   char*   this   type   std   

原文地址:https://www.cnblogs.com/codemeta-2020/p/12119410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!