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. Linux之文件传输

    本文借鉴<Linux命令大全> 1. bye命令 功能:终端FTP连线并结束程序 语法:bye 补充:在ftp模式下,输入bye即可中断目前的连线作业,并结束ftp的执行. 2. ftp命 ...

  2. C++ 类 与 static

    背景 从学习C++到使用现在,发现很多新的东西,正好整理一下. static 为静态,指是当类编译加载的时候,内存就会开辟存储空间的. static 数据成员 在类中,static 可修饰 类中的成员 ...

  3. 对S7通信的连接的理解以及对比CAN通信协议来理解PLC通讯

    对S7通信的连接的理解以及对比CAN通信协议来理解PLC通讯. 对功能块 SFB12 和 SFB13 的R_ID参数的理解 ? 对于同一个数据包.发送方与接收方的R_ID应该相同. 用下图解释 双向连 ...

  4. 新手小白如何向GitHub上提交项目

    首先你得注册一个自己的GitHub账号,注册网址:https://github.com/join 创建一个新的项目,填写项目名称,描述 创建完成之后,跳转到下面的页面,下面红框中的网址要记住,在后面上 ...

  5. 数学软件实训2-MATLAB程序综合设计及应用

    数学软件实训任务二 一 题目:MATLAB程序综合设计及应用 二 目的:熟练掌握MATLAB程序设计的基本方法,会根据MATLAB程序设计的 三 要求: 1 熟练掌握控制流的基本语法结构. 2 会熟练 ...

  6. 需要多个参数输入时-----------------考虑使用变种的Builder模式

    业务需求: 创建一个不可变的Person对象,这个Person可以拥有以下几个属性:名字.性别.年龄.职业.车.鞋子.衣服.钱.房子. 要求: 其中名字和性别是必填项,而其他选填项可以根据情况自由输入 ...

  7. solus linux 中文输入法

    默认用ibus输入框架,安装ibus-libpinyin sudo eopkg install ibus-libpinyin ibus 安装好后重启 在系统设置 -区域和语言中添加中文,(记得自己设置 ...

  8. Redis详解(三)——事务

    Redis详解(三)--事务 Redis事务的概念: Redis 事务的本质是一组命令的集合.事务支持一次执行多个命令,一个事务中所有命令都会被序列化.在事务执行过程,会按照顺序串行化执行队列中的命令 ...

  9. Spring AOP 管理事务

    <aop:config proxy-target-class="true"> <aop:pointcut expression="execution(* ...

  10. Problem C Updating a Dictionary

    Problem C     Updating a Dictionary In this problem, a dictionary is collection of key-value pairs, ...