简体   繁体   中英

Elements in a string vector to lower case

I am using one vector of strings and the strings are not in lower case. I want them to covert to lower case.

For converting string to lower case, I am using following method.

std::transform(strCmdLower.begin(), strCmdLower.end(), strCmdLower.begin(), ::tolower);

I can iterate vector and convert each string but I would like to know is there any library functions available for this purpose like above. Copying to new vector also fine.

std::vector<std::string> v1;
v1.push_back("ABC");
v1.push_back("DCE");

std::vector<std::string> v2(v1.begin(), v1.end());

You can still use std::transform with a lambda, eg

std::vector<std::string> v1;
v1.push_back("ABC");
v1.push_back("DCE");

std::vector<std::string> v2;
v2.reserve(v1.size());

std::transform(
    v1.begin(), 
    v1.end(), 
    std::back_inserter(v2), 
    [](const std::string& in) {
        std::string out;
        out.reserve(in.size());
        std::transform(in.begin(), in.end(), std::back_inserter(out), ::tolower);
        return out;
    }
);

LIVE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM