Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

std::copy or <vector>.push_back

Status
Not open for further replies.

Lorey

Programmer
Feb 16, 2003
88
SG
Hi!

just want to know whats the best to use (std::copy or push_back)

this:

std::copy(buf.begin()+pos, buf.begin()+pos+length,
data.begin() )

or :

for (unsigned char size_t index=pos; index < pos+length ; index++)
{
data.push_back(getBufData(index));
}

Also, do i really need to use data.reserve before std::copy
 
Also, do i really need to use data.reserve before std::copy
Well only if you want to avoid corrupting memory and possibly crashing your program. :p

Member functions are always preferrable to non-member functions because they can access the internal data structures of the class they're in and therefore can use some optimizations that would leave external functions in the dust. Plus, sometimes external functions simply cannot do what you want. std::remove() would be a good example -- it doesn't actually remove anything from a container because it can't; only the container's erase() member function can truly remove values.
Regardless, std::vector.push_back() & std::copy() don't do the same thing. A better comparison would be the range versions of std::vector.assign() or std::vector.insert()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top