实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)
使用vector容器,即避免不了进行查找,所以今天就罗列一些stl的find算法应用于vector中。
find() 
Returns an iterator to the first element in the range [first,last) that compares equal to val. If no such element is found, the function returns last.
#include <iostream>     // std::cout
#include <algorithm>    // std::find
#include <vector>       // std::vector
int main () {
  // using std::find with array and pointer:
  int myints[] = { 10, 20, 30, 40 };
  int * p;
  p = std::find (myints, myints+4, 30);
  if (p != myints+4)
    std::cout << "Element found in myints: " << *p << '\n';
  else
    std::cout << "Element not found in myints\n";
  // using std::find with vector and iterator:
  std::vector<int> myvector (myints,myints+4);
  std::vector<int>::iterator it;
  it = find (myvector.begin(), myvector.end(), 30);
  if (it != myvector.end())
    std::cout << "Element found in myvector: " << *it << '\n';
  else
    std::cout << "Element not found in myvector\n";
  return 0;
}
//
30
30find_end() 
Searches the range [first1,last1) for the last occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found.
#include <iostream>     // std::cout
#include <algorithm>    // std::find_end
#include <vector>       // std::vector
bool myfunction (int i, int j) {
  return (i==j);
}
int main () {
  int myints[] = {1,2,3,4,5,1,2,3,4,5};
  std::vector<int> haystack (myints,myints+10);
  int needle1[] = {1,2,3};
  // using default comparison:
  std::vector<int>::iterator it;
  it = std::find_end (haystack.begin(), haystack.end(), needle1, needle1+3);
  if (it!=haystack.end())
    std::cout << "needle1 last found at position " << (it-haystack.begin()) << '\n';
  int needle2[] = {4,5,1};
  // using predicate comparison:
  it = std::find_end (haystack.begin(), haystack.end(), needle2, needle2+3, myfunction);
  if (it!=haystack.end())
    std::cout << "needle2 last found at position " << (it-haystack.begin()) << '\n';
  return 0;
}
//输出:
5
3find_if() 
Returns an iterator to the first element in the range [first,last) for which pred returns true. If no such element is found, the function returns last.
#include <iostream>     // std::cout
#include <algorithm>    // std::find_if
#include <vector>       // std::vector
bool IsOdd (int i) {
  return ((i%2)==1);
}
int main () {
  std::vector<int> myvector;
  myvector.push_back(10);
  myvector.push_back(25);
  myvector.push_back(40);
  myvector.push_back(55);
  std::vector<int>::iterator it = std::find_if (myvector.begin(), myvector.end(), IsOdd);
  std::cout << "The first odd value is " << *it << '\n';
  return 0;
}
//输出:
25顺便说一句能够使用lambda表达式取代IsOdd函数。使得更加简洁。
find_first_of() 
Returns an iterator to the first element in the range [first1,last1) that matches any of the elements in [first2,last2). If no such element is found, the function returns last1.
#include <iostream>     // std::cout
#include <algorithm>    // std::find_first_of
#include <vector>       // std::vector
#include <cctype>       // std::tolower
bool comp_case_insensitive (char c1, char c2) {
  return (std::tolower(c1)==std::tolower(c2));
}
int main () {
  int mychars[] = {'a','b','c','A','B','C'};
  std::vector<char> haystack (mychars,mychars+6);
  std::vector<char>::iterator it;
  int needle[] = {'A','B','C'};
  // using default comparison:
  it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3);
  if (it!=haystack.end())
    std::cout << "The first match is: " << *it << '\n';
  // using predicate comparison:
  it = find_first_of (haystack.begin(), haystack.end(),
                      needle, needle+3, comp_case_insensitive);
  if (it!=haystack.end())
    std::cout << "The first match is: " << *it << '\n';
  return 0;
}
输出:
A
afind_if_not() 
最后出厂这个 我们应该重视一些 是C++11才有的方法。个人认为用处非常多。看看官方的描写叙述: 
Returns an iterator to the first element in the range [first,last) for which pred returns false. If no such element is found, the function returns last. 
样例:
#include <iostream>     // std::cout
#include <algorithm>    // std::find_if_not
#include <array>        // std::array
int main () {
  std::array<int,5> foo = {1,2,3,4,5};
  std::array<int,5>::iterator it =
    std::find_if_not (foo.begin(), foo.end(), [](int i){return i%2;} );
  std::cout << "The first even value is " << *it << '\n';
  return 0;
}
//输出:
2最后 再来一个程序:
#include <vector>
#include <string>
#include <algorithm>
struct value_t
{
  int a;
   int b;
};
class vector_finder
{
public:
vector_finder(const int a) :m_i_a(a) {}
bool operator ()(const std::vector<struct value_t>::value_type &value)
{
        return value.a == m_i_a;
}
private:
       int m_i_a;
};
int main()
{
            std::vector<struct value_t> my_vector;
            struct value_t my_value;
                my_value.a = 11; my_value.b = 1000;
            my_vector.push_back(my_value);
                my_value.a = 12; my_value.b = 1000;
            my_vector.push_back(my_value);
                my_value.a = 13; my_value.b = 1000;
            my_vector.push_back(my_value);
                my_value.a = 14; my_value.b = 1000;
            my_vector.push_back(my_value);
                std::vector<struct value_t>::iterator it = my_vector.end();
            it = std::find_if(my_vector.begin(), my_vector.end(), vector_finder(13));
            if (it == my_vector.end())
                   printf("not found\n");
            else
                   printf("found value.a:%d value.b:%d\n", it->a, it->b);
            return 0;
        }最后来一个实战中用到的。vector<string>中的string的首字母依照字母表进行排序:
