Qt - 无法让 lambda 工作
Qt - Cannot get lambda to work
我有以下功能,其中我想从单词 longer/shorter 中删除我的 std::set<QString> words
比 main_word
多于 4 个字符。
void Cluster::prune(QString main_word)
{
words.erase(std::remove_if(words.begin(),
words.end(),
[=](QString w){return std::abs(main_word.length() - w.length()) > 4;}),
words.end());
}
构建时出现以下错误:
d:\qt\tools\mingw48_32\lib\gcc\i686-w64-mingw32.8.0\include\c++\bits\stl_algo.h:1176: błąd: passing 'const QString' as 'this' argument of 'QString& QString::operator=(const QString&)' discards qualifiers [-fpermissive]
*__result = _GLIBCXX_MOVE(*__first);
^
我有点困惑 - 我对这个 lambda 做错了什么?
您不能在集合上使用 erase remove-if 习语 - 因为 set<K>
内部包含类型 const K
的元素 - 它们是不可修改的并且 std::remove_if
需要对象可移动分配。
你必须使用循环:
for (auto it = words.begin(); it != words.end(); /* nothing */)
{
if (std::abs(main_word.length() - it->length()) > 4) {
it = words.erase(it);
}
else {
++it;
}
}
我有以下功能,其中我想从单词 longer/shorter 中删除我的 std::set<QString> words
比 main_word
多于 4 个字符。
void Cluster::prune(QString main_word)
{
words.erase(std::remove_if(words.begin(),
words.end(),
[=](QString w){return std::abs(main_word.length() - w.length()) > 4;}),
words.end());
}
构建时出现以下错误:
d:\qt\tools\mingw48_32\lib\gcc\i686-w64-mingw32.8.0\include\c++\bits\stl_algo.h:1176: błąd: passing 'const QString' as 'this' argument of 'QString& QString::operator=(const QString&)' discards qualifiers [-fpermissive]
*__result = _GLIBCXX_MOVE(*__first);
^
我有点困惑 - 我对这个 lambda 做错了什么?
您不能在集合上使用 erase remove-if 习语 - 因为 set<K>
内部包含类型 const K
的元素 - 它们是不可修改的并且 std::remove_if
需要对象可移动分配。
你必须使用循环:
for (auto it = words.begin(); it != words.end(); /* nothing */)
{
if (std::abs(main_word.length() - it->length()) > 4) {
it = words.erase(it);
}
else {
++it;
}
}