C ++ vector is passed by value: did I understand correctly?

Say I have a structure like this:

struct typeA
{
    long first;
    string second
    double third;
};

If I announce

typeA myArray[100];

Then myArray is stored on the stack, consuming sizeof (typeA) * 100 bytes of garbage data (until I save some actual data, at least).

Whenever I pass this array as a parameter, I will always pass a pointer to the first of the first element on the stack. Thus, the pointer moves from stack to stack.

But if I declare

vector<int> myVector (4, 100);

Then the myVector object is actually stored on the stack and contains a pointer to the first element of the array of 4 * sizeof (int) bytes stored in the heap where the actual data is stored. Thus, the pointer jumps from the stack to the heap.

Whenever I pass this vector as a parameter, if I add it to the parameter list as follows:

vector<int> parameterVector

myVector .

:

vector<int> &parameterVector

myVector, , , , myVector, , , .

?

:

  • (, C, ) ?
  • myVector ?
  • , , , , , . ?
  • , , ++ , , ? ( , ).

!

+4
2

(, C, ) ?

some_type* some_name = new some_type[some_size]

myVector ?

, . , .

, , , , - , . ?

. O (N), . , , , , - .

+2
  • (, C, ) ?

std::vector<> , , . , . , . C, .

C-, std::array<>. , a std::array<> . , , .

  1. myVector ?

std::vector , . .

  1. , , , , , . ?

. , , .

  1. , , ++ , , ? ( , ).

" " - C-Legacy. . , , . , , , . , , , .

, ?

C- - . int, , , , , , , , .

, ++ . , . ++ 11 , . , , .

+1

Source: https://habr.com/ru/post/1614748/


All Articles