为什么这些C 元组不相等
Why are these c++ tuples not equal?
如果左侧的每个字符串变量肯定包含了后者的字面等价物,那为什么我不收到我的消息?
if (make_tuple(this->currentState, inputSymbol, stackTop) == make_tuple("q0", "a", "0"))
cout << "These tuples are equal" << endl;
我问的原因是因为我使用的是带有元组作为钥匙的地图,当我尝试使用find()时,它的行为就好像键在映射中不存在,我确定它是因为我使用迭代器浏览了地图,并显示了所有键(元组中的每个元素)。我怀疑该错误与上述代码有关,因为这些代码应该相等,但事实并非如此。(我正在使用map.find(make_tuple(blah,blah,blah)),并将其与map.end.end())思想进行比较?
这是我对您的问题的最佳猜测:
const char input[] = "hello";
if (std::make_tuple(input) == std::make_tuple("hello")) {
// won't get here
std::cout << "equaln";
}
原因是 tuple<>::operator==
只是元素平等。在这种情况下,我们有两个tuple<const char*>
,但是尽管它们指出的字符串相同,但实际的指针本身是不同的,我们只是在比较指针。
如果您希望它能起作用,则需要左侧是一种具有正确功能的operator==
的类型。例如,std::string
:
const char input[] = "hello";
if (std::make_tuple(std::string(input)) == std::make_tuple("hello")) {
// now it happens
std::cout << "equaln";
}
或,由于您使用的是std::map
,因此您将不是想要使用默认的tuple<>::operator<
,而是提供使用strcmp
而不是原始指针<
。
相关文章:
- 如何以优化的方式同时迭代两个间距不相等的数组
- 您自己的类型的结构化绑定,不是结构或元组(通过公共成员函数)
- 从函数返回元组时,将复制元组的参数,而不是移动元组参数
- 在 c++ 中比较不相等数组或字符串的方法
- 元组布尔值不会更改
- std::元组大小,是不是错过了优化?
- 为什么不采用非类型部分专用元组?
- 为什么不提升元组运算符 == 并<<编译?
- Boost::P ython,将元组转换为Python有效,矢量<tuple>不
- 遍历元组 c+17 样式的向量不起作用?
- 不带元组的可变参数模板
- GCC/Clang 返回元组时x86_64 C++ ABI 不匹配?
- 为什么这些C 元组不相等
- 为什么不能从元组中分配一对,但可以从一对中分配元组?
- c value_type对于std :: tr1不起作用:std :: map中的元组
- 错误:与提升::元组的'operator=='不匹配
- C++11 在运行时不使用 switch 为元组编制索引的方法
- 关于 Python 元组的不变性
- 元组不是按顺序构建的?
- 为什么元组不能工作?