Vector & find_if in C ++

Source: Internet
Author: User
<STL Application> vector & find_if

Someone asked a struct named C as follows:

code:
  struct C{   int v1;   int v2;};
  

It should be used to declare a vector as a vector <C> CV;

What should I do when I want to search for internal elements ??
The general solution is to use the for () loop to search for rows. In fact, such a straight picture is also simple.

This provides another solution for ease of use ....
Find_if () provides the find_if () statement, but it is a pity that only one statement can be written.
In addition, the operator inputs the type of * iterator, rather than the equivalent value.

For example, V1 and V2 of the objects in this CV are sequential
V1 v2
1 100
2 52
3 25
4 75
5 84
6 33

So what should we do if we look for an object with V2 = 75?
1. functor point

code:
  bool Cfind75(const C& obj){        return obj.v2==75;}
  

2. functor

code:
  class Cfind75{       public:          bool operator()(const C& obj)          {               return obj.v2==75;          }};
  

However, it would be worse than the limit value to be killed !!
No thanks... so this time the template will be used ....
So we have to change

code:
  template<int n>class CComp{    public:        bool operator()(const C& lhs)        {            return (lhs.v2==n);        }};
  

How to use it ?? Very simple ....

code:
  vector<C>::iterator cviter =         find_if(cv.begin(),cv.end(),CComp<75>());
  

Otherwise, cviter is the position where the V2 value of the C-type object in the original CV is 75.
--
I have no questions about small applications. However, those unfamiliar with STL are often ignored.

  1. # Include <iostream>
  2. # Include <algorithm>
  3. # Include <vector>
  4. # Include <cstdlib>
  5. Using namespace STD;
  6. Struct C
  7. {
  8. C (): V1 (0), V2 (0 ){}
  9. C (const Int & val1, const Int & val2): V1 (val1), V2 (val2 ){}
  10. C Operator () (const Int & val1, const Int & val2)
  11. {
  12. V1 = val1;
  13. V2 = val2;
  14. Return * this;
  15. }
  16. ~ C (){}
  17. Int V1;
  18. Int V2;
  19. };
  20. Template <int n>
  21. Class ccomp {
  22. Public:
  23. Bool operator () (const C & LHS)
  24. {
  25. Return (LHS. v2 = N );
  26. }
  27. };
  28. Int main (INT argc, char * argv [])
  29. {
  30. Vector <C> CV;
  31. C val;
  32. Cv. push_back (Val (1,100 ));
  33. Cv. push_back (Val (2, 52 ));
  34. Cv. push_back (Val (3,25 ));
  35. Cv. push_back (Val (4, 75 ));
  36. Cv. push_back (Val (5, 84 ));
  37. Cv. push_back (Val (6, 33 ));
  38. Vector <C >:: iterator cviter =
  39. Find_if (cv. Begin (), cv. End (), ccomp <75> ());
  40. Cout <cviter-> V1 <"" <cviter-> V2 <Endl;
  41. Cout <Endl;
  42. System ("pause ");
  43. Return 0;
  44. }

Related Article

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.