C++ STD Gems05
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的更多相关文章
- 【NX二次开发】NX内部函数,libuifw.dll文件中的内部函数
本文分为两部分:"带参数的函数"和 "带修饰的函数". 浏览这篇博客前请先阅读: [NX二次开发]NX内部函数,查找内部函数的方法 带参数的函数: void U ...
- C++ std::set
std::set template < class T, // set::key_type/value_type class Compare = less<T>, // set::k ...
- C++ std::priority_queue
std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...
- C++ std::queue
std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...
- C++ std::multimap
std::multimap template < class Key, // multimap::key_type class T, // multimap::mapped_type class ...
- C++ std::map
std::map template < class Key, // map::key_type class T, // map::mapped_type class Compare = less ...
- C++ std::list
std::list template < class T, class Alloc = allocator > class list; List Lists are sequence co ...
- C++ std::forward_list
std::forward_list template < class T, class Alloc = allocator > class forward_list; Forward li ...
- C++ std::deque
std::deque template < class T, class Alloc = allocator > class deque; Double ended queue deque ...
随机推荐
- 树莓派4b烧录系统
树莓派4b烧录系统 树莓派 型号:树莓派4b 系统:raspbian-buster-full 1.先用SDFormatterv4格式化sd卡 2.用win32diskimager-v0.9-binar ...
- C# WinForm 自定义控件,DataGridView背景透明,TabControl背景透明
注意: 以下代码,属性直接赋值的语法糖要vs2015以上才支持. using System.ComponentModel; using System.Drawing; using System. ...
- mysql分区介绍
http://www.cnblogs.com/chenmh/p/5644713.html 介绍 可以针对分区表的每个分区指定各自的存储路径,对于innodb存储引擎的表只能指定数据路径,因为数据和索引 ...
- 吴裕雄--天生自然JAVAIO操作学习笔记:字节流与字符流操作
import java.io.* ; public class Copy{ public static void main(String args[]){ if(args.length!=2){ // ...
- PostgreSQL数据库-抽奖sql
select * from users order by random();--会进行随机排序,每次运行都会返回不同的结果 select * from users order by random() ...
- firewalld学习-zone
原文地址:http://www.excelib.com/article/290/show firewalld默认提供了九个zone配置文件: block.xml.dmz.xml.drop.xml.ex ...
- 定时执行 Job【转】
Linux 中有 cron 程序定时执行任务,Kubernetes 的 CronJob 提供了类似的功能,可以定时执行 Job.CronJob 配置文件示例如下: ① batch/v2alpha1 是 ...
- Kubernetes 集群日志管理【转】
Kubernetes 开发了一个 Elasticsearch 附加组件来实现集群的日志管理.这是一个 Elasticsearch.Fluentd 和 Kibana 的组合.Elasticsearch ...
- Java笔记: 初始化块
Java语言提供了很多类初始化的方法,包括构造器.初始化器等.除了这两种方法之外,我们还可以用初始化块(initialization block)来实现初始化功能. 基本语法 初始化块是出现在类声明中 ...
- pyhton中matplotlib箱线图的绘制(matplotlib双轴图、箱线图、散点图以及相关系数矩阵图))
//2019.07.23 1.箱形图,又称为盒式图,一般可以很好地反映出数据分布的特征,也可以进行多项数据之间分布特征的比较,它主要包含五个基础数据:中位数,两个上下分位数以及上下边缘线数据 其中的一 ...