Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::copy failure, "cannot seek vector iterator after end"

Tags:

c++

stdvector

I put together this test case that reproduces the conditions and problem I'm getting in larger code. I do in fact need to copy from a C array of POD structures, but I'd like the destination to be a vector so it can handle the copy deletion on its own.

TEST_METHOD(std_copy)
{
    struct W { long a; int b; char c; char d; };
    W block[1] = { { 15, 42, 'D', 'X' } };
    std::vector<W> dest;
    dest.reserve(1);
    std::copy(block, block+1, dest.begin());
    Assert::AreEqual(42, dest[0].b);
}

The assertion "cannot seek vector iterator after end" seems to be occurring within the dest.begin() call, which makes no sense to me. I'm sure I'm just missing an obvious detail, but what is it?

like image 765
Mike C Avatar asked Jun 02 '19 05:06

Mike C


People also ask

How to copy items from one iterator to another in C++?

copy () function is used to copy items from one iterator to another iterator with a specific range. We can define the start and end position of the source and it will copy all items in this rage to a different destination. To use copy () function, we need to include <bits/stdc+.h> or header file. The syntax of std::copy () is as below:

How to copy old vector into new vector in Python?

copy (first_iterator_o, last_iterator_o, back_inserter ()) :- This is another way to copy old vector into new one. This function takes 3 arguments, first, the first iterator of old vector, second, the last iterator of old vector and third is back_inserter function to insert values from back. This also generated a deep copy.

What causes iterators to become invalid in C++?

Erasing an element from a container causes iterators to the element to become invalid. Increasing the size of a vector by using push or insert causes iterators into the vector to become invalid. If you compile this sample program in debug mode, at run time it asserts and terminates.

How to copy elements of an initialized vector to another vector?

Method 3 : By passing vector as constructor. At the time of declaration of vector, passing an old initialized vector, copies the elements of passed vector into the newly declared vector. They are deeply copied.


1 Answers

As the error message said, you're getting out of the bound of the vector.

Given std::copy(block, block+1, dest.begin());, dest is supposed to contain at least the same number of elements (i.e. one element here), but it's empty in fact.

You can use resize instead of reserve,

std::vector<W> dest;
dest.resize(1);
std::copy(block, block+1, dest.begin());

Or just

std::vector<W> dest(1);
std::copy(block, block+1, dest.begin());

Or use std::back_inserter to get an std::back_insert_iterator.

The container's push_back() member function is called whenever the iterator (whether dereferenced or not) is assigned to.

std::vector<W> dest;
dest.reserve(1);
std::copy(block, block+1, std::back_inserter(dest));
like image 165
songyuanyao Avatar answered Sep 21 '22 17:09

songyuanyao