Back_insert_iterator and iterator are different.

Source: Internet
Author: User

Look at the code first:

#include <iostream>#include<vector>#include<algorithm>#include<iterator>using namespacestd;intmain () {vector<int>Coll; //Create back_inserter for Coll//-Inconvenientback_insert_iterator<vector<int> >ITER (coll); //insert elements with the usual iterator interface*iter =1; ITER++; *iter =2; ITER++; *iter =3; Copy (Coll.begin (), Coll.end (), Ostream_iterator<int> (cout,", ")); cout<<Endl; //Create back Inserter and insert elements; //-ConvenientBack_inserter (coll) = -; Back_inserter (Coll)= -; Copy (Coll.begin (), Coll.end (), Ostream_iterator<int> (cout,", ")); cout<<Endl; //Use the back Inserter to append all elements again//Reserve enough memory to avoid reallocationColl.reserve (2*coll.size ()); Copy (Coll.begin (), Coll.end (),//SourceBack_inserter (coll));//DestinationCopy (Coll.begin (), Coll.end (),ostream_iterator<int> (cout,", ")); cout<<Endl;}

This line in the code above:

back_insert_iterator<vector<int> > iter (coll);

Defines an adapter. Personal feel also belongs to iterator, usage should be similar, so according to gourd painting Scoop, wrote the following code, define a iterator:

iterator<vector<int> > Iter1 (coll);

Result Error: Error C2976: "Std::iterator": Too few template parameters.

See iterator source code, the level is limited, only know is less parameter, concrete should how to remedy do not know. Later thought, the general definition of a iterater generally used the following form:

vector<int>= Coll.begin ();

Unlike Back_insert_iterator's definition, I think it should be back_insert_iterator is not part of a container, it should be a separate from the container. This may be the reason they are defined in different ways.

Back_insert_iterator and iterator are different.

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.