程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 實戰c++中的vector系列--vector應用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)

實戰c++中的vector系列--vector應用之STL的find、find_if、find_end、find_first_of、find_if_not(C++11)

編輯:C++入門知識

實戰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      // std::cout
#include     // std::find
#include        // 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 myvector (myints,myints+4);
  std::vector::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
30

find_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      // std::cout
#include     // std::find_end
#include        // 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 haystack (myints,myints+10);

  int needle1[] = {1,2,3};

  // using default comparison:
  std::vector::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
3

find_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      // std::cout
#include     // std::find_if
#include        // std::vector

bool IsOdd (int i) {
  return ((i%2)==1);
}

int main () {
  std::vector myvector;

  myvector.push_back(10);
  myvector.push_back(25);
  myvector.push_back(40);
  myvector.push_back(55);

  std::vector::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      // std::cout
#include     // std::find_first_of
#include        // std::vector
#include        // 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 haystack (mychars,mychars+6);
  std::vector::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
a

find_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      // std::cout
#include     // std::find_if_not
#include         // std::array

int main () {
  std::array foo = {1,2,3,4,5};

  std::array::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 
#include 
#include  
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::value_type &value)
{
        return value.a == m_i_a;
}
private:
       int m_i_a;
};


int main()
{
            std::vector 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::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的首字母按照字母表進行排序:

#include      // std::cout
#include     // std::stable_sort
#include        // std::vector
#include 

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 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



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved