efficiency copy vector into a second vector
-
Hi all I have to copy elements of a std::vector at the end of a second std::vector. What is the most efficient approach? I think that method insert is not very efficient (am I right?); what if I use copy in ? If also this is not good, what way can I follow? Kind Regards ManuStone
-
Hi all I have to copy elements of a std::vector at the end of a second std::vector. What is the most efficient approach? I think that method insert is not very efficient (am I right?); what if I use copy in ? If also this is not good, what way can I follow? Kind Regards ManuStone
Does
insert(vec2.end(), vec1.begin(), vec1.end());
work?
- S 50 cups of coffee and you know it's on!
-
Hi all I have to copy elements of a std::vector at the end of a second std::vector. What is the most efficient approach? I think that method insert is not very efficient (am I right?); what if I use copy in ? If also this is not good, what way can I follow? Kind Regards ManuStone
manustone wrote:
I think that method insert is not very efficient (am I right?); what if I use copy in ?
Do you mean the copy constructor? I think performance is quite comparable to insertion method (as suggested by Steve Echols), but why don't you make a test?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
[my articles] -
manustone wrote:
I think that method insert is not very efficient (am I right?); what if I use copy in ?
Do you mean the copy constructor? I think performance is quite comparable to insertion method (as suggested by Steve Echols), but why don't you make a test?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
[my articles] -
Hi all I have to copy elements of a std::vector at the end of a second std::vector. What is the most efficient approach? I think that method insert is not very efficient (am I right?); what if I use copy in ? If also this is not good, what way can I follow? Kind Regards ManuStone
std::vector vec1; std::vector vec2; std::copy(vec1.begin(), vec1.end(), std::back_inserter(vec2));
Pax Domini sit semper vobiscum