What does std::includes actually do?

后端 未结 3 988
慢半拍i
慢半拍i 2021-02-06 23:31

From the standard, std::includes:

Returns: true if [first2, last2) is empty or if every element in the range

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-07 00:17

    I believe you're trying to check if a includes b in your example, a doesn't include b but b does include a. If you swap b and a it will return true, since a is included in b.

    I hope I'm not missing something obvious.

    #include 
    #include 
    #include 
    
    int main() {
        std::vector a({1});
        std::vector b({1,1,1});
    
        // Outputs 'true'
        std::cout << std::boolalpha
            << std::includes(b.begin(), b.end(), a.begin(), a.end()) << '\n';
    }
    

    What I've understood by playing around with algorithm is, when you type includes(R2, R1) it checks if R2 owns R1 as a subgroup, if yes returns true if not returns false. Also if it's not ordered throws an error: sequence not ordered.

提交回复
热议问题