Qt - can't make lambda work

I have the following function in which I want to cut my std::set<QString> words from words longer / shorter than main_word 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()); } 

When building, I get the following error:

 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); ^ 

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

+2
source share
1 answer

You cannot use remove-if idiom erasure on sets because set<K> internally contains elements of type const K - they are not modifiable and std::remove_if requires the objects to be MoveAssignable.

You will need to use a loop:

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

Source: https://habr.com/ru/post/1212152/


All Articles