Click here to Skip to main content
15,891,621 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
the code like this:
C++
struct EdsSmsRefTable_t
{
	EdsSmsRefTable_t(DWORD dwRadioIssi)
	{	
		dwIssi = dwRadioIssi;				
	}
	DWORD	dwIssi;
};
int _tmain(int argc, _TCHAR* argv[])
{
	vector<EdsSmsRefTable_t> a;
        vector<EdsSmsRefTable_t> b;

	a.push_back(EdsSmsRefTable_t(10));
	vector<EdsSmsRefTable_t>::iterator begin = a.begin();

	if (begin == b.begin())
	{
		cout<<"=="<<endl;
	}
	cout<<"end"<<endl;
	getchar();
	return 0;
}

the code runs very well in Visual Studio 2003,but runs fail in Visual Studio 2005 with Expression:vector iterators incompatible.why?
the error:http://hi.csdn.net/space-1057418-do-album-picid-991700.html

thanks
Posted
Updated 27-Nov-11 15:56pm
v4
Comments
Sergey Alexandrovich Kryukov 27-Nov-11 21:47pm    
On what line of this code sample?
--SA

You are trying to compare iterators from different vectors. It is illegal.

It's not even clear what was your idea. Are you trying to check if one of the vectors begins with the "same place" as another one? Why? It makes no sense, even if you try to use pointers to the same object in different vectors. Iterators are not pointers to vector elements. Just imagine what happens if you try to iterate through one vector using an iterator obtained from another one, unrelated vector with the elements of the same type.

I guess this error is a fool-proof feature designed to avoid such pathological situations.

See also: http://social.msdn.microsoft.com/Forums/en/vclanguage/thread/9c319ef6-2497-4040-85c3-8ee0b383ded6[^].

Make sure you understand C++ STL iterators:
http://en.wikipedia.org/wiki/Iterator[^],
http://new.cplusplus.com/reference/std/iterator/[^],
http://www.tenouk.com/Module31a.html[^],
http://stackoverflow.com/questions/5606973/understanding-iterators-in-the-stl[^].

—SA
 
Share this answer
 
v3
Comments
Albert Holguin 27-Nov-11 22:05pm    
Excellent answer SA, +5
Sergey Alexandrovich Kryukov 27-Nov-11 22:07pm    
Thank you, Albert.
--SA
chandanadhikari 28-Nov-11 5:39am    
great answer and useful links +5!
Sergey Alexandrovich Kryukov 28-Nov-11 11:00am    
Thank you very much,
--SA
You can not compare iterators to NULL (or iterators to other container objects). It was possible in VS2003 only because it (i. e. VS2003) wasn't conform to the standard, but in newer versions (and in the C++ standard) such comparisons are illegal, or at the very least undefined.

You can compare iterators only if they point to the same container object. it is not sufficient they are of the same type. Because of this dependency to the actual object, the error can not be caught at compile time, therefore the implementation contains an assertion that will alert you at runtime.

If you want to test an iterator to see whether it has reached the end of the container, then you must create an iterator constant that represents exactly that:
C++
void Foo()
   std::vector<int> va;
   std::vector<int> vb;
   std::vector<int>::iterator it_va = va.begin();
   std::vector<int>::iterator va_end = va.end();
   if (it_va == va_end) // ok
      std::cout << "end of vector a" << std::endl;
   std::vector<int>::iterator it_vb = vb.begin();
   std::vector<int>::iterator vb_end = vb.end();
   if (it_vb == vb_end) // ok
      std::cout << "end of vector b" << std::endl;

   if (it_vb == va_end) // error!
      std::cout "something wonderful has happened ..." << std::endl;
}
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 28-Nov-11 11:03am    
A (weird) follow-up question is answered in detail, my 5.
--SA
Stefan_Lang 28-Nov-11 11:32am    
Thank you.
btw. I 5ed you too - the number of useful links you can always provide to such questions is astounding. Either you're the administrator for a huge SW question database, or you're a Google-wizard ;-)
Albert Holguin 28-Nov-11 14:10pm    
Google is a wonderful thing... :)
wzq000000 28-Nov-11 20:47pm    
Stefan_lang,thanks for your advice!~
but I founed some bug with your codes.
I think you are wrong.
std::vector<int>::iterator it_vb = va.begin();
std::vector<int>::iterator vb_end = vb.end();
if (it_vb == vb_end) // so this is not Ok
std::cout << "end of vector b" << std::endl;
thanks again!~
Stefan_Lang 29-Nov-11 4:05am    
Ah, there's indeed an error, but it's a bit earlier (2 lines above the part you posted:
std::vector<int>::iterator it_vb = va.begin(); // error: this should assign vb.begin()

I've fixed my solution.
In response to your question in solution 2, the way you find out if your iterator is valid or not, is by comparing it to the iterator returned by the end() member function of the container. The end() iterator is comparable to NULL for stl containers. You cannot dereference this iterator, the results are similar to trying to dereference a NULL pointer.

In the original code, if you wanted to print out the message if the iterator was not valid, or as you were asking, NULL or empty, this is what it would look like:
C++
        vector<edssmsreftable_t> a;
// Don't need this anymore, cannot compare iterators from two different containers
//        vector<edssmsreftable_t> b;
 
	a.push_back(EdsSmsRefTable_t(10));
	vector<edssmsreftable_t>::iterator begin = a.begin();
 
	if (begin == a.end())
        {    
          // The iterator is the same as the end iterator.
          // That means you are at the end of the container.
          ... 
        }
</edssmsreftable_t></edssmsreftable_t></edssmsreftable_t>


Alternatively, if you just wanted to check if your container is empty, use the empty() member function of the stl container.
 
Share this answer
 
Hi,

I don't understand what you mean by the following line.

if (begin == b.begin())


If you are hoping to compare the two vector elements , this is not the right way. You need to create an iterator, Then traverse the entire list and compare each elements.
 
Share this answer
 
Comments
wzq000000 28-Nov-11 0:20am    
I want to get a iterator from a function.
like this:
int getIterator(vector<int>::iterator &pt)
{
}

vector<int>::iterator pt;
getIterator(pt);
then I want to judge the iterator which is NULL(empty) or not NULL(not empty).
Stefan_Lang 28-Nov-11 8:15am    
You can't compare an iterator to NULL, or at least not in VS2005 or later. See my Solution (solution 3) for an explanation and solution to your problem.
Albert Holguin 28-Nov-11 14:11pm    
I can't even tell from his code where he's trying to compare to NULL, that statement almost makes me think he copied the code from somewhere.
Stefan_Lang 29-Nov-11 4:07am    
Considering that everyone copies code, I think that is a safe assumption to make ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900