Vector Understanding a Wave ~ ~ ~

Source: Internet
Author: User

Vector;

Header file:

#include <vector>

Using NAMESPACESTD;

Defined:

vector< type >q;//similar to "type q[];"

vector< type >q[1010]//similar to "type q[1010][]"

Operation:

To deposit data into a vector:

Name of function: W. Push_back (data);

#include <iostream>

#include <vector>

using namespace Std;

vector<int>w;

int main ()

{

int x;

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

{

scanf ("%d", &x);

W.push_back (x);//Plug the data into the end of the container. This container is very high-end, and you understand it as an array-like method of storage.

}

}

2. Let's get a better understanding of what's inside the vector;

Here are two functions: Q.begin (), Q.end ();

From the English name Begin,end very well understood must be the beginning and the end; = = nonsense.

Well, let's see what he is (data);

#include <stdio.h>

#include <iostream>

#include <vector>

Using NAMESPACESTD;

int main ()

{

vector<int>w;

for (int i=0;i<7;i++)//We'll plug in the I

{

W.push_back (i);

}

printf ("begin=%dend=%d\n", W.begin (), W.end ());

}

Results:

Very bad data--that's the memory. Then End-begin is 7. ~~~~~

We call W.begin (), W.end () as an iterator (t^t something very powerful) iterator is a so-called complex pointer "Here is not much to say what is an iterator, I do not know (ah, shame. "and what are the values that are stored separately?"

#include <stdio.h>

#include <iostream>

#include <vector>

Using NAMESPACESTD;

int main ()

{

vector<int>w;

for (int i=0;i<7;i++)//We'll plug in the I

{

W.push_back (i);

}

printf ("begin=%dend=%d\n", *w.begin (), *w.end ());

}

Results:

We understand very well that w.begin () "points" to the first value-0, but end , not 6? W.end () is the one at the end, equivalent to our definition array a[10], we are querying w.end (), which is equivalent to querying a[10] this element. So that means the last value, which is * (W.end ()-1);

#include <stdio.h>

#include <iostream>

#include <vector>

Using NAMESPACESTD;

int main ()

{

vector<int>w;

for (int i=0;i<7;i++)//We'll plug in the I

{

W.push_back (i);

}

printf ("begin=%d end=%d last is:%d\n", *w.begin (), *w.end (), * (W.end ()-1));

}

Results:


Then there is the traversal:

Here is a function w.size () that refers to the size of the range of values stored by this vector.

#include <stdio.h>

#include <iostream>

#include <vector>

Using NAMESPACESTD;

int main ()

{

vector<int>w;

for (int i=0;i<7;i++)//We'll plug in the I

{

W.push_back (i);

}

printf ("size=%d\n", W.size ());

}

Results:


Well, we can traverse it;

Here is a magical use of the same as the array!!! Why is it? T^t do not know AH;

#include <stdio.h>

#include <iostream>

#include <vector>

Using NAMESPACESTD;

int main ()

{

vector<int>w;

for (int i=0;i<7;i++)//We'll plug in the I

{

W.push_back (i);

}

for (int j=0;j<w.size (); j + +)

{

printf ("%d =%d\n", j+1,w[j]);

}

}

Results:

Another is that we know that W.begin (), W.end () is [iterator] (a complex "pointer"), then we use the same " iterator " to traverse, also can it;

Definition:vector<int>::iterator b;

#include <stdio.h>

#include <iostream>

#include <vector>

Using NAMESPACESTD;

int main ()

{

vector<int>w;

for (int i=0;i<7;i++)//We'll plug in the I

{

W.push_back (i);

}

Vector<int>::iterator b;

int cont=1;

For (B=w.begin (); B!=w.end (); b++)

{

printf ("%d =%d\n", cont++,*b);

}

}

Results:

There is also empty:

W.clear ();

#include <stdio.h>

#include <iostream>

#include <vector>

Using NAMESPACESTD;

int main ()

{

vector<int>w;

for (int i=0;i<7;i++)//We'll plug in the I

{

W.push_back (i);

}

Vector<int>::iterator A;

int cont=1;

For (A=w.begin (); A!=w.end (); a++)

{

printf ("%d =%d\n", cont++,*a);

}

W.clear ();

printf ("begin=%dend=%d\n", W.begin (), W.end ());

if (W.begin () ==w.end ())

printf ("Empty La la la!!! \ n ");

Vector<int>::iterator b;

Cont=1;

For (B=w.begin (); B!=w.end (); b++)

{

printf (" %d =%d\n", cont++,*b);

}

}

Results:


OK, there are a lot of other things to flip ah, very advanced stuff, try to know everything.

Vector Understanding a Wave ~ ~ ~

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.