【问题标题】:Class member container iterators not compatible in member function std::find, but why not?类成员容器迭代器在成员函数 std::find 中不兼容,但为什么不兼容呢?
【发布时间】:2012-07-29 10:40:18
【问题描述】:

我遇到了一些我似乎无法理解的行为。假设我有一个定义如下的类(一个简单的示例):

class some_class
{
   public:
        some_class()
        {
            x_["0"] = "0";
            x_["1"] = "1";

            y_[0] = x_.find("0");
            y_[1] = x_.find("1");
        }

        int some_function(int i)
        {
            if(std::find(y_.begin(), y_.end(), x_.find("1")) != y_.end())
            {
                std::cout << "Found!" << std::endl;

                return 1001;
            }

            return -1001;
        }

   private:
       std::unordered_map<string, string> x_;
       std::array<unordered_map<string, string>::iterator, 2> y_;
};

代码在调试模式下使用 Visual Studio 2012 RC 编译(其他模式未测试),但在调用 some_function 期间程序失败并显示以下断言消息

... microsoft visual studio 11.0\vc\include\list 行:289

表达式:列表迭代器不兼容...

调用代码是这样的

auto vector<int> as;
as.push_back(1);   

auto vector<int> bs;   
auto some = some_class();

std::transform(as.begin(), as.end(), std::inserter(bs, bs.begin()), [=](int i) { return  some.some_function(i); });

问题:

这种安排会有什么问题?如果我在 some_function 中声明 x_y_ 而不是作为成员变量,我可以让代码运行良好。该类在 std::transform 之类的情况下被调用/使用,如果这种情况应该考虑的话。

顺便说一句,下面的声明似乎会被编译器拒绝,是否应该被拒绝?

std::unordered_map<string, string> x_;
std::array<decltype(x_.begin()), 2> y_;

错误信息是

错误 C2228: '.begin' 的左边必须有类/结构/联合

【问题讨论】:

  • 可能是复制构造函数的问题,它复制了数组中包含的迭代器,因此这些迭代器并不指向同一个对象的映射,而是指向从中复制的对象的映射。
  • 您能否展示创建some_class 实例并调用some_function 的代码?
  • 请发布一个完整的程序来演示这个问题。我添加了int main() { some_class().some_function(); };程序运行完成。另外,你认为你的切线有什么问题?编译器接受y_ 的声明。 (在提问时,最好验证您是否提供了足够的信息来证明您正在尝试解决的问题。)
  • 试试const_iterator。此外,您可以使用初始化列表:x_{{"0","0"}, {"1","1"}}, y_{x_.find("0"), x_.find("1")}
  • 看看你的例子...... some 在哪里使用?

标签: c++ visual-c++ c++11 visual-studio-2012


【解决方案1】:

你的情况。

#include <unordered_map>
#include <array>
#include <iostream>
#include <string>

class some_class
{
   public:
        some_class()
        {
            x_["0"] = "0";
            x_["1"] = "1";

            y_[0] = x_.find("0");
            y_[1] = x_.find("1");
        }

        int some_function()
        {
            if(std::find(y_.begin(), y_.end(), x_.find("1")) != y_.end())
            {
                std::cout << "Found!" << std::endl;

                return 1001;
            }

            return -1001;
        }

   private:
       std::unordered_map<std::string, std::string> x_;
       std::array<std::unordered_map<std::string, std::string>::iterator, 2> y_;
};

void function(some_class cl)
{
    cl.some_function();
}

int main()
{
    some_class c;
    function(c);
}

您的iterators 指向另一个map 的元素,后者存储在copy-ctor 之后的this 中,并且在find 中有比较。

    bool operator==(const _Myiter& _Right) const
        {   // test for iterator equality
 #if _ITERATOR_DEBUG_LEVEL == 2
        if (this->_Getcont() == 0
            || this->_Getcont() != _Right._Getcont())
            {   // report error
            _DEBUG_ERROR("list iterators incompatible");
            _SCL_SECURE_INVALID_ARGUMENT;
            }

对于 decltype -

std::unordered_map<string, string> x_;
std::array<decltype(x_.begin()), 2> y_;

class-block 不正确,因为没有对象。如果x_ 将是static,这将有效。

【讨论】:

  • 是的,基本上就是这样。清醒的时间太长,但“虫子谈话”似乎总是有帮助。现在让我们希望这对其他人也有用。 :)
猜你喜欢
  • 1970-01-01
  • 2017-02-07
  • 1970-01-01
  • 2023-03-04
  • 2019-02-25
  • 2012-01-15
  • 1970-01-01
相关资源
最近更新 更多