Related functions commonly used by Vecto container R in C + +

Source: Internet
Author: User

Http://blog.chinaunix.net/uid-20622737-id-3278427.html

Examples of vectors used in C + +

I. Overview

Vector is part of the C + + Standard Template Library, which is a multifunctional template class and function library that can manipulate a variety of data structures and algorithms. A vector is a container that can hold various types of objects, in short, a vector is a dynamic array that can hold any type, dynamically changing its size.

For example:

C language Style

int myhouse[100];

Using vector

Vector Vecmyhouse (100); Defined

When defined as above, Vecmyhouse can store 100 data of type int.

1. It can be accessed like a normal array

EG:VECMYHOUSE[50] = 1024; Access

2. You can populate the container with data sequentially

Eg:int i = 0;

for (;i<; i++)

{

Vecmyhouse.push_back (1); add element (increase to last)

}

3. It can also dynamically change its size, using the following statement to achieve

Change the size of the container to 400 so that the container can hold 400 int data

Eg:vecMyHouse.resize (400); Change the size of the vector

4. You can also load a custom data type in a container

eg

Customizing a Class

Class CMyClass

{

};

Define a container to hold class

Vector Vecmyhouse;

5. You can assign an initial value to a container when you define it

Defines a container that holds 100 int data, with the initial value assigned to 0

Vector Vecmyhouse (100,0);

6. You can assign a container's object to another container

eg

Defines a container that holds 100 int data, with the initial value assigned to 0

Vector Vecmyhouse (100,0);

Define a new container with the same content as the container above

Vector Myvec;

Myvec = Vecmyhouse;

Second, the above is a simple introduction of the vector container, the following will detail its other features:

1. In order to use the vector, you must include the following code in your header file:

#include

2. Vectors belong to the STD named domain, so you need to pass the name qualification, you can add the file at the beginning

Using Std::vector;

Or

using namespace Std;

or prefix the code directly before using the vector

eg

Std::vector Myhouse;

3. Vectors provide the following functions or actions:

Here's a list of some common features

Define a vector

Std::vector C;

Features that you can use

C.clear () Removes all data from the container.

C.empty () Determines whether the container is empty.

C.erase (POS) Delete data at the POS location

C.erase (beg,end) Delete data from [Beg,end] Interval

C.front () returns the first data.

C.insert (Pos,elem) inserts a elem copy at POS location

C.pop_back () deletes the last data.

C.push_back (elem) adds a data to the tail.

C.resize (num) to reset the size of the container

C.size () returns the number of actual data in the container.

C.begin () returns an iterator that points to the first element of the container

C.end () returns an iterator that points to the last element of the container

Three, the following describes what is an iterator

Iterators are equivalent to pointers, for example:

For variables, use pointers to the corresponding variables

You can then use the * plus pointer to manipulate the variable.

int a = 10;

int *p;

p = &a;

Use the pointer to manipulate the variable

Eg: *p = 11; After operation a becomes 11

for a container, use an iterator to manipulate the value of the corresponding position in the container

When the iterator points to a location in the container, you can use the * iterator to manipulate the position.

Define a vector

Std::vector Myvec;

Add 10 elements

for (Int J =0; j<10; j + +)

{

Myvec.push_back (j);

}

Define an iterator

Std::vector::iterator p;

Point to the first element of the container

p = Myvec.begin ();

Move to the next element

p + +;

Modify the element assignment

*p = 20; < The second value in the Myvec container is modified to 20

Loop Scan iterator, change all values

p = Myvec.begin ();

for (; p!= myvec.end (); p++)

{

*p = 50;

}

The above simply describes the use of vectors, for beginners only, thank you.

-------------------------------------------------------------------------------------

1.vector of data deposit and output:

#include

#include

#include

using namespace Std;

void Main ()

{

int i = 0;

Vector v;

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

{

V.push_back (i);//depositing elements into a vector in one

}

Empty the data deposited

for (i = 0; i < v.size (); i++)//v.size () indicates the number of vectors deposited into the element

{

cout << v[i] << ""; To show each element.

}

Cont << Endl;

}

Note: You can also use V.begin () and V.end () to get the pointer position of the starting and ending element addresses of the vector. You can also do this:

Vector::iterator ITER;

for (iter = V.begin (); ITER! = V.end (); iter++)

{

cout << *iter << Endl;

}

2. The definition of a two-dimensional vector.

1) define a 10 vector element and a value of 1-10 for each vector symbol.

