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的更多相关文章

  1. 【NX二次开发】NX内部函数,libuifw.dll文件中的内部函数

    本文分为两部分:"带参数的函数"和 "带修饰的函数". 浏览这篇博客前请先阅读: [NX二次开发]NX内部函数,查找内部函数的方法 带参数的函数: void U ...

  2. C++ std::set

    std::set template < class T, // set::key_type/value_type class Compare = less<T>, // set::k ...

  3. C++ std::priority_queue

    std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...

  4. C++ std::queue

    std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...

  5. C++ std::multimap

    std::multimap template < class Key, // multimap::key_type class T, // multimap::mapped_type class ...

  6. C++ std::map

    std::map template < class Key, // map::key_type class T, // map::mapped_type class Compare = less ...

  7. C++ std::list

    std::list template < class T, class Alloc = allocator > class list; List Lists are sequence co ...

  8. C++ std::forward_list

    std::forward_list template < class T, class Alloc = allocator > class forward_list; Forward li ...

  9. C++ std::deque

    std::deque template < class T, class Alloc = allocator > class deque; Double ended queue deque ...

随机推荐

  1. PhpStorm 10.0.1破解激活步骤

    1.下载破解补丁 链接:https://pan.baidu.com/s/1S4JVKiaFnCZG01MpDPaAAA 密码:c6x6 放到phpstorm安装目录下的lib文件夹下: C:\soft ...

  2. JAVA 集合 List 分组的两种方法

    CSDN日报20170219--<程序员的沟通之痛> [技术直播]揭开人工智能神秘的面纱 程序员1月书讯 云端应用征文大赛,秀绝招,赢无人机! JAVA 集合 List 分组的两种方法 2 ...

  3. css 文本溢出省略号

    单行溢出显示省略号: white-space: nowrap; overflow: hidden; text-overflow: ellipsis; 多行溢出显示省略号: text-overflow: ...

  4. 用sql删除数据库重复的数据的方法

      /***********************************************两个意义上的重复记录:1.是完全重复的记录,也即所有字段均重复的记录,2.是部分关键字段重复的记录, ...

  5. Oracle性能优化小结

    Oracle性能优化小结 原则一.注意where子句中的连接顺序 Oracle采用自下而上的顺序解析where子句,根据这个原理,表之间的连接必须卸载其他where条件之前,哪些可以滤掉最大数量记录的 ...

  6. 第一单元总结:基于基础语言、继承和接口的简单OOP

    前情提要 到目前为止,OO课程已经完成了前三次的作业,分别为: 第一次作业:简单多项式的构造和求导.[正则表达式][数据结构][排序] 第二次作业:含三角函数因子的复杂多项式的构造.求导和化简.[递归 ...

  7. 在ListView头和尾添加东西

    直接上代码 import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view ...

  8. P1065 单身狗

    P1065 单身狗 转跳点:

  9. (转)如何判断VPS是基于哪种虚拟技术?Xen、OpenVZ、Xen HVM、KVM还是VMware

    对于VPS新手来说,怕被无良的奸商给忽悠,下的Xen的却给的OpenVZ的,如何来判断自己买的VPS是那种虚拟技术的,下面VPS侦探整理一些常见的方法. 1.通过系统上的相关目录或文件判断 执行:ls ...

  10. JAVA实现数组的反转--基础

    直接上代码 这个算法比较简单,唯一需要注意的就是第8行和第9行.一定要多减去1 因为for循环从0开始,而数组长度是从0到length-1的. class ArrReverse { //实现数组元素的 ...