the use of __c++ in c++/c++11

Source: Internet
Author: User

<iterator> is a header file in the C + + standard library that defines some of the iterator template classes in the C + + STL standard, which are derived from Std::iterator as base classes. An iterator provides the ability to manipulate a collection (container) element. The basic operations provided by iterators are access and traversal. The iterator simulates the pointer in C + +, can have the + + operation, and uses the * (dereference operator, deference) or-> operator to access the elements in the container. If the element in the container changes the memory used, it does not affect the bound iterator pointing to the correct location. Therefore, the iterator is actually more like a handle (handler). Iterators allow C + + programs to handle different data structures in a uniform manner. An iterator acts as an intermediary between a container and a common algorithm. Instead of manipulating a particular data type, the algorithm is defined to run within the scope specified by an iterator type. Any data structure that satisfies the requirements of an iterator can be manipulated by an algorithm.

STL iterator implements the "iterator pattern" in design pattern, that is, sequentially accesses an element in an aggregation without exposing the implementation details of the aggregation.

Iterators support traversing aggregation types in different ways. For example, for a tree data type, you can have an iterator that has a sequence, a middle order, and a sequential traversal. On an object of the same aggregation type, you can have multiple iterators at the same time, each maintaining a different traversal state. The iterative devices implemented on different aggregation types have standard external interfaces, which provides the possibility for the algorithms in STL to use iterators.

A total of five iterators are supported: Input iterators (*iter can only be used as right values after the dereference), Output iterators (*iter can only be used as left values after the dereference), forward iterators (with input iterators, output iterators all the functionality, and can iterate over the operation, support for multiple reads and writes of the same element), Bi-directional iterators, which are based on forward iterators, have the ability of stepping backward traversal, and random access iterators, based on bidirectional iterators, have the ability to access each data element directly. The random iterator adds an "iterator arithmetic operation").

Std::input_iterator_tag: An empty class that identifies the category of an iterator as an input iterator.

Std::output_iterator_tag: An empty class that identifies a class of iterators as an output iterator.

Std::forward_iterator_tag: An empty class that identifies a category of iterators as a forward iterator.

Std::bidirectional_iterator_tag: An empty class that identifies a class of iterators as a bidirectional iterator.

Std::random_access_iterator_tag: An empty class that identifies a class of iterators as a random iterator.

The following is a copy of <iterator> test code from other articles, and the details can refer to the corresponding reference:

#include "iterator.hpp" #include <iterator> #include <iostream> #include <typeinfo> #include <list > #include <vector> #include <algorithm> #include <deque> #include <string>//Reference:http
://www.cplusplus.com/reference/iterator/namespace iterator_ {/////////////////////////////////////////////////// Class Myiterator:public Std::iterator<std::input_iterator_tag, int> {public:myiterator (int* x):p (x) {} MyIter
	Ator (const myiterator& mit): P (MIT.P) {} myiterator& operator++ () {++p; return *this;}
	Myiterator operator++ (int) {myiterator tmp (*this); operator++ (); return tmp;}
	BOOL operator== (const myiterator& RHS) Const {return p = = RHS.P;}
	BOOL Operator!= (const myiterator& RHS) const {return P!= rhs.p;}
int& operator* () {return *p;} private:int* p;

};
	int test_iterator_1 () {//Std::iterator: Iterator base class int numbers[] = {10, 20, 30, 40, 50};
	Myiterator from (numbers); Myiterator until (nUmbers + 5);
	for (myiterator it = from; it!= until it++) std::cout << *it << ";

	Std::cout << ' \ n ';
return 0; /////////////////////////////////////////////////int test_iterator_2 () {//Std::iterator_traits: Defining iterator properties typedef ST
	D::iterator_traits<int*> traits; if (typeid (traits::iterator_category) = = typeID (std::random_access_iterator_tag)) std::cout << "int* is a random-

	Access iterator "<< Std::endl;;
return 0; ///////////////////////////////////////////////int test_iterator_3 () {//std::advance: pushes the iterator to n element positions, can be negative std::li
	St<int> MyList;

	for (int i = 0; i<10; i++) mylist.push_back (i * 10);

	Std::list<int>::iterator it = Mylist.begin ();

	Std::advance (it, 5); Std::cout << "The sixth element in MyList is:" << *it << ' \ n ';
0 return; }////////////////////////////////////////////////////////int Test_iterator_4 () {//Std::back_inserter: Constructs a post insertion iterator that inserts a new element at the end of X STD::VECTOR&LT;INT&GT
	Foo, bar;
	for (int i = 1; I <= 5; i++) {foo.push_back (i); Bar.push_back (i * 10);

	} std::copy (Bar.begin (), Bar.end (), Std::back_inserter (foo));
	Std::cout << "foo contains:"; for (Std::vector<int>::iterator it = Foo.begin (); it!= foo.end (); ++it) std::cout << ' << *it;

	1 2 3 4 5 std::cout << ' \ n ';
