Example of vector usage in C + + analysis of _c language

Source: Internet
Author: User

This example shows the vector usage in C + + and shares it for everyone's reference. Specifically as follows:

I. Overview

Vector is part of the C + + Standard Template Library, a versatile template class and function library that can manipulate a variety of data structures and algorithms. Vector is a container that can hold various types of objects, simply put, a vector is a dynamic array of any type that can be stored in a dynamic, dynamically varying size.
For example:

C language style
int myhouse[100];
Using vector
vector<int> vecmyhouse (100);

When defined, Vecmyhouse can store 100 int data.

1. It can be accessed like an ordinary array
For example:

VECMYHOUSE[50] = 1024;

2. You can fill the container with data sequentially
For example:

int i =0;
for (;i< i++)
{
vecmyhouse.push_back (1);
}

3. It can also dynamically change its size, using the following statement to implement the
Change the container size to 400 so that the container can hold 400 int data.
For example:

Vecmyhouse.resize (400);

4. You can also mount a custom data type in a container
For example:

Customize a class
class CMyClass
{
};
//define a container that holds class
vector<cmyclass> vecmyhouse;

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

Defines a container containing 100 int data, which is assigned the initial value of 0
vector<int> vecmyhouse (100,0);

6. You can assign the object of a container to another container
For example:

Defines a container containing 100 int data, which is assigned the initial value of 0
vector<int> vecmyhouse (100,0);
Define a new container, the contents of which are as vector<int> Myvec as the containers above
;
Myvec = Vecmyhouse;

The above is a brief introduction to the vector container, which will describe its other features in detail below:

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

#include <vector>

2. Vectors belong to the STD-named domain, so you need a naming qualification that you can add at the beginning of the file

Using Std::vector;

Or

using namespace Std;

or prefix it directly with the vector code.
For example:

Std::vector<int> Myhouse;

3. Vectors provide the following functions or operations:
Some of the most common features are listed below

Define a vector
std::vector<int> C;

Features you can use:
C.clear () Removes all data from the container.
C.empty () Determines whether the container is empty.
C.erase (POS) deletes data at POS locations
C.erase (beg,end) deletes data from the [beg,end) interval
C.front () returns the first data.
C.insert (Pos,elem) inserts a elem copy at the POS position
C.pop_back () deletes the last data.
C.push_back (elem) adds a data to the tail.
C.resize (num) Reset the size of the container
C.size () Back to 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

An iterator is equivalent to a pointer, for example:

For a variable, using the pointer to the corresponding variable
//later, you can manipulate the variable with the * plus pointer.
int a = ten;
int *p;
p = &a;

Manipulating the variable with a pointer
For example: *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 * plus iterator to manipulate the location.

Define a vector
std::vector<int> Myvec;
Add 10 elements for
(int J =0; j<10 J + +)
{
myvec.push_back (j);
}

Defines 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 element assignment
*p =//< The second value in the Myvec container is modified for the
//loop scan iterator to change all values
p = myvec.begin ();
for (; p!= myvec.end (); p++)
{
*p = M

The above is a simple description of vector usage, just for beginners, and continues to be explained in an instance form below.

1.vector of data stored and exported:

#include <stdio.h>
#include <vector>
#include <iostream>
using namespace std;
void Main ()
{
 int i = 0;
  Vector<int> v;
  for (i = 0; i < i++)
 {
      v.push_back (i);//Save the element one by one into vector
 }
  //To empty the stored data for
 (i = 0; i ; V.size (); i++)//v.size () indicates the number of vectors stored in the element
 {
     cout << v[i] << ""; Show each element
 }
 cont << Endl;
}

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

You can also do this:

Vector<int>::iterator iter;
for (iter = V.begin (); Iter!= v.end (); iter++)
{
  cout << *iter << Endl;
}

2. Definition of a two-dimensional vector.
1 defines a 10 vector element and a value of 1-10 for each vector character.

#include <stdio.h>
#include <vector>
#include <iostream>
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 that stores this row of data.
//So the length of each row can be changed. Vector<int> (0) is used to initialize vectors, otherwise the vectors cannot be stored in the element.
vector< vector<int> > Array (vector<int> (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 << arra y[J [i] << "";
 }
 cout<< Endl
}
}

2 defines an array in which the rows and columns are variable.

#include <stdio.h>
#include <vector>
#include <iostream>
using namespace std;
void Main ()
{
int i = 0, j = 0;
vector< vector<int> > Array;
vector< int > line;
for (j = 0; J <; J + +)
{
 array.push_back (line);//The element cannot be saved to initialize 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
}
}
 

Use the Vettor erase to specify the elements, as shown in the following example:

#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;
}

I hope the examples mentioned in this article will help us to master the vector usage in C + +.

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.