Solved

insert and incompatible iterators

Posted on 2013-12-31
1
355 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
1 Comment
 
LVL 32

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now