ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c – std :: includes实际上做了什么?

2019-10-05 05:16:03  阅读:257  来源: 互联网

标签:stl-algorithm c language-lawyer c17


the standard, std ::包括:

Returns: true if [first2, last2) is empty or if every element in the range [first2, last2) is contained in the range [first1, last1).
Returns false otherwise.

注意:由于这是在[alg.set.operations]下,因此必须对范围进行排序

从字面上看,如果我们让R1 = [first1,last1]和R2 = [first2,last2),这就是评估:

∀a∈R2 a∈R1

但是,这不是实际评估的内容.对于R1 = {1}且R2 = {1,1,1},std :: includes(R1,R2)返回false:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>

int main() {
    std::vector<int> a({1});
    std::vector<int> b({1,1,1});

    // Outputs 'false'
    std::cout << std::boolalpha
        << std::includes(a.begin(), a.end(), b.begin(), b.end()) << '\n';
}

Live on Wandbox

这是令人惊讶的.我用libstdc和libc验证了它,但我认为这不是标准库实现中的一个错误,因为它是算法库的一部分.如果这不是std :: includes应该运行的算法,那是什么?

解决方法:

我在cpplang slack和Casey Carter responded中发布了这个:

The description of the algorithm in the standard is defective. The intent is to determine [if] every element in the needle appears in order in the haystack.

[The algorithm it actually performs is:] “Returns true if the intersection of sorted sequences R1 and R2 is equal to R2”

或者,如果我们确保我们确定subsequence的含义:

Returns: true if and only if [first2, last2) is a subsequence of [first1, last1)

link to Casey Carter’s message

标签:stl-algorithm,c,language-lawyer,c17
来源: https://codeday.me/bug/20191005/1855098.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有