C ++ standard library study notes 3-Chapter 5

Source: Internet
Author: User

This chapter describes the general introduction of containers, iterators, algorithms, and function simulation. It is a summary of chapter 6, 7, and 8.

 

  • 1. (P74)

The basic concept of STL is to separate data from operations. Data is managed by container categories, and operations are defined by custom algorithms. The iterator acts as an adhesive between the two. Any algorithm can interact with any container.

 

 

  • 2. (P75)

The container category is used to manage a group of elements.

In general, containers are divided into two categories:

①. Sequence containers is an ordered cluster. Including vector, queue, and list.

②. Associative containers is a sorted cluster. Including set, multiset, map, and multimap.

The associated container Automatically sorts its elements based on specific sorting rules. The sorting criterion is presented as a function to compare the value or key of an element ). By default, operator <is used for comparison. However, you can also provide a comparison function to define different sorting rules.

Eg

Set <int> tk1; // sort data in ascending order
Set <int, greater <int> tk2; // sort data in ascending order.

 

Collation

Generally, the associative container is implemented by the binary sorting tree.

In addition, the C ++ Standard Library provides the Container adapter (Container Adpters), stack, queue, and priority queue.

 

  • 3. (P91)

About the key/value of pair.

It can be accessed through first and second.

Eg

Pos-> first;
(* Pos). second;

 

  • 4. (P93)

Iterator category:

①. Bidirectional iterator, which is provided by list, set, multiset, map, and multimap.

② Random access iterator, which is provided by vector, deque, and strings.

 

  • 5. (P104)

Iterator Adapter)

① Insert iterators (Insert iterator)

Including back_inserter (), front_inserter (), inserter (), and push_back (), push_front (), and insert ().

② Stream iterators (Stream iterator)

Istream_iterator <T> (cin) reads the T type,

Istream_iterator <T> () generates a "stream Terminator"

Ostream_iterator <T> (cout) standard reading

③ Reverse iterators (Reverse iterator)

 

The following example uses three iterators:

// Author: Tanky Woo
// Blog: www.WuTianQi.com
# Include <cstddef>
# Include <iostream>
# Include <vector>
# Include <list>
# Include <deque>
# Include <set>
# Include <algorithm>
# Include <iterator>
Using namespace std;

Int main ()
{
List <int> coll1;
For (int I = 1; I <= 9; ++ I)
Coll1.push _ back (I );
Cout <"insert iterator \ n ";
Vector <int> coll2;
Copy (coll1.begin (), coll1.end (), back_inserter (coll2 ));
Deque <int> coll3;
Copy (coll1.begin (), coll1.end (), front_inserter (coll3 ));
Set <int> coll4;
Copy (coll1.begin (), coll1.end (), inserter (coll4, coll4.begin ()));
Copy (coll1.begin (), coll1.end (), ostream_iterator <int> (cout ,""));
Cout <endl;
Copy (coll2.begin (), coll2.end (), ostream_iterator <int> (cout ,""));
Cout <endl;
Copy (coll3.begin (), coll3.end (), ostream_iterator <int> (cout ,""));
Cout <endl;
Copy (coll4.begin (), coll4.end (), ostream_iterator <int> (cout ,""));
Cout <endl;

Cout <"--------------------------------------------------- \ n ";
Cout <"reverse iterator \ n ";
Copy (coll1.rbegin (), coll1.rend (), ostream_iterator <int> (cout ,""));
Cout <endl;

Cout <"--------------------------------------------------- \ n ";
Cout <"stream iterator \ n ";
Vector <int> coll5;
Copy (istream_iterator <int> (cin), istream_iterator <int> (), back_inserter (coll5 ));
Copy (coll5.begin (), coll5.end (), ostream_iterator <int> (cout ,""));
Cout <endl;

System ("PAUSE ");
Return EXIT_SUCCESS;
}

 

 

  • 6. (P124)

Not to mention, I will summarize the details later.

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.