2.Container of CPP

I. Overview

1. Intro

   The Containers library is a generic collection of class templates and algorithms that allow programmers to easily implement common data structures

2. types

  • sequence containers : vector, deque, list, array. forward,list
  • associative containers : map, set, multiset, multimap
  • unordered associative containers : unordered_associative containers

3. container adaptors

  stack, queue, priority_queue;

II. Iterator

1. Intro

  An iterator is any object that, pointing to some element in a range of elements (container or array), has the ability to iterate through the elements of that range using a set of operators(with at least the increment (++) and dereference (*) operators)

2. Iterator categories

Input and output iterators

  the most limited types of iterators: they can perform sequential single-pass input or output operations.

Forward iterators
  • they have all the functionality of input iterators and -if they are not constant iterators- also the functionality of output iterators
  • they are limited to one direction in which to iterate through a range (forward).
  • All standard containers support at least forward iterator types.
Bidirectional iterators
  • they are like forward iterators
  • they can also be iterated through backwards.
Random-access iterators
  • they implement all the functionality of bidirectional iterators
  • they also have the ability to access ranges non-sequentially: distant elements can be accessed directly by applying an offset value to an iterator without iterating through all the elements in between.
  • These iterators have a similar functionality to standard pointers (pointers are iterators of this category).
the properties of random-access iterator category
  • Can be incremented a++
  • Supports equality/inequality comparisons a == b; a != b
  • Can be dereferenced as an rvalue *a
  • Can be dereferenced as an lvalue *a == t
  • Can be decremented a--
  • Supports arithmetic operators + and - a + n; a - n;
  • Supports inequality comparisons (<, >, <= and >=) between iterators
  • Supports compound assignment operations += and -=
  • Supports offset dereference operator ([]) a[n]
  • ! Each container may define its own specific iterator type able to iterate through it and access its elements.
    • iterator of vector is random-accesse type iterator

3. functions

cont.begin()  
	//Returns an iterator pointing to the **first element** in the sequence
cont.rbegin() 
	//Returns an iterator pointing to the first element  in the reversed sequence (i.e. the last element)
cont.end()    
	//Returns an iterator pointing to the **past-the-end element** in the sequence 
void advance (InputIterator& it, Distance n); 
    //eqto using operator+ for n times, if n > cont.size() it will cross border 
distance (InputIterator first, InputIterator last); 
    //Calculates the number of elements between first and last [first, last)
BidirectionalIterator prev (BidirectionalIterator it, typename \
iterator_traits<BidirectionalIterator>::difference_type n = 1);
ForwardIterator next (ForwardIterator it, typename iterator_traits<ForwardIterator>::difference_type n = 1);
vector<int>::iterator it = v.begin();// v = {0,1,2,3,4};
advance(it,2); //*it = 2
v[2]; // v[2] == 1

III. Vector

Member Function

1. constructor
	//default (1)	
explicit vector (const allocator_type& alloc = allocator_type());
	//fill (2)	
explicit vector (size_type n);
vector (size_type n, const value_type& val, const allocator_type& alloc = allocator_type());
	//range (3)	
template <class InputIterator>
vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());
	//copy (4)
vector (const vector& x);
vector (const vector& x, const allocator_type& alloc);
	//move (5)
vector (vector&& x);
vector (vector&& x, const allocator_type& alloc);
	//initializer list (6)
vector (initializer_list<value_type> il, const allocator_type& alloc = allocator_type());
vector<int> v1(3); 		// 0 0 0
vector<int> v2(3,5);	//5 5 5
string s = "Hello";
vector<char> v3(s.begin(), s.end());	//H e l l o
vector<char> v4(v3);	//H e l l o
2. Iterators

cont.begin() cont.end() cont.rbegin()

3. Element Access
cont.at(index) //return *a reference* to the requested element; 
			   //cont.at(index) = value; cout << cont.at(index)
