Use of STL find_end Algorithm

Source: Internet
Author: User

In my work, I plan to introduce the algorithm in STL into my work in order to save data operations and write too many similar sorting search codes, increase code streamlining. The usage of find_end is described below.

I. Function Description

Find_end:
Function prototype:
Template <class fdwit1, class fdwit2>
Fdwit1 find_end (fdwit1 first1, fdwit1 last, fdwit2 first2, fdwit2 last2 );

Template <class fdwit1, class fdwit2, class Pred>
Fdwit1 find_end (fdwit1 first1, fdwit1 last, fdwit2 first2, fdwit2 last2, PRED Pr );

Function: In the sequence marked by [first1, last1), find the "marked by iteratior to [first2, last2)
The second sequence. For example, Mississippi and second
Then, find_end () returns the first s of a fwdit1 pointing to the second SS sequence. If
If the second sequence is not found in the first sequence, last1. In the first version
Equal operators of layers. In the second version, the binary operator PR passed by the user is used.

I copied it from another reference. This application is the second algorithm that comes with comparative functions. For example, a complex data structure may be searched from different perspectives, such as the structure of a student, you can search by student ID or name. The comparison algorithms written in other languages are more adaptive.

Ii. source code in STL

Template <class _ fwdit1, class _ fwdit2,
Class _ Pr> inline
_ Fwdit1 find_end (_ fwdit1 _ first1, _ fwdit1 _ last1, _ fwdit2 _ first2, _ fwdit2 _ last2, _ Pr _ Pred)
{// Find last [_ first2, _ last2) Satisfying _ Pred
_ Assign_from_base (_ first1,
_ Find_end (_ checked_base (_ first1), _ checked_base (_ last1 ),
_ Checked_base (_ first2), _ checked_base (_ last2), _ Pred,
_ Dist_type (_ first1), _ dist_type (_ first2 )));
Return _ first1;
}

// Call the following function for search

Template <class _ fwdit1,
Class _ fwdit2, class _ diff1, class _ diff2, class _ Pr> inline
_ Fwdit1 _ find_end (_ fwdit1 _ first1, _ fwdit1 _ last1,
_ Fwdit2 _ first2, _ fwdit2 _ last2, _ Pr _ Pred, _ diff1 *, _ diff2 *)
{// Find last [_ first2, _ last2) Satisfying _ Pred
_ Debug_range (_ first1, _ last1 );
_ Debug_range (_ first2, _ last2 );
_ Debug_pointer (_ Pred );
_ Diff1 _ count1 = 0;
_ Distance (_ first1, _ last1, _ count1 );
_ Diff2 _ count2 = 0;
_ Distance (_ first2, _ last2, _ count2 );
_ Fwdit1 _ ans = _ last1;

If (0 <_ count2)
For (; _ count2 <= _ count1; ++ _ first1, -- _ count1)
{// Room for match, try it
_ Fwdit1 _ mid1 = _ first1;
For (_ fwdit2 _ mid2 = _ first2; ++ _ mid1)
If (! _ PRED (* _ mid1, * _ mid2) // call our comparison function.
Break;
Else if (++ _ mid2 ==_ last2)
{// Potential answer, save it
_ Ans = _ first1;
Break;
}
}
Return (_ ans );
}

// We can see that STL also compares variables one by one. The advantage is that regardless of the variable type, and the comparison algorithm. This is the advantage of generics.

Iii. Sample program

# Include "stdafx. H"
# Include "tchar. H"
# Include <vector>
# Include <algorithm>

Struct student
{
Int num;
Tchar name [32];
Public:
Student ()
{
Num = 0;
Memset (name, 0, sizeof (tchar) * 32 );
}
Static void empty (student * s)
{
S-> num = 0;
Memset (S-> name, 0, sizeof (tchar) * 32 );
}
};
Typedef STD: vector <student> VST;
Typedef STD: vector <student>: iterator vstitor;
Bool stu_findnum (const student & T1, const student & T2)
{
Return t1.num = t2.num;
}
Bool stu_findname (const student & T1, const student & T2)
{
Return _ strcmpi (t1.name, t2.name) = 0;
}

Void exam ()
{
VST, st2;
Student Stu;
Int I;
Vstitor;
For (I = 0; I <10; I ++)
{
Student: Empty (& Stu );
Stu. num= 100 + I;
_ Stprintf (STU. Name, "Student % d", I );
St. push_back (Stu );
}

Student: Empty (& Stu );
Stu. num = 104;
St2.push _ back (Stu );
// Search for students whose student ID is 104
Itor = STD: find_end (St. Begin (), st. End (), st2.begin (), st2.end (), stu_findnum );
If (itor! = ST. End ())
{
Printf ("Student: Name = % s num = % d/N", (* itor). Name,
(* Itor). Num );
}
St2.clear ();
Student: Empty (& Stu );
_ Tcscpy (STU. Name, "Student 1 ");
St2.push _ back (Stu );
// Find STUDENT 1 student
Itor = STD: find_end (St. Begin (), st. End (), st2.begin (), st2.end (), stu_findname );
If (itor! = ST. End ())
{
Printf ("Student: Name = % s num = % d/N", (* itor). Name,
(* Itor). Num );
}
}

Int main (INT argc, char * argv [])
{
Exam ();
Return 0;
}

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.