return 0; //////////////////////////////////////////////////////int test_iterator_5 () {//Std::begin: Returns an iterator/STD pointing to the first element in the sequence
	:: End: Returns an iterator int foo[] = {10, 20, 30, 40, 50} that points to the last element in the sequence;

	Std::vector<int> Bar;

	Iterate foo:inserting into bar for (auto it = Std::begin (foo); it!= std::end (foo); ++it) Bar.push_back (*it);
	Iterate bar:print contents:std::cout << "bar contains:"; for (Auto it = Std::begin (bar); it!= std::end (bar); ++it) std::cout << ' << *it;

	Std::cout << ' \ n ';
return 0; }//////////////////////////////////////////int Test_iteraTor_6 () {//std::D istance: Calculates the number of elements between the last and the Std::list<int> mylist;

	for (int i = 0; i<10; i++) mylist.push_back (i * 10);
	Std::list<int>::iterator-Mylist.begin ();

	Std::list<int>::iterator last = Mylist.end (); Std::cout << "The distance is:" << std::d istance (i, last) << ' \ n ';
Ten return 0; ////////////////////////////////////////int test_iterator_7 () {//Std::front_inserter: Constructs a forward insert iterator, inserts a new element at the beginning of x std::
	deque<int> foo, bar;
	for (int i = 1; I <= 5; i++) {foo.push_back (i); Bar.push_back (i * 10);

	} std::copy (Bar.begin (), Bar.end (), Std::front_inserter (foo));
	Std::cout << "foo contains:"; For (std::d eque<int>::iterator it = Foo.begin (); it!= foo.end (); ++it) std::cout << ' << *it;

	1 2 3 4 5 std::cout << ' n ';
return 0; //////////////////////////////////////////////////int Test_iterator_8 () {//Std::inserter: Constructs an insert iterator, starting at the point where the
Inserts a new element x in a contiguous position	std::list<int> foo, bar;
	for (int i = 1; I <= 5; i++) {foo.push_back (i); Bar.push_back (i * 10);
	Std::list<int>::iterator it = Foo.begin ();

	Std::advance (it, 3);

	Std::copy (Bar.begin (), Bar.end (), Std::inserter (foo, it));
	Std::cout << "foo contains:"; for (Std::list<int>::iterator it = Foo.begin (); it!= foo.end (); ++it) std::cout << ' << *it;

	1 2 3 4 5 std::cout << ' \ n ';
return 0; }////////////////////////////////////////////////////int Test_iterator_9 () {//Std::make_move_iterator:
	From it constructs a Move_iterator object std::vector<std::string> foo (3);

	Std::vector<std::string> bar{"One", "two", "three"};

	Std::copy (Std::make_move_iterator (Bar.begin ()), Std::make_move_iterator (Bar.end ()), Foo.begin ()); Bar now contains unspecified values;

	Clear It:bar.clear ();
	Std::cout << "foo:"; for (std::string& x:foo) std::cout << ' << x; One Two three std::cout << ' \ n ';
