Untitled

 avatar
unknown
c_cpp
2 years ago
941 B
3
Indexable
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <cctype>
#include <algorithm>

using std::stringstream;
using std::string;
using std::cout;
using std::endl;
using std::vector;
using std::tolower;
using std::transform;

#define PRINT(X) cout << (#X) << " = " << X << endl

string str_tolower(string s) {
    transform(s.begin(), s.end(), s.begin(),
            [](unsigned char c){ return std::tolower(c); });
    return s;
}

int search(string target, vector<string> const& lst) {
    target = str_tolower(target);
    auto it = find_if(lst.begin(), lst.end(),
            [&](string const& s) { return target == str_tolower(s); });
    if(it == lst.end())
        return -1;
    return it - lst.begin();
}

int main() {
    vector<string> v {"Aaa", "Bbb", "Ccc"};
    PRINT(search("BBB", v));
    PRINT(search("DDD", v));
    PRINT(search("aaa", v));
    return 0;
}
Editor is loading...