cont[index]
cont.front()   //return *a reference* to the first element
cont.back()    //return *a reference* to the last element
4. Capacity
cont.empty() // return true if the container is empty
`cont.size() // returns the number of elements in the container
5. Modifiers
cont.clear() //erases all elements
iterator insert( iterator pos, const T& value ); 
	//inserts value before pos
iterator insert( const_iterator pos, size_type count, const T& value );  
	//inserts count copies of the value before pos
iterator erase (const_iterator pos);	
	//Removes from the vector a **single** element pointed bt pos
iterator erase (const_iterator first, const_iterator last); 
	//Removes a range of elements  *[first,last)*
cont.push_back(T& val) //Adds a new element "val" at the end of the vector
void pop_back() 
	//Removes the last element in the vector, effectively reducing the container size by one.
void swap (vector& x); 
	/* Exchanges the content of the container by the content of x, which is another vector object of the same 	type. Sizes may differ.*/
//insert
#include <iostream>
#include <vector>
vector<int> v = {1,2,3,4};
auto it = v.begin();
v.insert(it, 0);	//v = {0,1,2,3,4}
v.insert(v.end(),3,0); // v = {0,1,2,3,4,0,0,0}
//erase

IV. Map

1. Overview

  • the key value are generally used to sort and uniquely identify the elements, while the mapped values store he content associated to this key.
  • the type of key and mapped value may differ
  • key and mapped value are grouped together in a pair type pair<const Key, T>

2. Iterators

  • a bidirectional iterator to value_type
  • cont.begin() cont.end() cont.rbegin()

3. Element Access

cont.at(key) return a reference to the requested element; cont.at(index) = value; cout << cont.at(index)
cont[key]

4. Capacity

cont.empty() return true if the container is empty
cont.size() returns the number of elements in the container

5. Modifiers

cont.clear() erases all elements
iterator insert( iterator pos, const T& value ); inserts value before pos
iterator insert( const_iterator pos, size_type count, const T& value ); inserts count copies of the value before pos
iterator erase (const_iterator pos); Removes from the vector a single element pointed bt pos
size_type erase (const key_type& k);
iterator erase (const_iterator first, const_iterator last); Removes a range of elements [first,last)
void swap (vector& x); Exchanges the content of the container by the content of x, which is another vector object of the same type. Sizes may differ.

6. operations

iterator find (const key_type& k); Searches the container for an element with a key equivalent to k and returns an iterator to it if found, otherwise it returns an iterator to map::end.

7. traverse

for (auto it : a) {
		cout << it.first << " " << it.second << endl;
	}

V. Set

1. Overview

  • Sets are containers that store unique elements following a specific order.
  • the elements are always const i.e. it can not be modified
  • the elements in a set are always sorted following a specific strict weak ordering criterion indicated by its internal comparison object
  • unordered_set is faster

VI. Unordered Set

1. Overview

Unordered sets are containers that store unique elements in no particular order

2. Constructor

//empty (1)	
explicit unordered_set ( size_type n = /* see below */,
                         const hasher& hf = hasher(),
                         const key_equal& eql = key_equal(),
                         const allocator_type& alloc = allocator_type() );
explicit unordered_set ( const allocator_type& alloc );
//range (2)	
template <class InputIterator>
         unordered_set ( InputIterator first, InputIterator last,
                         size_type n = /* see below */,
                         const hasher& hf = hasher(),
                         const key_equal& eql = key_equal(),
                         const allocator_type& alloc = allocator_type() );
//copy (3)	
unordered_set ( const unordered_set& ust );
unordered_set ( const unordered_set& ust, const allocator_type& alloc );
//move (4)	
unordered_set ( unordered_set&& ust );
unordered_set ( unordered_set&& ust, const allocator_type& alloc );
//initializer list (5)	
unordered_set ( initializer_list<value_type> il,
                size_type n = /* see below */,
                const hasher& hf = hasher(),
                const key_equal& eql = key_equal(),
                const allocator_type& alloc = allocator_type() );

3.Element lookup

  • iterator find ( const key_type& k );
  • size_type count ( const key_type& k ) const;

4.Modifiers

  • insert
//(1)	
pair<iterator,bool> insert ( const value_type& val );
//(2)	
pair<iterator,bool> insert ( value_type&& val );
//(3)	
iterator insert ( const_iterator hint, const value_type& val );
//(4)	
iterator insert ( const_iterator hint, value_type&& val );
//(5)	
template <class InputIterator>
    void insert ( InputIterator first, InputIterator last );
//(6)	
void insert ( initializer_list<value_type> il );
  • erase

VII. Forward List

1. Overview

  • Forward lists are implemented as singly-linked lists
  • Drawback
  • they lack direct access to the elements by their position

2. Member Functions

Constructor
//default (1)	
explicit forward_list (const allocator_type& alloc = allocator_type());
//fill (2)	
explicit forward_list (size_type n);
explicit forward_list (size_type n, const value_type& val,
                        const allocator_type& alloc = allocator_type());
//range (3)	
template <class InputIterator>
  forward_list (InputIterator first, InputIterator last,
                const allocator_type& alloc = allocator_type());
//copy (4)	
forward_list (const forward_list& fwdlst);
forward_list (const forward_list& fwdlst, const allocator_type& alloc);
//move (5)	
forward_list (forward_list&& fwdlst);
forward_list (forward_list&& fwdlst, const allocator_type& alloc);
//initializer list (6)	
forward_list (initializer_list<value_type> il,
              const allocator_type& alloc = allocator_type());
Operator =

Assigns new contents to the container, replacing its current contents.

3.Iterators

  • before_begin Return iterator to before beginning
    It is meant to be used as an argument for member functions emplace_after, insert_after, erase_after or splice_after, to specify the beginning of the sequence
  • begin Return iterator to beginning
    end Return iterator to end
    cbefore_begin Return const_iterator to before beginning
    cbegin Return const_iterator to beginning
    cend Return const_iterator to end

4. Capacity

empty Test whether array is empty
max_size Return maximum size

5.Element access

front Access first element

6.Modifiers

  • assign Assign content
    Assigns new contents to the forward_list container, replacing its current contents, and modifying its size accordingly.
//range (1)	
template <class InputIterator>
  void assign (InputIterator first, InputIterator last);
//fill (2)	
void assign (size_type n, const value_type& val);
//initializer list (3)	
void assign (initializer_list<value_type> il);
  • emplace_front Construct and inserts a new element at the beginning
  • push_front Insert a new element at beginning
  • pop_front Delete first element
  • emplace_after Construct and insert element
  • insert_after Insert elements
//(1)	
iterator insert_after ( const_iterator position, const value_type& val );
//(2)	
iterator insert_after ( const_iterator position, value_type&& val );
//(3)	
iterator insert_after ( const_iterator position, size_type n, const value_type& val );
//(4)	
template <class InputIterator>
  iterator insert_after ( const_iterator position, InputIterator first, InputIterator last );
//(5)	
iterator insert_after ( const_iterator position, initializer_list<value_type> il );
  • erase_after Erase elements
  • swap Swap content between two forward_list
  • resize Change size
  • clear Clear content

7. Operations

  • splice_after Transfers elements from fwdlst into the container inserting them after the element pointed by position.
//entire list (1)	
void splice_after (const_iterator position, forward_list& fwdlst);
void splice_after (const_iterator position, forward_list&& fwdlst);
//single element (2)	
void splice_after (const_iterator position, forward_list& fwdlst, const_iterator i);
void splice_after (const_iterator position, forward_list&& fwdlst, const_iterator i);
//element range (3)	
void splice_after (const_iterator position, forward_list& fwdlst,
                   const_iterator first, const_iterator last);
void splice_after (const_iterator position, forward_list&& fwdlst,
                   const_iterator first, const_iterator last);
  • remove Remove elements with specific value (public member function )
  • remove_if Remove elements fulfilling condition (public member function template )
  • unique Remove duplicate values (public member function )
  • merge Merge sorted lists (public member function )
  • sort Sort elements in container (public member function )
  • reverse Reverse the order of elements

VIII. List

1. Overview

  • List containers are implemented as doubly-linked lists

2. Member Function

similar to forward_list

3. Iterators

begin end

4. Element access:

front Access first element
back Access last element

5. Modifiers

  • assign assign a new content to container
  • push_front insert element at beginning
  • pop_front
  • push_back
  • pop_back
  • insert insert new elements before the element at the specified position.
//single element (1)	
iterator insert (const_iterator position, const value_type& val);
//fill (2)	
iterator insert (const_iterator position, size_type n, const value_type& val);
//range (3)	
template <class InputIterator>
iterator insert (const_iterator position, InputIterator first, InputIterator last);
//move (4)	
iterator insert (const_iterator position, value_type&& val);
//initializer list (5)	
iterator insert (const_iterator position, initializer_list<value_type> il);

6. Operations

  • splice transfer elements from list to list
  • remove remove elements with specific value
  • unique remove duplicate values
  • merge Merges x into the list by transferring all of its elements at their respective ordered positions into the container
//(1)	
  void merge (list& x);
  void merge (list&& x);
//(2)	
template <class Compare>
  void merge (list& x, Compare comp);
template <class Compare>
  void merge (list&& x, Compare comp);
  • sort sort elements in some order
  • reverse

IX. Queue

X. Stack

Member Functions

  • constructor
//initialize (1)	
explicit stack (const container_type& ctnr);
//move-initialize (2)	
explicit stack (container_type&& ctnr = container_type());
//allocator (3)	
template <class Alloc> explicit stack (const Alloc& alloc);
//init + allocator (4)	
template <class Alloc> stack (const container_type& ctnr, const Alloc& alloc);
//move-init + allocator (5)	
template <class Alloc> stack (container_type&& ctnr, const Alloc& alloc);
//copy + allocator (6)	
template <class Alloc> stack (const stack& x, const Alloc& alloc);
//move + allocator (7)	
template <class Alloc> stack (stack&& x, const Alloc& alloc);
  • empty Test whether container is empty
  • size Return size
  • top Access next element
  • push Insert element
  • emplace Construct and insert element
  • pop Remove top element
  • swap Swap contents

XI. Array

1. Overview

  • Arrays are fixed-size sequence containers: they hold a specific number of elements ordered in a strict linear sequence.
  • Arrays have a fixed size and do not manage the allocation
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值