Vector series--vector in actual combat C + + application STL Find, Find_if, Find_end, find_first_of, Find_if_not (c++11) __c++

Source: Internet
Author: User

Use vector containers, that is, to avoid the search, so today we list some STL find algorithm applied to the vector.

Find ()
Returns an iterator to the "the" 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 &L t;vector>//Std::vector int main () {//using Std::find with array and pointer:int myints[] = {10, 20, 3
  0, 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; }//

find_end ()
searches the range [first1,last1) for the "Last occurrence of" sequence defined by [ FIRST2,LAST2), and returns a iterator to its, or last1 if no occurrences are.

 #include <iostream>//std::cout #include <algorithm>//Std::find_end #includ

e <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; //Output: 5 3 

find_if ()
Returns an iterator to the "the" in the range [First,last] for which pred 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 (ten);
  Myvector.push_back (a);
  Myvector.push_back ();
  Myvector.push_back ();

  Std::vector<int>::iterator it = std::find_if (Myvector.begin (), Myvector.end (), isodd);
  Std::cout << "The odd value is" << *it << ' \ n ';

  return 0;
}
Output:
25

By the way, you can use a lambda expression instead of the isodd function to make it more concise.

find_first_of ()
Returns an iterator to the "the" 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 #i  Nclude <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" the ":" << *it << ' \ n '; Using predicate comparison:it = Find_first_of (Haystack.begin (), Haystack.end (), Needle, Needl

  E+3, comp_case_insensitive);

  if (It!=haystack.end ()) std::cout << "The" the ":" << *it << ' \ n ';
return 0; Output: A A 

Find_if_not ()
Finally factory this we should pay attention to some is c++11 only method. Personally feel a lot of use, look at the official description:
Returns an iterator to the "the" in the range [First,last) for which pred Returns false. If no such element is found, the function returns last.
Example:

#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 even value is" << *it << ' \ n ';

  return 0;
}
Output:
2

Finally, one more program:

#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; }

The first letter of a string in the,vector<string> used in combat is sorted alphabetically:

#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 < 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; Output: Ae ao at Bo bt BV 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 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.