C + + Advanced path 04STL knowledge

Source: Internet
Author: User
Tags define function

    1. Templates parameterize the data types to be handled by a function or class, that is, the polymorphism of the parameters. is a code reuse mechanism. Function templates have several overloads that define function templates with the same name, provide different parameters and implementations, and can be overloaded with other non-function templates.

    2. function templates, there is no type conversion mechanism for normal functions. So sometimes the function template is overloaded with normal functions. function is to find a specific function, and then find the function template.

    3. Unlike function templates, the instantiation of a function template is done automatically by the compilation system when the function call is processed, and the instantiation of the class template must be explicitly specified by the programmer in the program.

    4. C + + contains a standard library with many components. The Standard Template Library is part of the standard C + + standard library. There are 3 main types of components in STL: containers, iterators, and algorithms.

    5. A container is a data structure that is an object that contains objects. A container is a class template for commonly used data structures. Classification of containers: sequential containers and associative containers.

    6. Sequential container: vector,deque,list, associative container: set,multiset,map,multimap

    7. A sequential container is a group of elements of the same type that are organized in a strictly linear form. A vector,deque,list,deque is a mixture of vectors and lists.

      Basic operation of Vector:


I. Definition and initialization

vector< typeName > v1; The default v1 is empty, so the following assignment is the wrong v1[0]=5;

Vector<typename>v2 (v1); or V2=V1, or vector<typename> v2 (V1.begin (), V1.end ()),//v2 is a copy of V1 if V1.size () >v2.size () is assigned v2.size () is expanded to V1.size ().

vector< typeName > V3 (n,i);//v3 a typeName type element with n values of I

vector< typeName > V4 (n); V4 contains n elements with a value of 0

int a[4]={0,1,2,3,3}; Vector<int> v5 (a,a+5); The size of//V5 is the 5 values that 5,V5 is initialized to a. The latter pointer points to the next position of the last element that will be copied.

Vector<int> V6 (V5);//V6 is v5


Second, the vector object The most important several operations

1. V.push_back (t) adds a value of t at the end of the container, and the size of the container becomes larger.

In addition the list has the Push_front () function, which is inserted at the front end, and the following elements are incremented sequentially.

2. V.size () returns the number of data in the container, and the size returns the value of the size_type defined by the corresponding vector class. V.resize (2*v.size) or


V.resize (2*v.size, 99) doubles the capacity of V (and initializes the value of the new element to 99)

3. V.empty () Determine if the vector is empty

4. V[n] Returns the element with position N in V

5. V.insert (pointer,number, content) inserts the contents of a number in the position pointed to by the pointer in V.

and V. Insert (pointer, content), V.insert (Pointer,a[2],a[4]) inserts three elements of a[2] to a[4].

6. V.pop_back () Deletes the end element of the container and does not return the element.

7.v.erase (Pointer1,pointer2) removes elements from the middle of pointer1 to Pointer2, including Pointer1.

After removing an element from the vector, the elements in this position need to move forward one position, although the current iterator position does not automatically add 1,

But because the subsequent elements are moved forward in the same way, they are equivalent to the iterator's automatic pointing to the next position.

8. V1==V2 judge whether V1 and V2 are equal.

9.! =, <, <=, >, >= keep these operators in the habit of meaning.

Vector<typename>::iterator P=v1.begin (); The P-Initial value points to the first element of the V1. *p the value of the pointed element.

For const vector<typename> can only be accessed with pointers of type Vector<typename>::const_iterator.

One. P=v1.end (); P points to the next position of the last element of the V1.

12.v.clear () deletes all the elements in the container. 12.v.clear () deletes all the elements in the container.


using namespace Std;


bool COM (int x)

{

if (x% 9 = = 0)

return true;

Else

return false;

}


Template<typename t>

void Show (T t)

{

for (T::iterator it = T.begin (); It! = T.end (); it++)

cout<<*it<< "";

cout<<endl;

}


int _tmain (int argc, _tchar* argv[])

{

int a[5] = {0,1,2,3,4};

Vector<int> v1 (10);

Show (v1);

Vector<int> v2;

Show (v2);

Vector<int> v3 (10,1);

Show (v3);

Vector<int> V4 (A, &a[3]);

Show (v4);

Vector<int> v5;

for (int i = 0; i < one; i++)

V5.push_back (i);

Show (v5);

Cout<<v2.size () <<endl;

v2 = v5;

Cout<<v2.size () <<endl;

Show (v2);

cout<< "Vector initialization" <<endl;

cout<< "--------------------------------" <<endl;

Cout<<v3.empty () <<endl;

V2.resize (2 * v2.size ());

Cout<<v2.size () <<endl;

V2.resize (2 * v2.size (), 99);

Show (v2);

while (V2.size ()! = 11)

{

V2.pop_back ();

}

Show (v2);

V2.erase (V2.begin (), V2.begin () + 1);

Show (v2);

V2.clear ();

Show (v2);

cout<< "Vector operation" <<endl;

cout<< "----------------------------------------" <<endl;

Show (v5);

cout<<* (Find (V5.begin (), V5.begin () +)) <<endl; Returns the value of end () if not found, program exception

cout<<* (Find_if (V5.begin () + 1,v5.begin () + ten, com)) <<endl;

System ("pause");

return 0;

}


Deque's operation is similar to vector, the following describes Deque memory is not the same as vector.


int _tmain (int argc, _tchar* argv[])

{

Vector<int> v1 (2);

deque<int> D1 (2);

int* P1 = &v1[0];

int* P2 = &d1[0];


cout<<*p1<< "," <<p1<<endl;

cout<<*p2<< "," <<p2<<endl;


V1.push_back (20);

D1.push_back (20);


cout<<*p1<< "," <<p1<<endl;

cout<<*p2<< "," <<p2<<endl;


cout<<v1[0]<< "," <<&v1[0]<<endl;


System ("pause");

return 0;

}


Because, in the establishment of the vector container, generally with the establishment of space-fill data, such as the reconstruction of the larger space, copy the original spatial data, delete the original space, add new data, so repeatedly, to ensure that the vector is always a separate contiguous memory space In the establishment of the Deque container, generally with the establishment of space--building a new space-----fill the new data, so repeatedly, no original spatial data replication and deletion process, is composed of multiple contiguous memory space.



NT _tmain (int argc, _tchar* argv[])

{

List<int> L1;

for (int i = 0; i < one; i++)

L1.push_back (i);


List<int>::reverse_iterator it = L1.rbegin ();

cout<<*it<<endl;


Show (L1);

Reverse (L1.begin (), L1.end ());

Show (L1);


cout<< "---------------------------------" <<endl;

List<int> L2;

L2.push_back (3);

L2.push_back (1);

L2.push_back (5);

L2.sort ();

l2.reverse ();

Show (L2);


List<int> L3;

L3.push_back (7);

L3.push_back (6);

L3.push_back (8);

L3.sort ();

l3.reverse ();

Show (L3);


L2.merge (L3);

Show (L2);

Show (L3);


L2.splice (L2.begin (), L3);

Show (L2);

Show (L3);


System ("pause");

return 0;

}

The difference between merge () and splice () needs to be clear.


Associative containers have the ability to quickly extract elements based on a set of indexes, where elements can be accessed by key.

A set is a collection of 0 or more non-repeating and unordered elements called key values. Multiset is a set that is allowed to have duplicate key values.

A map is an image that can contain 0 or more unordered element pairs, one element is a non-repeating key value, and the other is a key-related value. Multimap allows a map with duplicate key values.



This article from "Very blog" blog, reproduced please contact the author!

C + + Advanced path 04STL knowledge

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.