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. Zero 初识Sciter

    在浏览有关Sciter技术前,您需要花点时间浏览以下内容. 您是否需要花时间学习Sciter? 如果您的工作或您想从事的工作与桌面应用开发无关,那么您不需要学习Sciter. 如果您不认同HTML\C ...

  2. css常用设置

    距离左边和上边 style="margin-left:100px;margin-top:10px" 设置相对位置 position:absolute; position:relat ...

  3. MySQL 通过SQL语句导出表为文件

    SELECT * //你要导出的字段 FROM `tabel` //表名 INTO OUTFILE "D:\\file.txt" //导出的文件路径和文件名 LINES TERMI ...

  4. RocketMQ 单机部署(单master模式)

    一.为了快速了解rockmq,先搭建一个简单的单机版的rocketmq,前期准备:1.CentOS 7.6 64位(阿里云)(4G内存) 2.jdk1.8 3.maven 3.5.4 4.直接从官网上 ...

  5. 当3D打影人头”成为黑客的秘密武器,隐私该如何保护?

    在<碟中谍>系列电影中,除了超级敬业又帅气的阿汤哥之外,最让人津津乐道的桥段就是用3D打印做出来的"人头".通过这些惟妙惟肖的"人头",阿汤哥完成了 ...

  6. 困惑我的x++和++x;

    刚学习C语言时X++和++X非常不解 目前有了新的领悟 1.X++ int x=0; int z=x++; 此时z?x? 这个问题可以分两步思考 第一步:先把x的值赋予z,此时z=x=0; 第二步:x ...

  7. Spark Shuffle 过程

    本文参考:http://www.cnblogs.com/cenyuhai/p/3826227.html 在数据流动的整个过程中,最复杂最影响性能的环节,就是 Shuffle 过程,本文将参考大神的博客 ...

  8. JS: 图片轮播模板——左右移动,点击编码移动,自动轮播

    <!DOCTYPE html><html> <head>  <meta charset="UTF-8">  <title> ...

  9. Golang的选择结构-if语句

    Golang的选择结构-if语句 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.选择语句应用场景概述 选择结构也称为条件判断,生活中关于判断的场景也非常的多,比如: ()登录Q ...

  10. 对状态字的理解 尤其是 首次检测位“/FC”的想法

    状态字 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0               BR CC1 CC0 OV OS OR STA RLO /FC 问题1 关于首次检测位& ...