Qt - 不能让lambda工作 [英] Qt - Cannot get lambda to work

查看:610
本文介绍了Qt - 不能让lambda工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能,其中我想修剪我的 std :: set< QString>字从长于/短于 main_word 的字数减去超过4个字符。

I have the following function, in which I want to prune my std::set<QString> words from words longer/shorter than main_word by more than 4 characters.

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());
}

我在构建时遇到以下错误:

I get the following error while building:

d:\qt\tools\mingw48_32\lib\gcc\i686-w64-mingw32\4.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做错了什么?

I'm a bit confused - what am I doing wrong with this lambda?

推荐答案

不能对集合使用erase remove-if idiom,因为 set< K> ; 内部包含 const K 类型的元素 - 它们是不可修改的, std :: remove_if 要求对象为MoveAssignable。

You can't use the erase remove-if idiom on sets - because a set<K> internally contains elements of type const K - they're nonmodifiable and std::remove_if requires the objects to be MoveAssignable.

您必须使用循环:

for (auto it = words.begin(); it != words.end(); /* nothing */)
{
    if (std::abs(main_word.length() - it->length()) > 4) {
        it = words.erase(it);
    }
    else {
        ++it;
    }
}

这篇关于Qt - 不能让lambda工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