首页 > 解决方案 > Is it bug in std::includes or am I doing something wrong

问题描述

C++ STL includes (http://www.cplusplus.com/reference/algorithm/includes/) Test whether sorted range includes another sorted range Returns true if the sorted range [first1,last1) contains all the elements in the sorted range [first2,last2)

void Test_STL_Includes() {
    vector<char>secondRowInKeyboard{ 'a','A','s','S','d','D','f','F','g','G','h','H','j','J','k','K','l','L' };

    sort(secondRowInKeyboard.begin(), secondRowInKeyboard.end());

    string s("Alaska");
    sort(s.begin(), s.end());

    if (includes(secondRowInKeyboard.begin(), secondRowInKeyboard.end(), s.begin(), s.end()))
    {
        cout << "Matches";
    }
    else
    {
        cout << "Not Matches";
    }

}

Expected: "Matches"

Actual: "Not Matches"

Am I making some mistake?

标签: c++stl

解决方案


It doesn't match because the "needle" contains two a but the "haystack" only has one a.

See also: What does std::includes actually do? ; another way to state it is that the set intersection must equal the second set.


推荐阅读