Introduction to C + + containers

Source: Internet
Author: User
Tags value store

The container is the place where the data is stored.

The STL (Template Library) for C + + has two types of containers: sequential containers and associative containers. Simply put, a sequential container aggregates a single type of element into a container, and then stores and accesses the elements based on their location . The associated container stores and reads the elements by key .

Standard Container class Description
Sequential containers
Vector Quick Insert and delete from behind, direct access to any element
Deque Quick Insert and delete from front or back, direct access to any element
List Double linked list, quick insert and delete from anywhere
Associative containers
Set Quick Find, no duplicate values allowed
Multiset Quick Find, allow duplicate values
Map One-to-many mappings, fast lookup based on keywords, no duplicate values allowed
Multimap One-to-many mappings, quick find based on keywords, allow duplicate values
Container Adapter
Stack Last in, first out
Queue Advanced First Out
Priority_queue The highest priority element is always the first out-of-order

Common functions for all standard libraries

Default constructor Provides a constructor for container default initialization.
Copy constructor constructor that initializes the container to an existing copy of the same container
Destructors Destructors for memory grooming when containers are no longer needed
Empty Returns true if there are no elements in the container, otherwise false
Max_size Returns the maximum number of elements in a container
Size Returns the current number of elements in a container
Operator= Assign a container to another container
operator< Returns true if the first container is less than the second container, otherwise returns false,
operator<= Returns true if the first container is less than or equal to the second container, otherwise false
Operator> Returns true if the first container is greater than the second container, otherwise false
operator>= Returns true if the first container is greater than or equal to the second container, otherwise false
operator== Returns true if the first container equals the second container, otherwise false
operator!= Returns true if the first container is not equal to the second container, otherwise false
Swap Swapping elements of two containers

None of the operator>,operator>=,operator<,operator<=,operator==,operator!= are suitable for priority_queue

Sequential container and associative container common functions

Begin Two versions of the function return iterator or const_iterator, referencing the first element of the container
End The function of two versions returns iterator or Const_iterator, referencing the last element of the container after a
Rbegin Two versions of the function return Reverse_iterator or const_reverse_iterator, referencing the last element of the container
Rend The function of two versions returns reverse_iterator or Const_reverse_iterator, referencing the first element of the container before a
Erase Clear one or several elements from a container
Clear Clears all elements in the container

①ector

The most commonly used container, originally designed to replace the C language of the array, can be automatically expanded (when I first started to spit groove why not support VLA ... ), supports random storage, adds an O (1) to the end, and an O (n) in the middle. But the problem is also very large, the data itself is stored in the array, in the size is not enough time to copy the data to a larger place.

How to do the wiki is specific: Https://zh.wikipedia.org/wiki/Vector_ (STL)

②list

List, the implementation method is a single-linked list, the insertion of the deletion at any part of the complexity of the time is O (n), the tail O (1), but does not support random access, and because each element also needs to store the pointer, so the memory overhead is much larger than the vector.

Detailed reading: Https://zh.wikibooks.org/wiki/C%2B%2B/STL/forward_list

③ deque

The combination of list and array, support random access, delete Insert is O (n), both ends are O (1) (push pop), but store the front and back pointers, the memory overhead is greater.

Detailed reading: Https://zh.wikibooks.org/wiki/C%2B%2B/STL/Deque

④ map,set,multimap,multiset

By key value store, you can sort by key value, find complexity O (logn)

map and multimap is associative containers that manage key/value pairs as elements as seen above. The elements of each container would sort automatically using the actual key for sorting criterion. The difference between the and that maps does not allow duplicates, whereas, Multimaps does.

  • Map-unique keys
  • Multimap-same key can be used many times
  • Set-unique key is the value
  • Multiset-key is the value, same key can be used many times

Map and Multimap code examples:

/*Map example-character Distribution*/#include<iostream>#include<map>#include<string>#include<cctype>using namespacestd; intMain () {/*Character counts is stored in a map, so that's * Character is the key. * Count of Char A is chars[' a ']. */Map<Char,Long>chars; cout<<"Chardist-count Character Distributions"<<Endl; cout<<"Type some text. Press ctrl-d to quit."<<Endl; CharC;  while(CIN.)Get(c)) {//Upper A and lower A are considered the sameC=tolower (static_cast<unsignedChar>(c)); CHARS[C]=chars[c]+1;//Could be written as ++chars[c];} cout<<"Character Distribution:"<<Endl; stringAlphabet"abcdefghijklmnopqrstuvwxyz");  for(string:: Iterator Letter_index=alphabet.begin (); Letter_index! = Alphabet.end (); letter_index++) {                  if(Chars[*letter_index]! =0) {cout<<Char(ToUpper (*Letter_index)) <<":"<< chars[*Letter_index]<<"\ t"<<Endl; }          }          return 0; }
View Code

Reference

STL Ten containers, Just_kong, http://www.aiuxian.com/article/p-2119514.html

C + + Programming/stl, https://en.wikibooks.org/wiki/C%2B%2B_Programming/STL#Containers

Introduction to C + + containers

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.