#include <iostream>     // std::cout
#include <algorithm>    // std::stable_sort
#include <vector>       // std::vector
#include <string>
static char ch = 'a';
bool myfunction(const std::string& lhs, const std::string& rhs)
{
    return lhs < rhs;
}
bool myfunction2(const std::string& lhs)
{
    return lhs[0] == ch;
}
int main() {
    std::vector<std::string> myvector;
    myvector.push_back("wo");
    myvector.push_back("wi");
    myvector.push_back("wa");
    myvector.push_back("ao");
    myvector.push_back("bo");
    myvector.push_back("ae");
    myvector.push_back("bv");
    myvector.push_back("cd");
    myvector.push_back("ef");
    myvector.push_back("gd");
    myvector.push_back("ww");
    myvector.push_back("cd");
    myvector.push_back("at");
    myvector.push_back("bt");
    myvector.push_back("ct");
    myvector.push_back("dt");
    myvector.push_back("et");
    myvector.push_back("ft");
    myvector.push_back("gt");
    myvector.push_back("ht");
    myvector.push_back("it");
    myvector.push_back("jt");
    myvector.push_back("kt");
    myvector.push_back("lt");
    myvector.push_back("mt");
    myvector.push_back("nt");
    myvector.push_back("ot");
    myvector.push_back("pt");
    myvector.push_back("qt");
    myvector.push_back("rt");
    myvector.push_back("st");
    myvector.push_back("tt");
    myvector.push_back("ut");
    myvector.push_back("vt");
    myvector.push_back("wt");
    myvector.push_back("xt");
    //myvector.push_back("yt");
    myvector.push_back("zt");
    myvector.push_back("qt");
    myvector.push_back("et");
    myvector.push_back("ee");
    std::stable_sort(myvector.begin(), myvector.end(), myfunction);
    for (std::string &s : myvector)
        std::cout << s << " ";
    std::cout << std::endl;
    std::cout << "===============" << std::endl;
    for (int i = 1;i < 27; i++)
    {
        auto  it_begin = std::find_if(myvector.begin(), myvector.end(), myfunction2);
        auto  it_end = std::find_if_not(it_begin, myvector.end(), myfunction2);
        for (auto i = it_begin; i != it_end; i++)
        {
            std::cout << *i << " ";
        }
        std::cout << std::endl;
        ch++;
    }
    return 0;
}
//输出:
ae ao at bo bt bv cd cd ct dt ee ef et et ft gd gt ht it jt kt lt mt nt ot pt qt qt rt st tt ut vt wa wi wo wt ww xt zt
===============
ae ao at
bo bt bv
cd cd ct
dt
ee ef et et
ft
gd gt
ht
it
jt
kt
lt
mt
nt
ot
pt
qt qt
rt
st
tt
ut
vt
wa wi wo wt ww
xt
zt
实战c++中的vector系列--vector应用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)的更多相关文章
- 实战c++中的string系列--std:vector 和std:string相互转换(vector to stringstream)
		string.vector 互转 string 转 vector vector vcBuf;string stBuf("Hello DaMao!!!");----- ... 
