关于向量的一些问题 [英] some question about vector

查看:129
本文介绍了关于向量的一些问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像这样的代码:

the code like this:

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;
}


该代码在Visual Studio 2003中运行得很好,但是在Visual Studio 2005中运行时,Expression:vector迭代器不兼容.为什么?
错误: http://hi.csdn.net/space-1057418-do-album -picid-991700.html

感谢


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

推荐答案

您正在尝试比较来自不同向量的迭代器.这是非法的.

甚至不清楚你的想法是什么.您是否要检查其中一个向量是否与另一个向量以相同的地方"开头?为什么?即使您尝试在不同向量中使用指向同一对象的指针,也没有任何意义.迭代器不是指向矢量元素的指针.试想一下,如果尝试使用从另一个具有相同类型元素的不相关向量中获得的迭代器迭代一个向量,会发生什么情况.

我认为此错误是旨在避免此类病理情况的万无一失的功能.

另请参阅: http://social.msdn.microsoft.com /Forums/zh-CN/vclanguage/thread/9c319ef6-2497-4040-85c3-8ee0b383ded6 [ 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
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


您不能将迭代器与NULL(或与其他容器对象的迭代器)进行比较.之所以在VS2003中出现这种情况仅仅是因为它(即VS2003)不符合该标准,但是在较新的版本(以及C ++标准)中,这样的比较是非法的,或者至少是未定义的.

仅当迭代器指向相同的容器对象时,您才可以比较它们.它们是同一类型是不够的.由于对实际对象的这种依赖性,因此在编译时无法捕获错误,因此该实现包含一个断言,该断言将在运行时提醒您.

如果要测试迭代器以查看其是否已到达容器的末尾,则必须创建一个迭代器常量,该常量精确地表示以下内容:
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:
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;
}


针对解决方案2中的问题,通过将其与end返回的迭代器进行比较,您可以找出迭代器是否有效的方式( )容器的成员函数.对于stl容器,end()迭代器与NULL相当.您无法取消引用此迭代器,结果类似于尝试取消引用NULL指针.

在原始代码中,如果您想在迭代器无效的情况下打印出消息,或者按照您的要求,将其打印为NULL或为空,则结果如下所示:
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:
        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>



另外,如果您只是想检查容器是否为空,请使用stl容器的empty()成员函数.



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


这篇关于关于向量的一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