Reputation: 3074
Is it possible to shallow copy pointer to vector? Here's my code:
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> foo = {"apple", "melon", "kiwi"};
std::string *argv = foo.data();
size_t argc = foo.size();
std::vector<std::string> boo(argv, argv+argc);
}
Does boo
gets shallow copied?
Is it faster to initialize boo
than doing std::vector<std::string> boo(foo);
?
Upvotes: 2
Views: 1370
Reputation: 166
By my understanding, you want vector
boo to contain what is inside foo without actually copying any of the data? in which case you have two options.
In the first instance you can do as eerorika suggested and make boo a reference to foo, essentially the same as a pointer. I don't think this is quite what you were going for though.
std::vector<std::string> & boo = foo;
Alternatively you can use std::move
which will set boo's internal pointer to point at foo's elements and then set foo's to nullptr
. this will put foo in an unsafe state to use however so don't make any calls to it after doing this.
std::vector<std::string> boo(std::move(foo));
Upvotes: 1
Reputation: 746
if you want shallow copy of something you can use std::shared_ptr
. Short example:
#include <iostream>
#include <memory>
#include <string>
#include<vector>
typedef std::vector<std::string> string_vector;
int main() {
std::shared_ptr<string_vector> s_ptr = std::make_shared<string_vector>( string_vector({"a","b"}) );
auto s_ptr_2 = s_ptr;
(*s_ptr_2)[0] = "c";
std::cout<<(*s_ptr)[0]<<" "<<(*s_ptr)[1] <<std::endl;
std::cout<<(*s_ptr_2)[0]<<" "<<(*s_ptr_2)[1] <<std::endl;
}
I advise you to read about shared_ptr
before using it (lifetime, ownership of objects)
Upvotes: 2
Reputation: 238461
std::vector
has unique ownership of its own buffer. The buffer cannot be shared with other vectors. As such, the std::vector
class does not provide a way to "shallow copy", except in case of transferring the ownership to another std::vector
, in which case the source std::vector
will no longer own the transferred buffer. Such transfer of ownership occurs in a move constructor or move assignment operator.
Other container implementations than the standard std::vector
have been written , which do implement "copy on write" semantics, which do not copy elements until a copy is modified.
std::vector<std::string> boo(argv, argv+argc)
copies the elements in the range, as per documentation of vector. It is not faster than copying the vector directly.
It is possible to refer to an object using indirection, such as reference or a (possibly smart) pointer. For example, you could write std::vector<std::string>& boo = foo;
in which case no copying of elements occurs.
When referring to objects indirectly, you must be very careful to ensure that the lifetime of the referred object is at least as long as the reference is used. If the lifetime of the object ends while a reference still exists, that reference is said to be dangling. The behaviour of attempting to access an object through a dangling reference is undefined.
Upvotes: 3