Solved

insert and incompatible iterators

Posted on 2013-12-31
1
371 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 66
mixing C++ & C# in Vis Studio 2013 7 236
Embarcadero C++ Builder XE2 TDateTime 8 80
Need some help with mailto 16 33
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

749 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