我需要将字符串向量转换为小写,但我需要保留文件名的大小写。它们由先前的字符串标记“file”或“out”标识。
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
template <class T>
void print(const T& v) {
std::copy(v.begin(), v.end(),
std::ostream_iterator<typename T::value_type>(std::cout, "\n"));
}
std::string lowercase(const std::string& s)
{
std::string result(s);
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
return result;
}
int main() {
std::vector<std::string> tokens {"Col1", "Col2", "File", "FileIn.dat", "Out", "FileOut.dat"};
std::transform(tokens.begin(), tokens.end(), tokens.begin(), lowercase);
// how to replace lowercase() with a lambda that will take the previous
// element into account while converting an element into lowercase
print(tokens);
return 0;
}
在上面的代码中,结果应该是
{"col1", "col2", "file", "FileIn.dat", "out", "FileOut.dat"};
在“file”和“out”之后保留字符串的大小写。
有没有办法使用std::transform和lambda函数来做到这一点?