Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying/inserting a Vector

I am reading Accelerated C++ and I need few suggestions on the question posted below.

  1. What does this code do?

    vector<int>u(10,100)    
    vector<int>v;    
    copy(u.begin(), u.end(), v.end());
    
  2. Provide 2 possible ways to correct the program, and list its advantages and disadvantages.

The first part was pretty straightforward, but I need help in the second part. I have provided 3 approaches and I am wondering if there are any more possible solutions.

Also, I am not sure of advantages and disadvantages of my approach. I have made an attempt, so please give me your views.


copy()

std::vector<int> u(10, 100);
std::vector<int> v;
std::vector<int> w ;
std::vector<int> x ;
std::copy(u.begin(), u.end(), back_inserter(v)); // 1st way of doing

Advantages

  • std::copy() doesn't change the value of iterator
  • Since the parameters of std::copy() don't depend on the specific container, the code can be reused with different containers

Disadvantages

  • std::back_inserter() only works with sequential containers and hence cannot be used with maps
  • Assigning the wrong iterator value to third parameter of std::copy() will not result in compiler errors but the program may behave differently

insert()

w.insert(w.end(), u.begin(), u.end() );

Advantages

  • insert() can be used with most containers

Disadvantages

Can't think of any.


push_back()

for ( std::vector<int>::const_iterator it = w.begin(); it != w.end(); ++it )
{
    x.push_back( *it );
}

Advantages

Cant think of any.

Disadvantages

  • Slow compared to std::copy() or vector::insert().

Is my approach correct? What other possible solutions are there?

like image 232
samantha Avatar asked Mar 05 '26 20:03

samantha


1 Answers

Your title suggests that you're interested in copying a vector, but your code suggests you're interested in inserting into a vector (keeping in mind that despite its name std::copy is used for insertion here).

If you want to copy:

// This requires the vector types to match exactly
std::vector<int> v = u;

// In case the vector differ in their value_type
// This requires that the value_type of the source be
// convertible to the value_type of the destination
std::vector<int> v(u.begin(), u.end());

If you want to insert, then both methods you describe (using std::copy plus an iterator adaptor or calling the insert member) are appropriate. You should pick one according to whether you're working with containers or with iterators in your particular spot of code. (When working with iterators the burden of using the iterator adaptor is put on the client that passes the iterator, so there's no need to worry about push_back.) If all you have is iterators, then calling e.g. insert is simply not an option; if you do have the containers and one of the members can do the job then feel free to use it. I would not consider an error to use an algorithm though.

Try to leave the explicit loop as a last resort option.

like image 183
Luc Danton Avatar answered Mar 07 '26 09:03

Luc Danton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!