降低 std::vector<std::string>
ToLower std::vector<std::string>
这与问题有关:
虽然现在一切正常,但我唯一无法完成的是 降低 用户输入,因为我收到错误:
函数
bool lookupTerm(const std::string& term, const std::vector<std::string>& possible_names) {
transform(term.begin(), term.end(), term.begin(), ::tolower);
for (const std::string &possible_name : possible_names)
{
if (possible_name.compare(term) == 0)
return true;
}
return false;
}
参数
const std::vector<std::string> possible_asterisk = { "star" ,
"asterisk" ,
"tilde"};
string term = "SoMeWorD";
错误
In file included from /usr/include/c++/7.2.0/algorithm:62:0,
from jdoodle.cpp:5:
/usr/include/c++/7.2.0/bits/stl_algo.h: In instantiation of '_OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation) [with _IIter = __gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >; _OIter = __gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >; _UnaryOperation = int (*)(int) throw ()]':
jdoodle.cpp:40:64: required from here
/usr/include/c++/7.2.0/bits/stl_algo.h:4306:12: error: assignment of read-only location '__result.__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >::operator*()'
*__result = __unary_op(*__first);
我知道转换应该接收一个字符串。我怎样才能暂时将 std::vector 转换为简单的 string 以便我可以将该词变为小写?
这是因为term
是const
参考。在将其转换为小写之前复制一份:
bool lookupTerm(const std::string& term, const std::vector<std::string>& possible_names) {
std::string lower(term);
transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
for (const std::string &possible_name : possible_names)
{
if (possible_name.compare(lower) == 0)
return true;
}
return false;
}
您也可以通过删除 const
并按值获取参数来实现相同的效果:
bool lookupTerm(std::string term, const std::vector<std::string>& possible_names) {
std::transform
需要能够更改第三个参数取消引用的任何内容。
这对你的情况不起作用,因为 term
是一个 const
对象。
您可以创建函数局部对象来存储转换后的字符串。
std::string lowercaseTerm(term);
transform(term.begin(), term.end(), lowercaseTerm.begin(), ::tolower);
然后在下一行中使用 lowercaseTerm
。
if (possible_name.compare(lowercaseTerm) == 0)
这与问题有关:
虽然现在一切正常,但我唯一无法完成的是 降低 用户输入,因为我收到错误:
函数
bool lookupTerm(const std::string& term, const std::vector<std::string>& possible_names) {
transform(term.begin(), term.end(), term.begin(), ::tolower);
for (const std::string &possible_name : possible_names)
{
if (possible_name.compare(term) == 0)
return true;
}
return false;
}
参数
const std::vector<std::string> possible_asterisk = { "star" ,
"asterisk" ,
"tilde"};
string term = "SoMeWorD";
错误
In file included from /usr/include/c++/7.2.0/algorithm:62:0,
from jdoodle.cpp:5:
/usr/include/c++/7.2.0/bits/stl_algo.h: In instantiation of '_OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation) [with _IIter = __gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >; _OIter = __gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >; _UnaryOperation = int (*)(int) throw ()]':
jdoodle.cpp:40:64: required from here
/usr/include/c++/7.2.0/bits/stl_algo.h:4306:12: error: assignment of read-only location '__result.__gnu_cxx::__normal_iterator<const char*, std::__cxx11::basic_string<char> >::operator*()'
*__result = __unary_op(*__first);
我知道转换应该接收一个字符串。我怎样才能暂时将 std::vector 转换为简单的 string 以便我可以将该词变为小写?
这是因为term
是const
参考。在将其转换为小写之前复制一份:
bool lookupTerm(const std::string& term, const std::vector<std::string>& possible_names) {
std::string lower(term);
transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
for (const std::string &possible_name : possible_names)
{
if (possible_name.compare(lower) == 0)
return true;
}
return false;
}
您也可以通过删除 const
并按值获取参数来实现相同的效果:
bool lookupTerm(std::string term, const std::vector<std::string>& possible_names) {
std::transform
需要能够更改第三个参数取消引用的任何内容。
这对你的情况不起作用,因为 term
是一个 const
对象。
您可以创建函数局部对象来存储转换后的字符串。
std::string lowercaseTerm(term);
transform(term.begin(), term.end(), lowercaseTerm.begin(), ::tolower);
然后在下一行中使用 lowercaseTerm
。
if (possible_name.compare(lowercaseTerm) == 0)