Example of vector in C ++

Source: Internet
Author: User

Example of vector in C ++

I. Overview

Vector is part of the C ++ standard template library. It is a versatile template class and function library that can operate on multiple data structures and algorithms. Vector is a container that can store various types of objects. Simply put, vector is a dynamic array that can store any type and dynamically change the size.

For example:

// C Language Style

Int myHouse [100];

// Use vector

Vector <int> vecMyHouse (100 );

After the preceding definition, vecMyHouse can store 100 int-type data.

1. It can be accessed like a normal Array

Eg: vecMyHouse [50] = 1024;

2. You can sequentially populate the container with data

Eg: int I = 0;

For (; I <25; I ++)

{

VecMyHouse. push_back (1 );

}

3. It can also dynamically change its size. It can be implemented through the following statement:

// Change the container size to 400 so that the container can accommodate 400 int-type data.

Eg: vecMyHouse. resize (400 );

4. You can also add custom data types to the container.

Eg:

// Customize a class

Class Cmyclass

{

};

// Define a container for storing class

Vector <Cmyclass> vecMyHouse;

5. You can assign an initial value to the container when defining the container.

// Define a container that contains 100 int data. The initial value is 0.

Vector <int> vecMyHouse (100,0 );

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

Eg:

// Define a container that contains 100 int data. The initial value is 0.

Vector <int> vecMyHouse (100,0 );

// Define a new container with the same content as the preceding container

Vector <int> myVec;

MyVec = vecMyHouse;

2. The above is a brief introduction to the vector container. The following describes its other functions in detail:

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

# Include <vector>

2. The vector belongs to the std naming domain. Therefore, you must use the naming conventions to add

Using std: vector;

Or

Using namespace std;

Or add the prefix directly before the code that uses the vector.

Eg:

Std: vector <int> myHouse;

3. vector provides the following functions or operations:

Some common functions are listed below

// Define a vector

Std: vector <int> c;

// Available features

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

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

C. erase (pos) deletes data at the pos position

C. erase (beg, end) deletes data in the [beg, end) interval.

C. front () returns the first data.

C. insert (pos, elem) insert an elem copy at the pos position

C. pop_back () Delete the last data.

C. push_back (elem) adds a data at the end.

C. resize (num) reset the container size

C. size () refers to the number of actual data returned to the container.

C. begin () returns the iterator pointing to the first element of the container.

C. end () returns the iterator pointing to the last element of the container.

Iii. What is an iterator?

The iterator is equivalent to a pointer, for example:

// For a variable, use a pointer to point to the corresponding variable

// You can use * to add a pointer to operate the variable later.

Int a = 10;

Int * p;

P = &;

// Use a pointer to operate the variable

Eg: * p = 11; // after the operation, a changes to 11

// For containers, use the iterator to operate the value at the corresponding position in the container

// When the iterator points to a position in the container, you can use the * iterator to operate the position.

// Define a vector

Std: vector <int> myVec;

// Add 10 elements

For (int j = 0; j <10; j ++)

{


MyVec. push_back (j );

}

// Define an iterator

Std: vector <int >:: iterator p;

// Point to the first element of the container

P = myVec. begin ();

// Move to the next element

P ++;

// Modify the value of this element

* P = 20; // <The second value in the myVec container is changed to 20

// Cyclically scan iterator to change all values

P = myVec. begin ();

For (; p! = MyVec. end (); p ++)

{

* P = 50;

}

The usage of vector is briefly described above. It is only for beginners. Thank you.

Bytes -------------------------------------------------------------------------------------

1. Storage and output of vector Data:

# Include <stdio. h>

# Include <vector>

# Include <iostream>

Using namespace std;

Void main ()

{

Int I = 0;

Vector <int> v;

For (I = 0; I <10; I ++)

{

V. push_back (I); // store the elements one by one into the vector.

}

Empty the stored data

For (I = 0; I <v. size (); I ++) // v. size () indicates the number of elements stored in a vector.

{

Cout <v [I] <"; // display each element

}

Cont <endl;

}

Note: You can also use v. begin () and v. end () to obtain the pointer positions of the starting and ending element addresses of a vector. You can also do this:

Vector <int>: iterator iter;

For (iter = v. begin (); iter! = V. end (); iter ++)

{

Cout <* iter <endl;

}

2. Two-dimensional vector definition.

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

# Include <stdio. h>

# Include <vector>

# Include <iostream>

Using namespace std;

Void main ()

{

Int I = 0, j = 0;

// Define a two-dimensional dynamic array with 10 rows. Each row uses a vector to store the data of this row.

Therefore, the length of each row can be changed. Vector <int> (0) is used to initialize the vector. Otherwise, elements cannot be stored in the vector.

Vector <int> Array (10, vector <int> (0 ));

For (j = 0; j <10; j ++)

{

For (I = 0; I <9; I ++)

{

Array [j]. push_back (I );

}

}

For (j = 0; j <10; j ++)

{

For (I = 0; I <Array [j]. size (); I ++)

{

Cout <Array [j] [I] <"";

}

Cout <endl;

}

}

2) define a row and column as an array of changes.

# Include <stdio. h>

# Include <vector>

# Include <iostream>

Using namespace std;

Void main ()

{

Int I = 0, j = 0;

Vector <int> Array;

Vector <int> line;

For (j = 0; j <10; j ++)

{

Array. push_back (line); // You Need to initialize each vector. Otherwise, elements cannot be saved.

For (I = 0; I <9; I ++)

{

Array [j]. push_back (I );

}

}

For (j = 0; j <10; j ++)

{

For (I = 0; I <Array [j]. size (); I ++)

{

Cout <Array [j] [I] <"";

}

Cout <endl;

}

}

 

Specify an element using vettor erase

# Include "iostream"

# Include "vector"

Using namespace std;

Int main ()

{

Vector <int> arr;

Arr. push_back (6 );

Arr. push_back (8 );

Arr. push_back (3 );

Arr. push_back (8 );

For (vector <int >:: iterator it = arr. begin (); it! = Arr. end ();)

{

If (* it = 8)

{

It = arr. erase (it );

}

Else

{

++ It;

}

}

Cout <"After remove 8: \ n ";

For (vector <int >:: iterator it = arr. begin (); it <arr. end (); ++ it)

{

Cout <* it <"";

}

Cout <endl;

}

Author: "thousands of miles"

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.