I. Guidelines to vector

1. &: Point to the vector on the stack

(1) Form

Pointing to a vector variable on a stack, variables on the stack are automatically released (so-called local variables) because of the different {} ranges.

Five constructions corresponding to vector s:

https://blog.csdn.net/sandalphon4869/article/details/94589399#1_15

#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<int> k{ 1,2,3 };
//vector<int>* p=&k;
vector<int>* p;
p = &k;

for (int i = 0; i < p->size(); i++)
{
cout << p->at(i) << ' ';
}
//1 1 1

return 0;
}

Wait

(2) Automatic release of local variables

Because what P points to is released out of parentheses and re-pointed to empty, p - > size () = 0

#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<int>* p;
{
vector<int> k{ 1,2,3 };
p = &k;
}

for (int i = 0; i < p->size(); i++)
{
cout << p->at(i) << ' ';
}
//Nothing output

return 0;
}

2.new: points to vector s on the heap

(1) Form

The new way is to allocate data memory on the heap area, unlike the local variables above, when you actively release delete, the data will disappear.

Vector < int > * P = new vector < int > {1,2,3}; means that the pointer points to a new one, so it is written as vector < int > * p; P = new vector < int > {1,2,3}; it is also a meaning.

Three constructions corresponding to vector construction methods.

#include <iostream>
#include <vector>
using namespace std;

int main()
{
//vector<int>* p = new vector<int>{1,2,3};
vector<int>* p;
p = new vector<int>{ 1,2,3 };

for (int i = 0; i < p->size(); i++)
{
cout << p->at(i) << ' ';
}
//1 2 3

return 0;
}
#include <iostream>
#include <vector>
using namespace std;

int main()
{
//vector<int>* p = new vector<int>(3);
vector<int>* p;
p = new vector<int>(3);

for (int i = 0; i < p->size(); i++)
{
cout << p->at(i) << ' ';
}
//0 0 0

return 0;
}
#include <iostream>
#include <vector>
using namespace std;

int main()
{
//vector<int>* p = new vector<int>(3,1);
vector<int>* p;
p = new vector<int>(3,1);

for (int i = 0; i < p->size(); i++)
{
cout << p->at(i) << ' ';
}
//1 1 1

return 0;
}

(2) Data will not disappear unless it is released voluntarily.

#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<int>* p;
{
p = new vector<int>{ 1,2,3 };
}

for (int i = 0; i < p->size(); i++)
{
cout << p->at(i) << ' ';
}
//1 2 3

return 0;
}

2. An array of pointers to vector s

1. initialization

Format: vector < int > * P 

#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<int> a{ 1,2,3 }, b{ 4,5,6 }, c{7,8,9,10};

//Writing 1
vector<int>* p = { &a,&b,&c };

/*
//Writing 2
vector<int>* p;
p = &a, p = &b, p = &c;
*/

for (int i = 0; i < 3; i++)
{
for (int j = 0; j < p[i]->size(); j++)
{
cout << p[i]->at(j) << ' ';
}
cout << endl;
}
/*
1 2 3
4 5 6
7 8 9 10
*/
return 0;
}

2. Pointer Failure

A pointer refers to a piece of memory that holds data, but if that piece of memory is released, the pointer will fail.

(2) Local variables

#include <iostream>
#include <vector>
using namespace std;

int main()
{
vector<int>* p;

//Local variables are released
{
vector<int> a{ 1,2,3 }, b{ 4,5,6 }, c{ 7,8,9,10 };
p = &a, p = &b, p = &c;
}

cout << p->size() << endl;
//0

//cout << p->at(0) << endl;
//error

return 0;
}

Posted on Thu, 10 Oct 2019 08:00:08 -0700 by pikebro2002