- 实战c++中的vector系列--vector的一些异常
		今天就写一写vector的一些异常.能够捕捉的异常. out_of_range 相当于数组的越界了.vector会自己主动增大容量,可是假设索引超出了当前的size.就会引发异常. #include& ... 
- 实战c++中的vector系列--vector<unique_ptr<>>初始化(全部权转移)
		C++11为我们提供了智能指针,给我们带来了非常多便利的地方. 那么假设把unique_ptr作为vector容器的元素呢? 形式如出一辙:vector<unique_ptr<int> ... 
- 实战c++中的vector系列--vector的遍历(stl算法、vector迭代器(不要在循环中推断不等于end())、operator[])
		遍历一个vector容器有非常多种方法.使用起来也是仁者见仁. 通过索引遍历: for (i = 0; i<v.size(); i++) { cout << v[i] << ... 
- 实战c++中的string系列--string与char*、const char *的转换(data() or c_str())
		在project中,我们也有非常多时候用到string与char*之间的转换,这里有个一我们之前提到的函数 c_str(),看看这个原型: const char *c_str(); c_str()函数 ... 
- 实战c++中的string系列--不要使用memset初始化string(一定别这么干)
		參考链接: http://www.cppblog.com/qinqing1984/archive/2009/08/07/92479.html 百度百科第一次这么给力: void *memset(voi ... 
- 实战c++中的string系列--std::string与MFC中CString的转换
		搞过MFC的人都知道cstring,给我们提供了非常多便利的方法. CString 是一种非常实用的数据类型. 它们非常大程度上简化了MFC中的很多操作,使得MFC在做字符串操作的时候方便了非常多.无 ... 
- 实战c++中的string系列--十六进制的字符串转为十六进制的整型(一般是颜色代码使用)
		非常久没有写关于string的博客了.由于写的差点儿相同了.可是近期又与string打交道,于是荷尔蒙上脑,小蝌蚪躁动. 在程序中,假设用到了颜色代码,一般都是十六进制的,即hex. 可是server ... 
- 实战c++中的string系列--string的替换、查找(一些与路径相关的操作)
		今天继续写一些string操作. string给我们提供了非常多的方法,可是每在使用的时候,就要费些周折. 场景1: 得到一个std::string full_path = "D:\prog ... 
随机推荐
- php处理类
			Thomas Boutell 以及众多的开发者创造了以GD图形库闻名的一个图形软件库,用于动态的图形计算. GD提供了对于诸如C, Perl, Python, PHP, OCaml等等诸多编程语言的支 ... 
- hdu3652B-number(数位dp)
			B-number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ... 
- BZOJ 2084 二分+hash OR Manacher
			思路: 二分+哈希 //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> ... 
- SQL学习--Select(一)TOP、派生表、连接、谓词
			TOP关键字 WITH TIES t.title,sum(s.qty)as totalSales from sales s left join titles t on s.title_id=t.tit ... 
- Hadoop学习笔记(一)Hadoop的单节点安装
			要想深入学习Hadoop分布式文件系统,首先需要搭建Hadoop的实验环境,Hadoop有两种安装模式,即单节点集群模式安装(也称为伪分布式)和完全分布式模式安装,本节只介绍单节点模式的安装,参考官方 ... 
- Hibernate_01_初体验
			hibernate开发的基本步骤: 编写配置文档hibernate.cfg.xml: 编写实体类: 生成对应实体类的映射文件并添加到配置文档中: 调用hibernate API进行测试. Hibern ... 
- 图方法:寻找无向图联通子集的JAVA版本
			图像处理中一般使用稠密方法,即对图像进行像素集合进行处理.在图像拓扑方面,更多地应用图计算方法. 寻找无向图联通子集的JAVA版本,代码: //查找无向图的所有连通子集//wishchin!!! pu ... 
- 推荐系统:MovivLens20M数据集解析
			MovieLens 是历史最悠久的推荐系统.它由美国 Minnesota 大学计算机科学与工程学院的 GroupLens 项目组创办,是一个非商业性质的.以研究为目的的实验性站点.MovieLens ... 
- mqtt-client回调方法简介
			mqtt-client回调方法简介 毫无疑问Callback方式是最复杂的一种,但是其也是能够提供更好的服务,因此有必要好好研究,下面就是对使用回调方式的简单介绍: 一.在使用回调方式前,先通过MQT ... 
- POJ_2411_Mondriaan's Dream_状态压缩dp
			Mondriaan's Dream Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 15407 Accepted: 888 ... 
