Why/when should I use std::unique/shared_ptr (std::vector) over just std::vector?What is a smart pointer and when should I use one?Concatenating two std::vectorsWhen should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?How to find out if an item is present in a std::vector?Why is “using namespace std” considered bad practice?What is std::move(), and when should it be used?Why should C++ programmers minimize use of 'new'?Are the days of passing const std::string & as a parameter over?Why is my program slow when looping over exactly 8192 elements?Why should I use a pointer rather than the object itself?
What Exploit Are These User Agents Trying to Use?
Was the Stack Exchange "Happy April Fools" page fitting with the '90's code?
What is the fastest integer factorization to break RSA?
Is it possible to create a QR code using text?
Placement of More Information/Help Icon button for Radio Buttons
Notepad++ delete until colon for every line with replace all
How to install cross-compiler on Ubuntu 18.04?
Why are UK visa biometrics appointments suspended at USCIS Application Support Centers?
If a warlock makes a Dancing Sword their pact weapon, is there a way to prevent it from disappearing if it's farther away for more than a minute?
Mathematica command that allows it to read my intentions
Am I breaking OOP practice with this architecture?
Why didn't Boeing produce its own regional jet?
My ex-girlfriend uses my Apple ID to log in to her iPad. Do I have to give her my Apple ID password to reset it?
What do you call someone who asks many questions?
Using "tail" to follow a file without displaying the most recent lines
Can compressed videos be decoded back to their uncompresed original format?
Rotate ASCII Art by 45 Degrees
Can a virus destroy the BIOS of a modern computer?
how do we prove that a sum of two periods is still a period?
What's the meaning of "Sollensaussagen"?
What historical events would have to change in order to make 19th century "steampunk" technology possible?
What reasons are there for a Capitalist to oppose a 100% inheritance tax?
Ambiguity in the definition of entropy
How do conventional missiles fly?
Why/when should I use std::unique/shared_ptr (std::vector) over just std::vector?
What is a smart pointer and when should I use one?Concatenating two std::vectorsWhen should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?How to find out if an item is present in a std::vector?Why is “using namespace std” considered bad practice?What is std::move(), and when should it be used?Why should C++ programmers minimize use of 'new'?Are the days of passing const std::string & as a parameter over?Why is my program slow when looping over exactly 8192 elements?Why should I use a pointer rather than the object itself?
I'm a little bit confused about the main use of std::unique/shared_ptr(std::vector<>)
when I can simply use a std::vector<>
, which, as I know, is itself inherently a dynamic array. As I have also seen around, people say that there is no any performance difference between these two. So, based on all this, what is the point of using a smart pointer pointing to a container (in this case, a vector) instead of a vector alone?
c++ c++11
|
show 4 more comments
I'm a little bit confused about the main use of std::unique/shared_ptr(std::vector<>)
when I can simply use a std::vector<>
, which, as I know, is itself inherently a dynamic array. As I have also seen around, people say that there is no any performance difference between these two. So, based on all this, what is the point of using a smart pointer pointing to a container (in this case, a vector) instead of a vector alone?
c++ c++11
5
Do you need to shared ownership of the vector ?
– Jarod42
Mar 7 at 21:34
1
people say that there is no any performance difference between these two Whoever told you that lied, or didn't know what they were talking about.std::shared_ptr
is expensive and should only be used is you really need shared ownership. What is your use case for your vector?
– NathanOliver
Mar 7 at 21:36
Which context? What is 'TYPE'?
– SergeyA
Mar 7 at 21:40
whereasstd::shared_ptr<std::vector<T>>
might make sense, I don't see the point ofstd::unique_ptr<std::vector<T>>
.
– Jarod42
Mar 7 at 21:40
Thank you. No, my problem is not sharing the object (that's why I just added the unique). I just wanted to know when we can have a dynamically allocated vector/array, why should we use smart pointers whose, as I know, main application is dynamic allocation?
– Federico
Mar 7 at 21:41
|
show 4 more comments
I'm a little bit confused about the main use of std::unique/shared_ptr(std::vector<>)
when I can simply use a std::vector<>
, which, as I know, is itself inherently a dynamic array. As I have also seen around, people say that there is no any performance difference between these two. So, based on all this, what is the point of using a smart pointer pointing to a container (in this case, a vector) instead of a vector alone?
c++ c++11
I'm a little bit confused about the main use of std::unique/shared_ptr(std::vector<>)
when I can simply use a std::vector<>
, which, as I know, is itself inherently a dynamic array. As I have also seen around, people say that there is no any performance difference between these two. So, based on all this, what is the point of using a smart pointer pointing to a container (in this case, a vector) instead of a vector alone?
c++ c++11
c++ c++11
edited Mar 7 at 21:42
Federico
asked Mar 7 at 21:33
FedericoFederico
47110
47110
5
Do you need to shared ownership of the vector ?
– Jarod42
Mar 7 at 21:34
1
people say that there is no any performance difference between these two Whoever told you that lied, or didn't know what they were talking about.std::shared_ptr
is expensive and should only be used is you really need shared ownership. What is your use case for your vector?
– NathanOliver
Mar 7 at 21:36
Which context? What is 'TYPE'?
– SergeyA
Mar 7 at 21:40
whereasstd::shared_ptr<std::vector<T>>
might make sense, I don't see the point ofstd::unique_ptr<std::vector<T>>
.
– Jarod42
Mar 7 at 21:40
Thank you. No, my problem is not sharing the object (that's why I just added the unique). I just wanted to know when we can have a dynamically allocated vector/array, why should we use smart pointers whose, as I know, main application is dynamic allocation?
– Federico
Mar 7 at 21:41
|
show 4 more comments
5
Do you need to shared ownership of the vector ?
– Jarod42
Mar 7 at 21:34
1
people say that there is no any performance difference between these two Whoever told you that lied, or didn't know what they were talking about.std::shared_ptr
is expensive and should only be used is you really need shared ownership. What is your use case for your vector?
– NathanOliver
Mar 7 at 21:36
Which context? What is 'TYPE'?
– SergeyA
Mar 7 at 21:40
whereasstd::shared_ptr<std::vector<T>>
might make sense, I don't see the point ofstd::unique_ptr<std::vector<T>>
.
– Jarod42
Mar 7 at 21:40
Thank you. No, my problem is not sharing the object (that's why I just added the unique). I just wanted to know when we can have a dynamically allocated vector/array, why should we use smart pointers whose, as I know, main application is dynamic allocation?
– Federico
Mar 7 at 21:41
5
5
Do you need to shared ownership of the vector ?
– Jarod42
Mar 7 at 21:34
Do you need to shared ownership of the vector ?
– Jarod42
Mar 7 at 21:34
1
1
people say that there is no any performance difference between these two Whoever told you that lied, or didn't know what they were talking about.
std::shared_ptr
is expensive and should only be used is you really need shared ownership. What is your use case for your vector?– NathanOliver
Mar 7 at 21:36
people say that there is no any performance difference between these two Whoever told you that lied, or didn't know what they were talking about.
std::shared_ptr
is expensive and should only be used is you really need shared ownership. What is your use case for your vector?– NathanOliver
Mar 7 at 21:36
Which context? What is 'TYPE'?
– SergeyA
Mar 7 at 21:40
Which context? What is 'TYPE'?
– SergeyA
Mar 7 at 21:40
whereas
std::shared_ptr<std::vector<T>>
might make sense, I don't see the point of std::unique_ptr<std::vector<T>>
.– Jarod42
Mar 7 at 21:40
whereas
std::shared_ptr<std::vector<T>>
might make sense, I don't see the point of std::unique_ptr<std::vector<T>>
.– Jarod42
Mar 7 at 21:40
Thank you. No, my problem is not sharing the object (that's why I just added the unique). I just wanted to know when we can have a dynamically allocated vector/array, why should we use smart pointers whose, as I know, main application is dynamic allocation?
– Federico
Mar 7 at 21:41
Thank you. No, my problem is not sharing the object (that's why I just added the unique). I just wanted to know when we can have a dynamically allocated vector/array, why should we use smart pointers whose, as I know, main application is dynamic allocation?
– Federico
Mar 7 at 21:41
|
show 4 more comments
1 Answer
1
active
oldest
votes
First of all, you shouldn't be using std::shared_ptr
unless you need the specific "shared ownership" semantics associated with std::shared_ptr
. If you need a smart pointer, you should default to std::unique_ptr
by default, and only switch away from it in the scenario where you expressly find that you need to.
Secondly: ostensibly, the reason to prefer std::unique_ptr<TYPE>
over TYPE
is if you plan to move the object around a lot. This is the common design paradigm for large objects that are either unmovable, or otherwise expensive to move—i.e. they implemented a Copy Constructor and didn't implement a Move Constructor, so moves are forced to behave like a Copy.
std::vector
, however, does have relatively efficient move semantics: if you move a std::vector
around, regardless of how complex its contained types are, the move only constitutes a couple of pointer swaps. There's no real risk that moving a std::vector
will incur a large amount of computational complexity. Even in the scenario where you're overriding a previously allocated array (invoking the Destructors of all objects in the vector), you'd still have that complexity if you were using std::unique_ptr<std::vector<TYPE>>
instead, saving you nothing.
There are two advantages to std::unique_ptr<std::vector<TYPE>>
. The first of which is that it gets rid of the implicit copy constructor; maybe you want to enforce to maintaining programmers that the object shouldn't be copied. But that's a pretty niche use. The other advantage is that it allows you to stipulate the scenario where there's no vector, i.e. vec.size() == 0
is a different condition than doesNotExist(vec)
. But even in that scenario, you should be preferring std::optional<std::vector>
instead, which better conveys through the code the intent of the object. Granted, std::optional
is only available in C++17→ Code, so maybe you're in an environment that hasn't implemented it yet. But otherwise, there's little reason to use std::unique_ptr<std::vector>
.
So in general, I don't believe there are practical uses for std::unique_ptr<std::vector>
. There's no practical performance difference between it and std::vector
, and using it will just make your code needlessly complex.
Yeah, there is no need to convey optionality with unique_ptr. There are plenty of C++14 conforming implementations of optional, so one can just use one of those.
– SergeyA
Mar 7 at 21:53
Examine what happens when you have a throwing move ctor/assign in the type stored in the vector..
– Yakk - Adam Nevraumont
Mar 7 at 23:02
@Yakk-AdamNevraumont You're going to have to elaborate on what you mean, because from context, it's not clear how this is supposed to improve my answer.
– Xirema
Mar 8 at 0:31
@Xirema What happens tostd::vector<TypeThatCanThrowOnAMove>
when you try to move-assign it? What happens tostd::unique_ptr<std::vector<TypeThatCanThrowOnAMove>>
? It is a corner case, but a difference.
– Yakk - Adam Nevraumont
Mar 8 at 0:44
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55053129%2fwhy-when-should-i-use-stdunique-shared-ptr-stdvector-over-just-stdvect%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
First of all, you shouldn't be using std::shared_ptr
unless you need the specific "shared ownership" semantics associated with std::shared_ptr
. If you need a smart pointer, you should default to std::unique_ptr
by default, and only switch away from it in the scenario where you expressly find that you need to.
Secondly: ostensibly, the reason to prefer std::unique_ptr<TYPE>
over TYPE
is if you plan to move the object around a lot. This is the common design paradigm for large objects that are either unmovable, or otherwise expensive to move—i.e. they implemented a Copy Constructor and didn't implement a Move Constructor, so moves are forced to behave like a Copy.
std::vector
, however, does have relatively efficient move semantics: if you move a std::vector
around, regardless of how complex its contained types are, the move only constitutes a couple of pointer swaps. There's no real risk that moving a std::vector
will incur a large amount of computational complexity. Even in the scenario where you're overriding a previously allocated array (invoking the Destructors of all objects in the vector), you'd still have that complexity if you were using std::unique_ptr<std::vector<TYPE>>
instead, saving you nothing.
There are two advantages to std::unique_ptr<std::vector<TYPE>>
. The first of which is that it gets rid of the implicit copy constructor; maybe you want to enforce to maintaining programmers that the object shouldn't be copied. But that's a pretty niche use. The other advantage is that it allows you to stipulate the scenario where there's no vector, i.e. vec.size() == 0
is a different condition than doesNotExist(vec)
. But even in that scenario, you should be preferring std::optional<std::vector>
instead, which better conveys through the code the intent of the object. Granted, std::optional
is only available in C++17→ Code, so maybe you're in an environment that hasn't implemented it yet. But otherwise, there's little reason to use std::unique_ptr<std::vector>
.
So in general, I don't believe there are practical uses for std::unique_ptr<std::vector>
. There's no practical performance difference between it and std::vector
, and using it will just make your code needlessly complex.
Yeah, there is no need to convey optionality with unique_ptr. There are plenty of C++14 conforming implementations of optional, so one can just use one of those.
– SergeyA
Mar 7 at 21:53
Examine what happens when you have a throwing move ctor/assign in the type stored in the vector..
– Yakk - Adam Nevraumont
Mar 7 at 23:02
@Yakk-AdamNevraumont You're going to have to elaborate on what you mean, because from context, it's not clear how this is supposed to improve my answer.
– Xirema
Mar 8 at 0:31
@Xirema What happens tostd::vector<TypeThatCanThrowOnAMove>
when you try to move-assign it? What happens tostd::unique_ptr<std::vector<TypeThatCanThrowOnAMove>>
? It is a corner case, but a difference.
– Yakk - Adam Nevraumont
Mar 8 at 0:44
add a comment |
First of all, you shouldn't be using std::shared_ptr
unless you need the specific "shared ownership" semantics associated with std::shared_ptr
. If you need a smart pointer, you should default to std::unique_ptr
by default, and only switch away from it in the scenario where you expressly find that you need to.
Secondly: ostensibly, the reason to prefer std::unique_ptr<TYPE>
over TYPE
is if you plan to move the object around a lot. This is the common design paradigm for large objects that are either unmovable, or otherwise expensive to move—i.e. they implemented a Copy Constructor and didn't implement a Move Constructor, so moves are forced to behave like a Copy.
std::vector
, however, does have relatively efficient move semantics: if you move a std::vector
around, regardless of how complex its contained types are, the move only constitutes a couple of pointer swaps. There's no real risk that moving a std::vector
will incur a large amount of computational complexity. Even in the scenario where you're overriding a previously allocated array (invoking the Destructors of all objects in the vector), you'd still have that complexity if you were using std::unique_ptr<std::vector<TYPE>>
instead, saving you nothing.
There are two advantages to std::unique_ptr<std::vector<TYPE>>
. The first of which is that it gets rid of the implicit copy constructor; maybe you want to enforce to maintaining programmers that the object shouldn't be copied. But that's a pretty niche use. The other advantage is that it allows you to stipulate the scenario where there's no vector, i.e. vec.size() == 0
is a different condition than doesNotExist(vec)
. But even in that scenario, you should be preferring std::optional<std::vector>
instead, which better conveys through the code the intent of the object. Granted, std::optional
is only available in C++17→ Code, so maybe you're in an environment that hasn't implemented it yet. But otherwise, there's little reason to use std::unique_ptr<std::vector>
.
So in general, I don't believe there are practical uses for std::unique_ptr<std::vector>
. There's no practical performance difference between it and std::vector
, and using it will just make your code needlessly complex.
Yeah, there is no need to convey optionality with unique_ptr. There are plenty of C++14 conforming implementations of optional, so one can just use one of those.
– SergeyA
Mar 7 at 21:53
Examine what happens when you have a throwing move ctor/assign in the type stored in the vector..
– Yakk - Adam Nevraumont
Mar 7 at 23:02
@Yakk-AdamNevraumont You're going to have to elaborate on what you mean, because from context, it's not clear how this is supposed to improve my answer.
– Xirema
Mar 8 at 0:31
@Xirema What happens tostd::vector<TypeThatCanThrowOnAMove>
when you try to move-assign it? What happens tostd::unique_ptr<std::vector<TypeThatCanThrowOnAMove>>
? It is a corner case, but a difference.
– Yakk - Adam Nevraumont
Mar 8 at 0:44
add a comment |
First of all, you shouldn't be using std::shared_ptr
unless you need the specific "shared ownership" semantics associated with std::shared_ptr
. If you need a smart pointer, you should default to std::unique_ptr
by default, and only switch away from it in the scenario where you expressly find that you need to.
Secondly: ostensibly, the reason to prefer std::unique_ptr<TYPE>
over TYPE
is if you plan to move the object around a lot. This is the common design paradigm for large objects that are either unmovable, or otherwise expensive to move—i.e. they implemented a Copy Constructor and didn't implement a Move Constructor, so moves are forced to behave like a Copy.
std::vector
, however, does have relatively efficient move semantics: if you move a std::vector
around, regardless of how complex its contained types are, the move only constitutes a couple of pointer swaps. There's no real risk that moving a std::vector
will incur a large amount of computational complexity. Even in the scenario where you're overriding a previously allocated array (invoking the Destructors of all objects in the vector), you'd still have that complexity if you were using std::unique_ptr<std::vector<TYPE>>
instead, saving you nothing.
There are two advantages to std::unique_ptr<std::vector<TYPE>>
. The first of which is that it gets rid of the implicit copy constructor; maybe you want to enforce to maintaining programmers that the object shouldn't be copied. But that's a pretty niche use. The other advantage is that it allows you to stipulate the scenario where there's no vector, i.e. vec.size() == 0
is a different condition than doesNotExist(vec)
. But even in that scenario, you should be preferring std::optional<std::vector>
instead, which better conveys through the code the intent of the object. Granted, std::optional
is only available in C++17→ Code, so maybe you're in an environment that hasn't implemented it yet. But otherwise, there's little reason to use std::unique_ptr<std::vector>
.
So in general, I don't believe there are practical uses for std::unique_ptr<std::vector>
. There's no practical performance difference between it and std::vector
, and using it will just make your code needlessly complex.
First of all, you shouldn't be using std::shared_ptr
unless you need the specific "shared ownership" semantics associated with std::shared_ptr
. If you need a smart pointer, you should default to std::unique_ptr
by default, and only switch away from it in the scenario where you expressly find that you need to.
Secondly: ostensibly, the reason to prefer std::unique_ptr<TYPE>
over TYPE
is if you plan to move the object around a lot. This is the common design paradigm for large objects that are either unmovable, or otherwise expensive to move—i.e. they implemented a Copy Constructor and didn't implement a Move Constructor, so moves are forced to behave like a Copy.
std::vector
, however, does have relatively efficient move semantics: if you move a std::vector
around, regardless of how complex its contained types are, the move only constitutes a couple of pointer swaps. There's no real risk that moving a std::vector
will incur a large amount of computational complexity. Even in the scenario where you're overriding a previously allocated array (invoking the Destructors of all objects in the vector), you'd still have that complexity if you were using std::unique_ptr<std::vector<TYPE>>
instead, saving you nothing.
There are two advantages to std::unique_ptr<std::vector<TYPE>>
. The first of which is that it gets rid of the implicit copy constructor; maybe you want to enforce to maintaining programmers that the object shouldn't be copied. But that's a pretty niche use. The other advantage is that it allows you to stipulate the scenario where there's no vector, i.e. vec.size() == 0
is a different condition than doesNotExist(vec)
. But even in that scenario, you should be preferring std::optional<std::vector>
instead, which better conveys through the code the intent of the object. Granted, std::optional
is only available in C++17→ Code, so maybe you're in an environment that hasn't implemented it yet. But otherwise, there's little reason to use std::unique_ptr<std::vector>
.
So in general, I don't believe there are practical uses for std::unique_ptr<std::vector>
. There's no practical performance difference between it and std::vector
, and using it will just make your code needlessly complex.
answered Mar 7 at 21:47
XiremaXirema
14.2k11649
14.2k11649
Yeah, there is no need to convey optionality with unique_ptr. There are plenty of C++14 conforming implementations of optional, so one can just use one of those.
– SergeyA
Mar 7 at 21:53
Examine what happens when you have a throwing move ctor/assign in the type stored in the vector..
– Yakk - Adam Nevraumont
Mar 7 at 23:02
@Yakk-AdamNevraumont You're going to have to elaborate on what you mean, because from context, it's not clear how this is supposed to improve my answer.
– Xirema
Mar 8 at 0:31
@Xirema What happens tostd::vector<TypeThatCanThrowOnAMove>
when you try to move-assign it? What happens tostd::unique_ptr<std::vector<TypeThatCanThrowOnAMove>>
? It is a corner case, but a difference.
– Yakk - Adam Nevraumont
Mar 8 at 0:44
add a comment |
Yeah, there is no need to convey optionality with unique_ptr. There are plenty of C++14 conforming implementations of optional, so one can just use one of those.
– SergeyA
Mar 7 at 21:53
Examine what happens when you have a throwing move ctor/assign in the type stored in the vector..
– Yakk - Adam Nevraumont
Mar 7 at 23:02
@Yakk-AdamNevraumont You're going to have to elaborate on what you mean, because from context, it's not clear how this is supposed to improve my answer.
– Xirema
Mar 8 at 0:31
@Xirema What happens tostd::vector<TypeThatCanThrowOnAMove>
when you try to move-assign it? What happens tostd::unique_ptr<std::vector<TypeThatCanThrowOnAMove>>
? It is a corner case, but a difference.
– Yakk - Adam Nevraumont
Mar 8 at 0:44
Yeah, there is no need to convey optionality with unique_ptr. There are plenty of C++14 conforming implementations of optional, so one can just use one of those.
– SergeyA
Mar 7 at 21:53
Yeah, there is no need to convey optionality with unique_ptr. There are plenty of C++14 conforming implementations of optional, so one can just use one of those.
– SergeyA
Mar 7 at 21:53
Examine what happens when you have a throwing move ctor/assign in the type stored in the vector..
– Yakk - Adam Nevraumont
Mar 7 at 23:02
Examine what happens when you have a throwing move ctor/assign in the type stored in the vector..
– Yakk - Adam Nevraumont
Mar 7 at 23:02
@Yakk-AdamNevraumont You're going to have to elaborate on what you mean, because from context, it's not clear how this is supposed to improve my answer.
– Xirema
Mar 8 at 0:31
@Yakk-AdamNevraumont You're going to have to elaborate on what you mean, because from context, it's not clear how this is supposed to improve my answer.
– Xirema
Mar 8 at 0:31
@Xirema What happens to
std::vector<TypeThatCanThrowOnAMove>
when you try to move-assign it? What happens to std::unique_ptr<std::vector<TypeThatCanThrowOnAMove>>
? It is a corner case, but a difference.– Yakk - Adam Nevraumont
Mar 8 at 0:44
@Xirema What happens to
std::vector<TypeThatCanThrowOnAMove>
when you try to move-assign it? What happens to std::unique_ptr<std::vector<TypeThatCanThrowOnAMove>>
? It is a corner case, but a difference.– Yakk - Adam Nevraumont
Mar 8 at 0:44
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55053129%2fwhy-when-should-i-use-stdunique-shared-ptr-stdvector-over-just-stdvect%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
5
Do you need to shared ownership of the vector ?
– Jarod42
Mar 7 at 21:34
1
people say that there is no any performance difference between these two Whoever told you that lied, or didn't know what they were talking about.
std::shared_ptr
is expensive and should only be used is you really need shared ownership. What is your use case for your vector?– NathanOliver
Mar 7 at 21:36
Which context? What is 'TYPE'?
– SergeyA
Mar 7 at 21:40
whereas
std::shared_ptr<std::vector<T>>
might make sense, I don't see the point ofstd::unique_ptr<std::vector<T>>
.– Jarod42
Mar 7 at 21:40
Thank you. No, my problem is not sharing the object (that's why I just added the unique). I just wanted to know when we can have a dynamically allocated vector/array, why should we use smart pointers whose, as I know, main application is dynamic allocation?
– Federico
Mar 7 at 21:41