Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

insert and incompatible iterators

Posted on 2013-12-31
1
Medium Priority
?
385 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 35

Accepted Solution

by:
sarabande earned 1600 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

Independent Software Vendors: 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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

715 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