#include

#include

#include

using namespace Std;

void Main ()

{

int i = 0, j = 0;

Defines a two-dimensional dynamic array with 10 rows, each of which is a vector of data stored in this row.

So the length of each line can vary. Vector (0) is used to initialize the vector, otherwise the vector can not be deposited into the element.

vector< vector > Array (vector (0));

for (j = 0; J <; J + +)

{

for (i = 0; i < 9; i++)

{

array[J].push_back (i);

}

}

for (j = 0; J <; J + +)

{

for (i = 0; i < array[J].size (); i++)

{

cout << array[J [i] << "";

}

cout<< Endl;

}

}

2) define an array in which the rows and columns are changed.

#include

#include

#include

using namespace Std;

void Main ()

{

int i = 0, j = 0;

vector< vector > Array;

vector< int > line;

for (j = 0; J <; J + +)

{

Array.push_back (line);//The element cannot be deposited if it is initialized for each vector.

for (i = 0; i < 9; i++)

{

array[J].push_back (i);

}

}

for (j = 0; J <; J + +)

{

for (i = 0; i < array[J].size (); i++)

{

cout << array[J [i] << "";

}

cout<< Endl;

}

}

3> specifying elements using Vettor Erase

#include "iostream"

#include "vector"

using namespace Std;

int main ()

{

Vector arr;

Arr.push_back (6);

Arr.push_back (8);

Arr.push_back (3);

Arr.push_back (8);

For (Vector::iterator It=arr.begin (); It!=arr.end ();)

{

if (* it = = 8)

{

it = arr.erase (it); After deletion, you must reset the value of it

}

Else

{

++it;

}

}

cout << "after remove 8:\n";

for (Vector::iterator it = Arr.begin (); it < Arr.end (); ++it)

{

cout << * it << "";

}

cout << Endl;

}

-----------------------------------------------------------------------------------------

#include
#include
#include
#include
using namespace Std;

void Main ()
{
int iarray[]={0,1,2,3,4,5,6,6,6,7,8};
Vector Ivector (iarray,iarray+sizeof (IArray)/sizeof (int));
int iarray1[]={6,6};
Vector Ivector1 (iarray1,iarray1+sizeof (iarray1)/sizeof (int));
int iarray2[]={5,6};
Vector Ivector2 (iarray2,iarray2+sizeof (iarray2)/sizeof (int));
int iarray3[]={0,1,2,3,4,5,7,7,7,9,7};
Vector Ivector3 (iarray3,iarray3+sizeof (IARRAY3)/sizeof (int));

Find the first element of equal value of adjacent elements in Ivector
Cout<<*adjacent_find (Ivector.begin (), Ivector.end ()) <<endl;< p= "" >

Find out the number of elements with element value 6 in Ivector
Cout<<count (Ivector.begin (), Ivector.end (), 6) <<endl;< p= "" >

Find out the number of elements less than 7 in Ivector
Cout<<count_if (Ivector.begin (), Ivector.end (), bind2nd (Less (), 7)) <<endl;< p= "" >

Find the element where the first element with an element value of 4 is located in the Ivector
Cout<<*find (Ivector.begin (), Ivector.end (), 4) <<endl;< p= "" >

Find the element where the first element of the Ivector is greater than 2
Cout<<*find_if (Ivector.begin (), Ivector.end (), bind2nd (Greater (), 2))
<<endl;

Find the last position in the Ivector sub-sequence Ivector1, and then the elements in the next 3 positions
cout<<* (Find_end (Ivector.begin (), Ivector.end (), Ivector1.begin (),
Ivector1.end ()) +3) <<endl;< p= "" >

Find the first position in the Ivector sub-sequence Ivector1, and then the elements in the next 3 positions
cout<<* (Find_first_of (Ivector.begin (), Ivector.end (), Ivector1.begin (),
Ivector1.end ()) +3) <<endl;

Sub-sequence Ivector2 the starting position element that appears in Ivector
Cout<<*search (Ivector.begin (), Ivector.end (), Ivector2.begin (), Ivector2.end ())
<<endl;< p= "" >

Find start position elements with 3 consecutive 6 occurrences
Cout<<*search_n (Ivector.begin (), Ivector.end (), 3,6,equal_to ()) <<endl;< p= "" >

Judge two intervals ivector and Ivector3 equal No (0 is false, 1 is true)
cout << equal (Ivector.begin (), Ivector.end (), Ivector3.begin ()) << Endl;

Find the position of the interval Ivector3 in Ivector
Pair<int*,int*>result=mismatch (Ivector.begin (), Ivector.end (), Ivector3.begin ());
cout<< result.first-ivector.begin () << Endl;
}

#include
#include
#include
#include
using namespace Std;

Class even_by_two{//A Function object in the form of a definition
Public
int operator () () const
{return _x+=2;}
Private
static int _x;
};
int even_by_two::_x=0; Static data member initialization

void Main ()
{
int iarray[]={0,1,2,3,4,5,6,6,6,7,8};
int iarray1[]={0,1,2,3,4,4,5,5,6,6,6,6,6,7,8};
Vector Ivector (iarray,iarray+sizeof (IArray)/sizeof (int));
Vector Ivector1 (iarray+6,iarray+8);
Vector Ivector2 (iarray1,iarray1+sizeof (iarray1)/sizeof (int));
ostream_iterator< int > output (cout, ""); Define a flow iterator for output data

Iterate through the Ivector1 interval and even_by_two each element
Generate (Ivector1.begin (), Ivector1.end (), Even_by_two ());
Copy (Ivector1.begin (), ivector1.end (), output);
cout<<endl;< p= "" >

Iterates through the specified interval (start and length) of the ivector, even_by_two each element
Generate_n (Ivector.begin (), 3,even_by_two ());
Copy (Ivector.begin (), ivector.end (), output);
cout<<endl;< p= "" >

Delete Element 6
Remove (Ivector.begin (), Ivector.end (), 6);
Copy (Ivector.begin (), ivector.end (), output);
cout<<endl;

Delete (not actually removed from the original sequence) element 6, the result is placed in another interval
Vector Ivector3 (12);
Remove_copy (Ivector.begin (), Ivector.end (), Ivector3.begin (), 6);
Copy (Ivector3.begin (), ivector3.end (), output);
cout<<endl;< p= "" >

Delete (not actually removed from the original sequence) elements less than 6
Remove_if (Ivector.begin (), Ivector.end (), bind2nd (Less (), 6));
Copy (Ivector.begin (), ivector.end (), output);
cout<<endl;

Delete (actually not removed from the original sequence) less than 7 elements, the result is placed in another interval,
Remove_copy_if (Ivector.begin (), Ivector.end (), Ivector3.begin (),
bind2nd (Less (), 7));
Copy (Ivector3.begin (), ivector3.end (), output);
cout<<endl;

Change all element value 6 to element value 3
Replace (Ivector.begin (), Ivector.end (), 6, 3);
Copy (Ivector.begin (), ivector.end (), output);
cout<<endl;

Change all the element value 3 to the element value 5, and the result is placed in another interval
Replace_copy (Ivector.begin (), Ivector.end (), Ivector3.begin (), 3,5);
Copy (Ivector3.begin (), ivector3.end (), output);
cout<<endl;

Change all element values less than 5 to the element value 2
Replace_if (Ivector.begin (), Ivector.end (), bind2nd (Less (), 5), 2);
Copy (Ivector.begin (), ivector.end (), output);
cout<<endl;

Change all the element value 8 to the element value 9, and the result is placed in another interval
Replace_copy_if (Ivector.begin (), Ivector.end (), Ivector3.begin (),
bind2nd (Equal_to (), 8), 9);
Copy (Ivector3.begin (), ivector3.end (), output);
cout<<endl;

Reverse re-rearrangement of each element
Reverse (Ivector.begin (), Ivector.end ());
Copy (Ivector.begin (), ivector.end (), output);
cout<<endl;

Reverse-rearrange each element and result in another interval
Reverse_copy (Ivector.begin (), Ivector.end (), Ivector3.begin ());
Copy (Ivector3.begin (), ivector3.end (), output);
cout<<endl;

Rotation (interchange Element) [First,middle), and [Middle,end]
Rotate (Ivector.begin (), Ivector.begin () +4,ivector.end ());
Copy (Ivector.begin (), ivector.end (), output);
cout<<endl;

Rotate (interchange Element) [First,middle], and [middle,end], the result is placed in another interval,
Rotate_copy (Ivector.begin (), Ivector.begin () +5,ivector.end (),
Ivector3.begin ());
Copy (Ivector3.begin (), ivector3.end (), output);
cout<<endl;
}

#include
#include
#include
#include
using namespace Std;

void Main ()
{
int iarray[]={26,17,15,22,23,33,32,40};
Vector Ivector (iarray,iarray+sizeof (IArray)/sizeof (int));

Find and output maximum, minimum element
Cout<<*max_element (Ivector.begin (), Ivector.end ()) <<endl;
Cout<<*min_element (Ivector.begin (), Ivector.end ()) <<endl;< p= "" >

Sorts the elements of the Ivector.begin () +4-ivector.begin (),
Put in [Ivector.begin (), Ivector.begin () +4] interval. Remaining elements are not guaranteed to maintain the original relative order
Partial_sort (Ivector.begin (), Ivector.begin () +3,ivector.end ());
Copy (Ivector.begin (), Ivector.end (), Ostream_iterator (cout, ""));
cout<<endl;< p= "" >

Local sorting and copying to elsewhere
Vector Ivector1 (5);
Partial_sort_copy (Ivector.begin (), Ivector.end (), Ivector1.begin (),
Ivector1.end ());
Copy (Ivector1.begin (), Ivector1.end (), Ostream_iterator (cout, ""));
cout<<endl;< p= "" >

Sort, which is incremented by default.
Sort (Ivector.begin (), Ivector.end ());
Copy (Ivector.begin (), Ivector.end (), Ostream_iterator (cout, ""));
cout<<endl;< p= "" >

Inserts the specified element into the interval without affecting the lowest, highest position of the interval's original sort
Cout<<*lower_bound (Ivector.begin (), Ivector.end (), <<endl;
Cout<<*upper_bound (Ivector.begin (), Ivector.end (), <<endl;< p= "" >

For ordered intervals, a binary search method can be used to find an element
Cout<<binary_search (Ivector.begin (), Ivector.end (), <<endl;
Cout<<binary_search (Ivector.begin (), Ivector.end (), <<endl;< p= "" >

Next permutation combination
Next_permutation (Ivector.begin (), Ivector.end ());
Copy (Ivector.begin (), Ivector.end (), Ostream_iterator (cout, ""));
cout<<endl;< p= "" >

Previous permutation combination
Prev_permutation (Ivector.begin (), Ivector.end ());
Copy (Ivector.begin (), Ivector.end (), Ostream_iterator (cout, ""));
cout<<endl;< p= "" >

Merges two sequences ivector and Ivector1, and puts the results in Ivector2
Vector Ivector2 (13);
Merge (Ivector.begin (), Ivector.end (), Ivector1.begin (), Ivector1.end (),
Ivector2.begin ());
Copy (Ivector2.begin (), Ivector2.end (), Ostream_iterator (cout, ""));
cout<<endl;< p= "" >

Place elements less than * (Ivector.begin () +5) on the left side of the element
The rest is placed on the right side of the element. Not guaranteed to maintain the relative position of the original
Nth_element (Ivector2.begin (), Ivector2.begin () +5,ivector2.end ());
Copy (Ivector2.begin (), Ivector2.end (), Ostream_iterator (cout, ""));
cout<<endl;< p= "" >

Sort, and keep the original relative position
Stable_sort (Ivector2.begin (), Ivector2.end ());
Copy (Ivector2.begin (), Ivector2.end (), Ostream_iterator (cout, ""));
cout<<endl;< p= "" >

For an ordered interval, find one of the sub-ranges, where each element is the same value as a specific element
Pair<vector::iterator,vector::iterator> Pairite;
Pairite=equal_range (Ivector2.begin (), Ivector2.end (), 22);
cout<<* (Pairite.first) <<endl;
cout<<* (pairite.second) <<endl;< p= "" >

Merges two ordered sequences and replaces them in place
int iarray3[] = {1, 3, 5, 7, 2, 4, 6, 8};
Vector Ivector3 (iarray3,iarray3+sizeof (IARRAY3)/sizeof (int));
Inplace_merge (Ivector3.begin (), Ivector3.begin () + 4, ivector3.end ());
Copy (Ivector3.begin (), Ivector3.end (), Ostream_iterator (cout, ""));
cout<<endl;

Compare sequences Ivector3 and Ivector4 in dictionary order
int iarray4[] = {1, 3, 5, 7, 1, 5, 9, 3};
Vector Ivector4 (iarray4,iarray4+sizeof (IARRAY4)/sizeof (int));
cout<< Lexicographical_compare (Ivector3.begin (), Ivector3.end (),
Ivector4.begin (), Ivector4.end ()) << Endl

}

Related functions commonly used by Vecto container R in C + +

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.