Skip to main content
GameDev.net gamedev.net
🔒 Locked

Equating std::vectors

Started by daniel_i_l Aug 14, 2006 at 8:19 AM 5 replies 700+ views
Original Post
daniel_i_l
daniel_i_l
If I have a std vector: vector variables; and pushed it back with a lot of different vars. How do I take another vector vector other; and fill it up with the values of the vector variables? esentially doing: other = variables? -and with doing a for loop;) Thanks!
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
ChaosPhoenix
ChaosPhoenix
Quote:
Original post by daniel_i_l
If I have a std vector: vector variables;
and pushed it back with a lot of different vars. How do I take another vector
vector other;
and fill it up with the values of the vector variables? esentially doing:
other = variables?
-and with doing a for loop;)
Thanks!



I assume you mean "without doing a for loop".

I thought STD Vector had the assignment operator overloaded? If it doesn't then you'll have to use a for(unless you want to modify the std vector source code, or write your own vector class).

However, which ever way you do it you'll most likely be using a for loop anyway, so just write a decent function that does what you want.
ToohrVyk
ToohrVyk
std::vector<var> variables;std::vector<var> other;// Simplest wayother = variables;// A little bit faster: copy only the elements in excess.if (variables.size() > other.size())  std::copy(variables.begin() + other.size(), variables.end(), std::back_inserter(other));
Enigma
Enigma
How can I copy thee? Let me count the ways...
  • new_vector = old_vector;

  • new_vector.assign(old_vector.begin(), old_vector.end());

  • new_vector.clear();
    new_vector.insert(new_vector.begin(), old_vector.begin(), old_vector.end());

  • new_vector.swap(old_vector); (More efficient but leaves old_vector with new_vectors original contents

  • std::copy(old_vector.begin(), old_vector.end(), std::back_inserter(new_vector)); (inefficient)

  • std::copy(old_vector.rbegin(), old_vector.rend(), std::front_inserter(new_vector)); (inefficient, liable to end up on thedailywtf.com)

  • new_vector.clear();
    new_vector.reserve(old_vector.size());
    for (std::vector< Type >::const_iterator element = old_vector.begin(), elements_end = old_vector.end(); element != elements_end; ++element)
    {
    new_vector.push_back(*element);
    }

  • ...
Σnigma
mikeman
mikeman
Quote:

// A little bit faster: copy only the elements in excess.

if (variables.size() > other.size())
std::copy(variables.begin() + other.size(), variables.end(), std::back_inserter(other));


Hm... The fact that 2 "subvectors" have the same size, does not mean that they're actually equal... If the "other" vector has N elements, and the "variables" vector has N+M elements, how do you know that the other[0,N) and variables[0,N) are actually equal and thus does not need to be copied? For example:

vector other: 5,12,28
vector variables: 1,2,3,4,5,6

After your copying, wouldn't "other" have: 5,12,28,4,5,6?
ToohrVyk
ToohrVyk
Quote:
Original post by mikeman
Hm... The fact that 2 "subvectors" have the same size, does not mean that they're actually equal...


You are absolutely right, this is not the case in general.

Now, suppose that other is intended to be a copy of variables, that the only operation performed on variables is appending, and the only operation performed on other is the "copy excess elements" operation described in my post. In this particular situation, other and variables would be the same (and this same process is used when resuming downloads, for instance). Perhaps I should have insisted a little bit more on this specific assumption.

If you want to feel safe, you can include an assertion, such as:

assert( std::equal(other.begin(), other.end(), variables.begin()) );

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.