return 0; //////////////////////////////////////////////int test_iterator_10 () {//Std::next: Gets the iterator for the next element STD::LIST&LT;INT&G T
	MyList

	for (int i = 0; i<10; i++) mylist.push_back (i * 10);
	Std::cout << "MyList:"; Std::for_each (Mylist.begin (), Std::next (Mylist.begin (), 5), [] (int x) {std::cout << ' << x;});

	0 std::cout << ' \ n ';
return 0; //////////////////////////////////////int Test_iterator_11 () {//std::p Rev: Gets the iterator for the previous element std::list<int> MyList
	;

	for (int i = 0; i<10; i++) mylist.push_back (i * 10); Std::cout << "The last element is" << *STD::p rev (Mylist.end ()) << ' \ n ';
0; ////////////////////////////////////////int Test_iterator_12 () {//Std::back_insert_iterator: An iterator that inserts an element at the end of the container std::v
	ector<int> foo, bar;
	for (int i = 1; I <= 5; i++) {foo.push_back (i); Bar.push_back (i * 10); } std::back_insert_iterator< std::vector<int> > Back_it (fOO);

	Std::copy (Bar.begin (), Bar.end (), back_it);
	Std::cout << "foo:"; for (Std::vector<int>::iterator it = Foo.begin (); it!= foo.end (); ++it) std::cout << ' << *it;

	1 2 3 4 5 std::cout << ' \ n ';
return 0; }///////////////////////////////////////////////int test_iterator_13 () {//Std::front_insert_iterator:
	An iterator that inserts an element at the beginning of the container std::d eque<int> foo, bar;
	for (int i = 1; I <= 5; i++) {foo.push_back (i); Bar.push_back (i * 10);

	std::front_insert_iterator< std::d eque<int> > Front_it (foo);

	Std::copy (Bar.begin (), Bar.end (), front_it);
	Std::cout << "foo:"; For (std::d eque<int>::iterator it = Foo.begin (); it!= foo.end (); ++it) std::cout << ' << *it;

	1 2 3 4 5 std::cout << ' n ';
return 0; //////////////////////////////////////////////int test_iterator_14 () {//std::insert_iterator: inserting iterators std::list<
	int> foo, bar; for (int i = 1; I <= 5;
	i++) {foo.push_back (i); Bar.push_back (i * 10);
	Std::list<int>::iterator it = Foo.begin ();

	Std::advance (it, 3);

	std::insert_iterator< std::list<int> > Insert_it (foo, it);

	Std::copy (Bar.begin (), Bar.end (), insert_it);
	Std::cout << "foo:"; for (Std::list<int>::iterator it = Foo.begin (); it!= foo.end (); ++it) std::cout << ' << *it;

	1 2 3 4 5 std::cout << ' \ n ';
return 0; }///////////////////////////////////////////////////////int Test_iterator_15 () {//Std::istreambuf_iterator:                    Input stream cache iterator std::istreambuf_iterator<char> Eos; End-of-range iterator std::istreambuf_iterator<char> IIT (STD::CIN.RDBUF ());

	stdin iterator std::string mystring;

	Std::cout << "Please, enter your name:";

	while (IIT!= EOS && *iit!= ' \ n ') mystring + = *iit++;

	Std::cout << "Your name is" << mystring << ". \ n";
return 0; }

////////////////////////int test_iterator_16 () {//Std::istream_iterator: Input iterator for reading continuous elements from an input stream double value1, value2;

	Std::cout << "Please, insert two values:";              Std::istream_iterator<double> EOS;   End-of-stream iterator std::istream_iterator<double> IIT (std::cin);

	StdIn iterator if (IIT!= eos) value1 = *iit;
	++IIT;

	if (IIT!= eos) value2 = *iit;

	Std::cout << value1 << "*" << value2 << "=" << (value1*value2) << ' \ n ';
return 0; ///////////////////////////////////////////////////////int test_iterator_17 () {//Std::move_iterator: For Dereference (
	dereference) to a right value reference (Rvalue reference) of the iterator std::vector<std::string> foo (3);

	Std::vector<std::string> bar{"One", "two", "three"};

	typedef std::vector<std::string>::iterator Iter;

	Std::copy (Std::move_iterator<iter> (Bar.begin ()), std::move_iterator<iter> (Bar.end ()), Foo.begin ()); Bar now contains unspecified values; Clear IT:bar.clear ();
	Std::cout << "foo:"; for (std::string& x:foo) std::cout << ' << x;

	One two three std::cout << ' \ n ';
return 0; }///////////////////////////////////////////////////////int test_iterator_18 () {//Std::ostreambuf_iterator:
	Output stream Cache iterator std::string mystring ("Some text here...\n"); Std::ostreambuf_iterator<char> out_it (std::cout); StdOut iterator Std::copy (Mystring.begin (), Mystring.end (), out_it);
Some text here ... return 0; /////////////////////////////////////////int test_iterator_19 () {//Std::ostream_iterator: Output iterator for sequential write output stream std::vect
	Or<int> Myvector;

	for (int i = 1; i<10; ++i) myvector.push_back (i * 10);
	Std::ostream_iterator<int> out_it (Std::cout, ","); Std::copy (Myvector.begin (), Myvector.end (), out_it);
0, A, n, a, a, and a. ///////////////////////////////////////////int test_iterator_20 () {//Std::reverse_iterator: Reverse iterator std::vector< Int>Myvector;

	for (int i = 0; i<10; i++) Myvector.push_back (i);
							      typedef std::vector<int>::iterator ITER_TYPE; // ?
	0 1 2 3 4 5 6 7 8 9?                     Iter_type from (Myvector.begin ());                      ^//------> Iter_type until (myvector.end ());     ^//std::reverse_iterator<iter_type> rev_until (from);     ^//<------std::reverse_iterator<iter_type> Rev_from (until);
	^ Std::cout << "Myvector:"; while (Rev_from!= rev_until) std::cout << ' << *rev_from++;

	9 8 7 6 5 4 3 2 1 0 std::cout << ' \ n ';
return 0; }//Namespace Iterator_

GitHub: Https://github.com/fengbingchun/Messy_Test

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.