Introduction to C ++ containers and repeated search examples

Source: Internet
Author: User


# Include <vector>

# Include <iostream>

# Include <set>

 

Using namespace std;

 

Int main (int argc, char * argv [])

{

Vector <int> v;

// Find some data for testing

For (int I = 0; I <50; I ++)

V. push_back (rand () % 25 );

For (int I = 0; I <50; I ++)

{

Cout <v [I] <'\ T ';

If (I + 1) % 10 = 0)

Cout <endl;

}

Cout <endl;

//////////////////////////////////////// ////////////////////////////

// Start the test

Set <int> iset; // removes duplicate data. Only one copy of each data is saved.

Set <int> repeatset; // stores duplicate data.

For (size_t I = 0; I! = V. size (); ++ I)

{

Pair <set <int >:: iterator, bool> ret = iset. insert (v [I]);

// If the insert operation fails, it indicates the number is repeated.

If (! Ret. second)

Repeatset. insert (v [I]);

}

// Output the test result

For (set <int >:: iterator iter = repeatset. begin (); iter! = Repeatset. end (); ++ iter)

{

Cout <* iter <"";

}

Cout <endl;

}

 

 

 

 

 

 

Container Overview

 

Introduction to STL standard containers

 

Standard containers

 

Ordered container

 

Vector is equivalent to an array. It can be quickly inserted and deleted from the back to directly access any element.

 

Deque dual queue, which can be quickly inserted and deleted from the front or back to directly access any element

 

List double-stranded tables, fast insertion and deletion from anywhere

 

Associated container

 

Set quick search, repeated values not allowed

 

Multiset quick search, allowing repeated values

 

Map one-to-one ing, fast search based on keywords, repeated values are not allowed

 

Multimap one-to-multiple ing, fast search based on keywords, allowing repeated values

 

Container Adapter

 

Stack first-in-first-out

 

Queue first-in-first-out

 

Priority_queue the highest priority element is always the first column

 

Common functions in all standard Libraries

 

The default constructor provides the constructor initialized by default by the container.

 

The copy constructor initializes the container as the constructor of the existing similar container copy.

 

Memory-organized destructor no longer needed for containers

 

True is returned if the empty container has no elements; otherwise, false is returned.

 

Max_size returns the maximum number of elements in the container.

 

Size returns the current number of elements in the container.

 

Operator = assign one container to another

 

Operator <if the first container is smaller than the second container, true is returned; otherwise, false is returned,

 

Operator <= if the first container is less than or equal to the second container, true is returned; otherwise, false is returned.

 

Operator> If the first container is greater than the second container, true is returned; otherwise, false is returned.

 

Operator> = if the first container is greater than or equal to the second container, true is returned; otherwise, false is returned.

 

Operator = If the first container is equal to the second container, true is returned; otherwise, false is returned.

 

Operator! = If the first container is not equal to the second container, true is returned; otherwise, false is returned.

 

Swap two container elements

 

Operator>, operator >=, operator <, operator <=, operator =, operator! = Not applicable to priority_queue

 

Sequential container and associated container common functions

 

Begin this function two versions return iterator or const_iterator, reference the first element of the container

 

End this function returns two versions of iterator or const_iterator, referencing the last element of the container

 

Rbegin the two versions of the function return reverse_iterator or const_reverse_iterator, referencing the last element of the container

 

Rend. The two versions of this function return reverse_iterator or const_reverse_iterator, which references the first element of the container.

 

Erase clears one or more elements from the container.

 

Clear all elements in the container

 

The following table shows the typedef commonly used in sequential containers and associated containers. These typedef are commonly used in the general declaration of variables, parameters, and function return values.

 

Type of elements stored in the value_type container

 

Reference container stores reference of element type

 

The const_reference container stores constant references of the element type. This reference can only read elements in the container and perform the const operation.

 

Pointer for storing element types in the pointer container

 

Iterator points to the iterator that stores element types in the container

 

Const_iterator points to the constant iterator that stores element types in the container and can only read elements in the container.

 

The reverse_iterator points to the reverse iterator that stores element types in the container, which reversely iterates in the container.

 

Const_reverse_iterator directs to the reverse iterator that stores element types in the container and can only read elements in the container.

 

Difference_type refers to the type of the result of two iterators subtract from the same container (list and associated containers do not define operator -)

 

Size_type is used to calculate the number of items in the container and the type of the retrieval sequence container (list retrieval is not allowed)

 

Comparison of four containers

 

Vector (continuous space storage, you can use the [] Operator) to quickly access random elements, quickly Insert elements at the end, but insert Between the ages of the sequence, it is slow to delete elements, and if the space allocated at the beginning is insufficient

 

There is a larger space for re-allocation, and then the performance overhead of copying

 

Deque) quick access to random elements, fast

 

Insert elements at the beginning and end, random insertion, and slow deletion of elements, and faster space allocation than vector.

 

List (each element is connected by a linked list) accessing random elements is not as fast as vector, and random inserted elements are faster than vector. Therefore, space is allocated to each element. Therefore, there is no sufficient space and the allocation is re-allocated.

 

The internal elements of the set are unique and stored in a balance tree structure. Therefore, the elements are sorted during traversal and the search speed is relatively high.

 

The key of a map must be unique.

 

Multiset

 

Multimap

 

Stack adapter, which must be used together with other containers

 

Queue is a restricted dequem

 

Vector <bool> and bitset <>. The length can be changed dynamically.

 

The elements inserted by priority_queue have the highest priority.

 

Valarray is specialized in numerical calculation and adds special mathematical functions.

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.