Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;使用第三个序列作为标准,检查两个序列是否相等_C++_Algorithm_Std_Equality - Fatal编程技术网

C++ C++;使用第三个序列作为标准,检查两个序列是否相等

C++ C++;使用第三个序列作为标准,检查两个序列是否相等,c++,algorithm,std,equality,C++,Algorithm,Std,Equality,我需要检查两个给定的序列是否相等,但如果我发现一个不匹配的元素,我需要检查第三个序列(和其他两个序列大小相同),如果可以忽略的话 我知道我可以编写一个简单的算法来解决这个问题,但我想知道是否有类似C++的风格来解决这个问题,使用std算法 例如: A = [1, 2, 3, 4, 5, 6] B = [1, 2, 3, A, 5, 6] M = [true, true, true, false, true, true] N = [true, true, true, true, true, tru

我需要检查两个给定的序列是否相等,但如果我发现一个不匹配的元素,我需要检查第三个序列(和其他两个序列大小相同),如果可以忽略的话

我知道我可以编写一个简单的算法来解决这个问题,但我想知道是否有类似C++的风格来解决这个问题,使用std算法

例如:

A = [1, 2, 3, 4, 5, 6]
B = [1, 2, 3, A, 5, 6]
M = [true, true, true, false, true, true]
N = [true, true, true, true, true, true]

bool equal1 = the_equal(begin_A, end_A, begin_B, begin_M); // Should return true, even if '4' is different from 'A' since in the same position of the found mismatch, in the M sequence, we have a false that indicates that position should be ignored.

bool equal2 = the_equal(begin_A, end_A, begin_B, begin_N); // Should return false, since '4' is different from 'A' and we have a true in sequence N for that position.
这可以通过以下方式解决:

template<I1, I2, I3> // iterators
bool the_equal(I1 first1, I1 last1, I2 first2, I3 first3) {
  while (first1 != last1) {
    if (*first1 != *first2 && *first3 != false)
      return false;
    ++first1; ++first2; ++first3;
  }

  return true;
}
模板//迭代器
布尔等于(I1 first1,I1 last1,I2 first2,I3 first3){
while(first1!=last1){
if(*first1!=*first2&&*first3!=false)
返回false;
++first1;++first2;++first3;
}
返回true;
}

编辑:我忘了说我需要在c++98中解决这个问题,因为工具链的局限性:(

虽然
std::equal
很接近,但不清楚它是否可以直接使用。我认为你的代码相当好。我可能会将其重写为稍微不同的代码

template<I1, I2, I3> // iterators
bool equal_with_mask(I1 first1, I1 last1, I2 first2, I3 first3) {
  for (; first1 != last1; ++first1, ++first2, ++first3) {
    if (*first3 && (*first1 != *first2)) {
      return false;
    }
  }
  return true;
}
模板//迭代器
带屏蔽的布尔相等(I1 first1、I1 last1、I2 first2、I3 first3){
对于(;first1!=last1;++first1、++first2、++first3){
如果(*first3&&(*first1!=*first2)){
返回false;
}
}
返回true;
}
如果(in)相等性比较很重,那么首先检查掩码可能是好的

琐事:第三个序列(
first3
)类似于某些硬件设备中使用的掩码,如三值内容寻址内存(TCAM)。

您可以创建一个(可变)函子:


我还认为您的代码很好。您可以使用其中一个标准函数,但imho并不总是建议这样做,因为它会降低代码的可读性。最好的解决方案是您可以阅读的解决方案。不过,这里有一个解决方案(许多可能的实现之一)使用:

模板//迭代器
布尔等式2(I1 first1,I1 last1,I2 first2,I3 first3){
自动p=std::不匹配(first1、last1、first2);
如果(p.first!=last1)
return(!first3[p.first-first1]?true:the_equal2(p.first+1,last1,p.second+1,first3));
返回false;
}
请注意,未检查错误


但它看起来像是一个比POST更好的例子吗?使用递归和
autop
,您必须查找
p
可能是什么类型,即使您不知道会返回什么不匹配(a
std::pair
)您甚至可以通过使用一些迭代器特征或奇特的find if/equal/mismatch组合来改进它,以摆脱递归并使其更不可读。

这行
if(first1!=first2&&first3!=false)
be
if(*first1!=*first2&*first3!=false)
(注意添加的星号用于取消引用)你想实现什么?是关于编码风格吗?我的意思是,即使在这样一个简单的例子中,也可能有很多可能的方法,但它们可能看起来比你已经想到的更糟糕。@Arun修复,谢谢。@user1810087我想知道我是否会重新发明轮子。:[FYI]将显示C++中所有的标准算法。这里错误>代码>模板<代码>是的,Arun,你明白了。第三个序列将是一个面具。@BiagioFesta谢谢,我使用了问题中的文本并对其进行了修改。让我抓起一个编译器,过一会儿再处理完整的语法。实际上,这个非常好:)它正是我要找的=)一个简单的循环就足以摆脱递归。
template <typename IT>
struct CmpWithMask
{
    CmpWithMask(IT it) : it(it) {}

    template <typename LHS, typename RHS>
    bool operator () (const LHS& lhs, const RHS& rhs) {
        return !*it++ || lhs == rhs;
    }
    IT it;
};

template <typename IT>
CmpWithMask<IT> MakeCmpWithMask(IT it) { return CmpWithMask<IT>{it}; }
std::cout << std::equal(A.begin(), A.end(),
                        B.begin(),
                        MakeCmpWithMask(M.begin())) << std::endl;
std::cout << std::equal(A.begin(), A.end(),
                        B.begin(),
                        MakeCmpWithMask(N.begin())) << std::endl;
template<typename I1, typename I2, typename I3> // iterators
bool the_equal2(I1 first1, I1 last1, I2 first2, I3 first3) {
  auto p = std::mismatch(first1, last1, first2);
  if(p.first != last1)
    return (!first3[p.first - first1] ? true : the_equal2(p.first+1, last1, p.second+1, first3));

  return false;
}