Home > Back-end >  The vector address, ginseng
The vector address, ginseng

Time:01-16

V, a vector array has stored data T

Func1
{
Func2 (& amp; V [0]);//1
//func2 (& amp; [0]) (v);//2
}

Func2 (void * p)
{
T * var=p (T *);//3
T var=* (T *) p;//4
T& Var=* (T *) p;//5
}

An element of the vector in func1 incoming func2, if only one element, just call a function func2, more than 12345 any combination can get data at func2
If
Func1
{
Func2 (& amp; V [0]);//1
Func2 (& amp; V [1]);//1
}
Call func2 twice into two elements in the vector, func2 way 3, 5 only can have the data, the last call way four elements can normal
If
Func1
{
Func2 (& amp; [0]) (v);//2
Func2 (& amp; [1] (v));//2
}
Can normal visit func2 of 4, 5, 3 only the last call to normal access
Fun2 is thread function, at the same time perform

The vector & amp; To remove with a certain element of address what's the problem? Why the second element after the address, the first element of Pointers can't be able to visit
In the two threads func2 variables should be of no effect, so why not use Pointers

CodePudding user response:

correction: method 5 references and probabilistic normal visit , actual or abnormal
Take the address is the vector of elements, only the last take address can be normal access to the element at a time, why

CodePudding user response:

refer to the alias

CodePudding user response:

refer to the second floor 4 teacher zhao response:
reference the alias

Reference only one element in the vector is normal, reference multiple elements, only the last reference element can access, all become invalid references before, with a pointer

CodePudding user response:

Upload can reflect the smallest complete code of the problem

CodePudding user response:

quoted zhao 4, 4/f, the teacher's reply:
upload can reflect the problem of the smallest complete code

 
//variable definition
Vector Cameras.

//the preview thread
UINT WINAPI ThreadPreview (LPVOID param)
{
Param CameraInfo camera=(CameraInfo *);//1
//CameraInfo& Camera=* (CameraInfo * param);//2
.
}

//open the
Void the open ()
{
CameraInfo cmaera;
//camera initialization
//...
Cameras. Pushback (camera);
AfxBeginThread (ThreadPreview, & amp; (cameras [cameras. The size () - 1)));//open thread preview the camera
}


Call open for the first time, the thread mode camera whether a pointer or reference, can be normal access to;
The second call open, open the second camera, other threads within only a second call open threads can access to the camera, a call before opening the thread cannot access the camera, address becomes invalid

CodePudding user response:

Pushback after operation, the original pointer of the vector elements, references, and iterators are invalid,

To switch to the list

CodePudding user response:

refer to 6th floor iamnobody response:
pushback after operation, the original pointer to the vector elements, references, and iterators are invalid,

To switch to a list

See
But the list can't random access
Want to ask, in order to realize random access, add or delete the original element address remains the same, what is the container to do, or do you have any other way