Use find_if () for map ()

Source: Internet
Author: User

According to the C ++ StandardProgramThe find () member function of map can be used to search for the first element with a key, but cannot search for the element with a specific value.

If you search for a specific value, you need to use find_if () or traverse it cyclically.

The following example shows how to use find_if ():

# Include <iostream> # include <map> # include <string> # include <algorithm> typedef STD: Map <STD: String, int> map_type; Template <class K, class V> class map_find_func {PRIVATE: V value; public: map_find_func (const V & V): Value (v) {} bool operator () (STD: pair <const K, v> & ELEM) {return (ELEM. second = value) ;}}; int main () {map_type col1; col1.insert (STD: make_pair ("1234", 1); col1.insert (STD :: make _ pair ("2234" , 2); map_type: iterator it = STD: find_if (col1.begin (), col1.end (), map_find_func <STD: String, int> (1 )); if (it! = Col1.end () {STD: cout <it-> first <STD: Endl ;}}

Output result:
1234
Press any key to continue

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.