feelfree
feelfree

Reputation: 11763

std::vector and its initialization pointer consistence

I give the following codes to illustrate my question, and you can them in http://cpp.sh/

// Example program
#include <iostream>
#include <string>
#include <vector>

int main()
{
  int *p;
  p = new int [10];
  for(int i=0; i<10; i++)
      p[i] = i;

  std::vector<int> vecArray(p,p+10);
  vecArray[3]=300;

  for(int i=0; i<10; i++)
      std::cout<<vecArray[i]<<std::endl;

    for(int i=0; i<10; i++)
      std::cout<<p[i]<<std::endl;

  delete []p;
}

From the codes we can see that after the pointer p is used to initialze the vector vecArray then when the content of the vector is changed it will not affact the content in the pointer. My question is: how could it be possible that content of the vctor is always the same with the pointer?

Upvotes: 3

Views: 75

Answers (3)

Nayana Adassuriya
Nayana Adassuriya

Reputation: 24766

This is the constructor of vector that uses a range

template <class InputIterator> vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type());

And this is the description:

Range constructor: Constructs a container with as many elements as the range [first,last), with each element emplace-constructed from its corresponding element in that range, in the same order.

it says emplace-constructed from its corresponding element. So it means it create a new copy of the object pointing by the pointer.

That's why The underlying type an std::vector uses must be CopyAssignable

So as the summery vector create set of copies from the array element. So if you change any of a element from one set, it doesn't reflect in other set.

Upvotes: 1

J&#233;r&#244;me
J&#233;r&#244;me

Reputation: 8066

You can make your vector contain pointer to int.

You push in your vector addresses of your dynamic allocated array.

#include <iostream>
#include <string>
#include <vector>

int main()
{
  int *p;
  p = new int [10];
  for(int i=0; i<10; i++)
      p[i] = i;

  std::vector<int*> vecArray;
  for(int i=0; i<10; i++)
      vecArray.push_back(&p[i]); //push_back addresses in the vector

  p[3]=300; //you can also: *(vecArray[3])=300;

  for(int i=0; i<10; i++)
      std::cout<<*vecArray[i]<<std::endl; // Deference your pointer to get the value

  for(int i=0; i<10; i++)
      std::cout<<p[i]<<std::endl;

  delete []p;
}

Upvotes: 2

Drax
Drax

Reputation: 13288

The content of the vector is a copy of the content of your dynamically allocated array.

You need to understand that your sample code allocates 10 integers TWO times, one time when you explicitly call new and the other one when you construct your vector.

You can have both sharing the same memory by, for example, first building your vector and then getting a pointer to its data :

#include <iostream>
#include <vector>

int     main(void)
{
  std::vector<int> vecArray(10);

  for(int i=0; i<10; i++)
    vecArray[i] = i;

  const int* p = vecArray.data();

  vecArray[3]=300;

  for(int i=0; i<10; i++)
    std::cout<<vecArray[i]<<std::endl;

  for(int i=0; i<10; i++)
    std::cout<<p[i]<<std::endl;
}

Upvotes: 4

Related Questions