C + + STL Source Analysis Learning Note (ii) iterator auto_ptr (old version of book example new version C + + has abolished this concept)

Source: Internet
Author: User

ITERATOR

Template<class Inputiterator,class t>

Inputiterator Find (inputiterator first,inputiterator last,const t& value)

{

while (first! = Last && *first! = value)

++first;

return first;

}

code example

1#include <iostream>2#include <vector>3#include <list>4#include <deque>5#include <algorithm>6#include <iostream>7 8 using namespacestd;9 Ten intMainintargcChar*argv[]) One { A     Const intArraySize =7; -     intIa[arraysize] = {0,1,2,3,4,5,6}; -  thevector<int> Ivect (ia,ia+arraySize); -list<int> IList (ia,ia+arraySize); -deque<int> Ideque (ia,ia+arraySize); -  +vector<int>::iterator it1 = Find (Ivect.begin (), Ivect.end (),4); -     if(It1 = =ivect.end ()) +cout <<"4 not found."<<Endl; A     Else atcout <<"4 found."<< * It1 <<Endl; -  -list<int>::iterator it2 = Find (Ilist.begin (), Ilist.end (),6); -     if(It2 = =ilist.end ()) -cout <<"6 not found."<<Endl; -     Else incout <<"6 found."<< *it2 <<Endl; -  todeque<int>::iterator it3 = Find (Ideque.begin (), Ideque.end (),8); +     if(It3 = =ideque.end ()) -cout <<"8 not found."<<Endl; the     Else *cout <<"8 Find"<< *it3 <<Endl; $ Panax Notoginseng  -     return 0; the}

The container in the STL is vector\set\list and so on.

The algorithm has find\count and so on

The two are independent, and the connection between them is to be connected by iterator.

Iterator similar smart pointers

Smart Pointer auto_ptr has a reference counting function in addition to the usual pointer concept

Automatically frees element memory resources by reference count of the elements pointed to by the pointer without having to call delete manually

The sample code is as follows

C + + STL Source Analysis Learning Note (ii) iterator auto_ptr (old version of book example new version C + + has abolished this concept)

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.