简体   繁体   English

连接两个STL向量时出现错误“向量迭代器不兼容”

[英]Error “vector iterators incompatible” when concatenating two STL vectors

Following the advice on this question , I am attempting to do just that. 根据有关此问题的建议,我正在尝试做到这一点。 However, at runtime I get an error "vector iterators incompatible", in the following: 但是,在运行时,出现以下错误“向量迭代器不兼容”:

std::vector<SE> all;
all.insert(all.end(),p->ev.begin(),p->ev.end());

where class pointed to by p contains a member p指向的类包含一个成员

std::vector<SE> ev;

It is possible for the vector ev to be empty, in which case I want 'all' to be unchanged. 向量ev可能为空,在这种情况下,我希望'all'保持不变。 Otherwise, I want all the elements of p->ev to be added to 'all', in the same order, at the end of 'all'. 否则,我希望将p-> ev的所有元素以相同的顺序添加到'all'的末尾。

What am I doing wrong? 我究竟做错了什么?

Visual Studio 2010 C++, 32-bit Windows XP Visual Studio 2010 C ++,32位Windows XP

ADDITIONAL INFO: Using the debugger, I have traced the proximate cause of the error message as the "owning container" of p->ev.begin() being 0. I have no clue what the "owning container" means. 附加信息:使用调试器,我已跟踪错误消息的直接原因,因为p-> ev.begin()的“拥有容器”为0。我不知道“拥有容器”是什么意思。

My first thought is to try something like this 我的第一个想法是尝试这样的事情

if(!p->ev.isEmpty())
{
   foreach(SE record in p->ev)
   {
      all.insert(SE);
   }
}

UPDATE: I didn't see you link at first, but will leave my anwser. 更新:起初我没有看到您链接,但是会离开我的答案。

I tried this and it seemed to work ok 我尝试了这个,它似乎工作正常

#include <vector>
#include <iostream>

struct P {
    P() {
        ev.push_back(3);
        ev.push_back(4);
    }
    std::vector<int> ev;
};

int main(int argc,char** argv) {
    std::vector<int> all;
    all.push_back(1);
    all.push_back(2);

    P* p = new P();
    all.insert(all.end(),p->ev.begin(),p->ev.end());
    copy(all.begin(),all.end(),std::ostream_iterator<int>(std::cout));
        delete p;
    return 0;
}

The only thing I could thing of giving that error would be if the SE type is redefined somewhere (maybe a typedef?). 我唯一能给出该错误的事情是,如果在某处重新定义了SE类型(可能是typedef?)。 For the above example using basic types there shouldn't be an issue. 对于上面使用基本类型的示例,应该没有问题。

Produces 1,2,3,4 in order. 按顺序产生1,2,3,4。 If ev is empty, then we get 1,2 as you asked 如果ev为空,则按照您的要求得到1,2

Apparently this is a bug in Visual Studio 2010. 显然,这是Visual Studio 2010中的错误。

Visual Studio has a feature "Checked Iterators". Visual Studio具有“检查的迭代器”功能。 In a debug build, every iterator operation is chacked at runtime for errors, eg, out of range. 在调试版本中,每个迭代器操作都会在运行时被检查是否存在错误,例如超出范围。 My pgm failed one of these checks, namely, that the iterators for the range for the vector being inserted were from the same collection class (ie, in this case, from the same vector). 我的pgm无法通过这些检查之一,即所插入向量范围的迭代器来自同一集合类(即,在这种情况下,来自同一向量)。 As you can see from my code sample, they were, so the test result was incorrect. 从我的代码示例中可以看到,它们是正确的,因此测试结果不正确。

The test worked correctly if the vector being inserted (p->ev in my example) was in the same class as the destination vector ('all'), but not if the vector being inserted was in a different class. 如果要插入的向量(在我的示例中为p-> ev)与目标向量属于同一类(“ all”),则该测试正常进行,但是如果要插入的向量位于不同的类中,则该测试无法正常进行。 That's one reason why Ronnie didn't see the problem, even if he was using VS 2010. 这就是Ronnie即使使用VS 2010却看不到问题的原因之一。

For anyone who may run into this same difficulty, the "cure" is to disable the iterator checking, by defining C++ preprocessor variable _ITERATOR_DEBUG_LEVEL as 0. The Microsoft documentation on use of _SECURE_SCL for this purpose is incorrect. 对于可能遇到相同困难的任何人,“治愈”是通过将C ++预处理程序变量_ITERATOR_DEBUG_LEVEL定义为0来禁用迭代器检查。为此目的使用_SECURE_SCL的Microsoft文档是不正确的。

With checking disabled, everything works as expected. 禁用检查后,一切都会按预期进行。

A similar bug was reported, and allegedly fixed, in VS 2010 in connection with 'erase'. VS 2010中报告了一个类似的错误,并据称已修复,该错误与“擦除”有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM