C++ 类成员容器迭代器在成员函数std::find中不兼容,但为什么不兼容?
我犯了一些我似乎无法控制的行为。假设我有一个定义如下的类(一个简单的示例):C++ 类成员容器迭代器在成员函数std::find中不兼容,但为什么不兼容?,c++,visual-c++,c++11,visual-studio-2012,C++,Visual C++,C++11,Visual Studio 2012,我犯了一些我似乎无法控制的行为。假设我有一个定义如下的类(一个简单的示例): class一些\u类 { 公众: 某个_类() { x_u[“0”]=“0”; x_u[“1”]=“1”; y_[0]=x_.find(“0”); y_[1]=x_.find(“1”); } int某些函数(int i) { 如果(std::find(y_u.begin(),y_u.end(),x_u.find(“1”)!=y_u.end()) { std::cout你的情况 #include <unordere
class一些\u类
{
公众:
某个_类()
{
x_u[“0”]=“0”;
x_u[“1”]=“1”;
y_[0]=x_.find(“0”);
y_[1]=x_.find(“1”);
}
int某些函数(int i)
{
如果(std::find(y_u.begin(),y_u.end(),x_u.find(“1”)!=y_u.end())
{
std::cout你的情况
#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);
}
对于decltype-
std::unordered_map<string, string> x_;
std::array<decltype(x_.begin()), 2> y_;
std::无序映射x;
std::数组y;
类块
中不正确,因为没有对象。如果x_
将是静态的
这可能是复制构造函数的问题,复制数组中包含的迭代器,因此这些迭代器不指向同一对象的映射,而是指向从中复制的对象的映射。可以吗显示创建some_类
实例并调用some函数
的代码?请发布一个完整的程序来演示这个问题。我添加了int main(){some_类().some_函数();}
;程序运行到结束。此外,您认为您的切面有什么错误?编译器接受y
的声明(在提问时,最好验证您是否提供了足够的信息来演示您试图解决的问题。)尝试const_迭代器
。此外,您可以使用初始值设定项列表:x_{0”,“0”},{1”,“1”},y_{x.find(“0”),x_uu.find(“1”)}
考虑一下您的示例……一些的用法在哪里?是的,基本上就是这样。清醒的时间太长了,但“bug对话”似乎总是有帮助。现在我们只希望这对其他人也有用。:)
std::unordered_map<string, string> x_;
std::array<decltype(x_.begin()), 2> y_;
#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);
}
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;
}
std::unordered_map<string, string> x_;
std::array<decltype(x_.begin()), 2> y_;