C + + uses containers to find repeating columns to implement _c language

Source: Internet
Author: User
Tags mathematical functions
Copy Code code as follows:

# include <vector>
# include <iostream>
# include <set>
using namespace Std;
int main (int argc, char * argv[])
{
Vector<int> v;
Find some data to test
for (int i = 0; i < i++)
V.push_back (rand ()% 25);
for (int i = 0; i < i++)
{
cout << v[i] << ' t ';
if ((i + 1)% 10 = 0)
cout << Endl;
}
cout << Endl;
////////////////////////////////////////////////////////////////////
Start testing
Set<int> Iset; Remove duplicate data, save only one copy per data
Set<int> Repeatset; Save duplicate data
for (size_t i = 0; I!= v.size (); ++i)
{
Pair<set<int>::iterator, bool> ret = Iset.insert (v[i));
If the insertion fails, it indicates a duplicate number
if (!ret.second)
Repeatset.insert (V[i]);
}
Output test Results
for (Set<int>::iterator iter = Repeatset.begin (); Iter!= repeatset.end (); ++iter)
{
cout << *iter << "";
}
cout << Endl;
}

Container Introduction
Introduction to STL Standard Container class
Standard Container class description
Sequential containers
Vector quite with the array, quickly inserting and deleting from behind, directly accessing any element
Deque double queues, fast insertion and deletion from front or back, direct access to any element
List double linked list, quick insert and delete from anywhere
Associative container
Set Quick Lookup, no duplicate values allowed
Multiset Quick Lookup to allow duplicate values
Map one-to-one mapping, quick lookup based on keywords, no duplicate values allowed
Multimap one-to-many mappings, fast lookup based on keywords, allowing duplicate values
Container adapters
Stack backward First Out
Queue Advanced First Out
Priority_queue highest priority element is always the first one to step out
All standard library Common functions
The default constructor provides the constructor for the default initialization of the container.
The copy constructor initializes the container to the constructor of an existing replica of the same container
Destructors for memory collation when a destructor no longer requires a container
Returns true if there are no elements in the empty container, or false
Max_size returns the maximum number of elements in a container
Size returns the number of current 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, or false
Operator> returns True if the first container is greater than the second container, or false
Operator>= returns True if the first container is greater than or equal to the second container, or false
operator== returns True if the first container equals the second container, or false
Operator!= returns True if the first container is not equal to the second container, otherwise returns false
Swap swap two elements of a container
None of the operator>,operator>=,operator<,operator<=,operator==,operator!= are suitable for priority_queue
Sequential container and associated container common functions
Begin the function two versions return iterator or Const_iterator, referencing the first element of the container
End the function two versions return iterator or const_iterator, referencing the container after the last element
Rbegin the function two versions return reverse_iterator or Const_reverse_iterator, referencing the last element of the container
Rend the function two versions return reverse_iterator or Const_reverse_iterator, referring to the first element of the container preceded by a
Erase clears one or more elements from the container
Clear clears all elements in the container
The following table shows the commonly used typedef in sequential and associative containers, which are commonly used for general declarations of variables, parameters, and function return values.
The type of the element that is stored in the Value_type container
Reference a reference to an element type in a container
A constant reference in the Const_reference container that holds the element type, which reads only the elements in the container and makes the const operation
A pointer to an element type in the pointer container
Iterator point to the iterator that holds the element type in the container
Const_iterator The constant iterator that holds the element type in the container, only the elements in the container can be read
The reverse_iterator points to a reverse iterator in the container that holds the element type, which iterates backwards in the container
Const_reverse_iterator the reverse iterator that holds the element type in the container, only the elements in the container can be read
Difference_type the type of two iterator subtraction results that refer to the same container (list and association containers do not define operator-)
Size_type is used to calculate the number of items in the container and the type of the retrieval order container (cannot be retrieved on the list)

Comparison of containers
Vector (continuous space storage, you can use the "" operator) to quickly access random elements, fast insertion of elements at the end, but in the middle of the sequence of insertion, delete elements slow, and if the initial allocation of space is not enough
Then there's a redistribution of larger space and then a copy of the performance overhead
Deque (small pieces of continuous, small pieces linked to a linked list, in fact, is a small array of pointers, because know the type, so still can use "", but the speed is not vector fast) fast access to random elements, fast
Inserts the element at the beginning and the end, randomly inserts, deletes the element to be slow, the space redistribution is faster than the vector
List (linked to each element) access to random elements is not as fast as vector, random insertion element faster than vector, allocate space for each element, so there is no room for redistribution
Set internal element unique, with a balanced tree structure to store, so the time to traverse the sort, and find a faster oh.
The combination of map one-to-one mappings, key cannot be repeated.
Multiset
Multimap
Stack adapters that must be used in conjunction with other containers
The queue is a restricted dequem
Vector<bool> and Bitset<>, the front can dynamically change the length.
Priority_queue inserts the element to have the priority order, the top comes out is the highest priority
Valarray specifically for numerical calculations, adding 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.