Solved

insert and incompatible iterators

Posted on 2013-12-31
1
377 Views
Last Modified: 2014-01-13
As an exercise, I am implementing my own versions of the STL containers std::vector and std::string. When implementing my version of the following insert method

 void insert (iterator position, InputIterator first, InputIterator last);

Open in new window

I would like to check that the input iterators first and last refer to the same container. I know that the STL containers carry out such a check, since if one tries to use insert when this condition fails, one gets an error, e.g., "string iterators incompatible". How is this check implemented for STL containers?
0
Comment
Question by:Rothbard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 34

Accepted Solution

by:
sarabande earned 400 total points
ID: 39751800
with my compiler (ms visual c++ 2008) the following compiles without error:

std::vector<std::string> v0, v1, v2;

v0.push_back("ABC");
v1.push_back("DEF");
v2.push_back("GHI");
v0.insert(v0.begin(), v1.begin(), v2.end());

Open in new window


even if I exchange the 2nd and 3rd argument there is no (compiler) error.

however at runtime there is a check if you run the code in the debugger. the debugger has additional information for arguments at runtime and "knows" to which container an iterator belongs. because of that it would find out that v2.end() is beyond the range spanned by v1.begin() and v1.end() and therefore I get a debug assertion.

at my system it happened in function std::vector::const_iterator::operator== member function.

#if _HAS_ITERATOR_DEBUGGING
     Compat(_Right);
#else
     _SCL_SECURE_VALIDATE(this->_Has_container() && this->_Same_container(_Right));
#endif /* _HAS_ITERATOR_DEBUGGING */

Open in new window


when calling the Compat member function.

the operator == compares the right boundaries of the containers defined by the iterators first and last. it is checked in debug mode by calling the global function _Debug_range which exactly does the check you talked of.

void __CLRCALL_OR_CDECL _Debug_range(_InIt _First, _InIt _Last, const wchar_t *_File, unsigned int _Line)
{   // test iterator pair for valid range
    ...
}

Open in new window


in release mode the program crashes because of the error. when I run it from IDE it breaks in the function _Distance(_First, _Last, _Count); with access violation what is not very surprising because the (pointer) addresses associated with the iterators would not span a useable array of strings especially if using string class objects.

Sara
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question