std :: find case insensitive checking

时间:2018-01-03 17:16:43

标签: c++

bool is_executable_file(std::wstring file) {
    std::vector<std::wstring> d = get_splitpath(file);
    // d [ 3 ] is extension of file ie .exe
    std::vector<std::wstring> ext = { L".exe", L".dll", L".cmd", L".msi" };
    if ( std::find(ext.begin() , ext.end() , d [ 3 ]) != ext.end() ) {
        return true;
    }
    // do further checks
    return false;
}

在上面我如何让std::find进行不区分大小写的检查,以便我不需要添加所有组合,即.exe.EXE是否相同?

或者另外一种方法来检查扩展名列表中的文件扩展名,忽略扩展名和扩展名列表中的大小写?

1 个答案:

答案 0 :(得分:1)

您可以使用std::equalstd::tolower检查wstring s

#include <iostream>
#include <string>
#include <algorithm>
#include <cwctype>
#include <locale>

int main()
{
    std::wstring wstr1 = L"dll", wstr2 = L"DLL";

    auto icompare = [](wchar_t const &c1, wchar_t const &c2)
    { 
        return std::tolower(c1, std::locale()) == std::tolower(c2, std::locale());
    };

    if (std::equal(wstr1.begin(), wstr1.end(), wstr2.begin(), wstr2.end(), icompare))
        std::cout << "equal" << std::endl;
    else
        std::cout << "notEqual" << std::endl;

    return 0;
}

例如:https://ideone.com/I3zukI