The use of the Find function in C + +

Source: Internet
Author: User

The Find function primarily implements finding the specified element within the container, and the element must be of the basic data type.
The lookup succeeded in returning an iterator to the specified element, and the lookup failed to return an end iterator.

Header file

#include <algorithm>

function implementation

template<classclass t>const t& val) {  while ( First!= last)   {     ifreturn first ;      + + first;   }     return Last ;}

Example 1 (vector)

#include <iostream>#include <algorithm>#include <vector>UsingNamespaceStdIntMain () {vector<String>M M.push_back ("Hello"); M.push_back ("hello2); M.push_back ( "hello3" ); if (Find (M.begin (), M.end (), hello M.end ()) cout << no" < < Endl; else cout << yes "<< Endl;}       

Example 2 (SET)

#include <iostream>#include <algorithm>#include <String>#include <Set>UsingNamespaceStdIntMain () {set<String>M M.insert ("Hello"); M.insert ("hello2); M.insert ( "hello3" Span style= "color: #000000;" >); if (Find (M.begin (), M.end (), hello M.end ()) cout << no" < < Endl; else cout << yes "<< Endl;}       

Note 1:set itself has a find function, for example:

#include <iostream>#include <algorithm>#include <String>#include <Set>UsingNamespaceStdIntMain () {set<String>M M.insert ("Hello"); M.insert ("hello2); M.insert ( "hello3" Span style= "color: #000000;" >); if (Find (M.begin (), M.end (), hello M.end ()) cout << no" < < Endl; else cout << yes "<< Endl;}       

Note 2:string itself has a find function, for example:

#include <iostream>#include <algorithm>#include <String>UsingNamespaceStdIntMain () {string s ="Helllo";if (S.find ("E") ==String:: NPOs)Yes cout <<"No"<<EndlElsecout << "Yes" << Endl; if (S.find ("z") = = string:: NPOs) //no cout << "no" <<  Endl else cout << "Yes" << Endl;}        

The use of the Find function in C + +

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.