Solved how to get true or false in a single line code on QVector?
-
Hi,
- I have
typedef QPair<QString,QString> group_key;
- I have global
QVector<group_key>checkboxItems;
- I did populate
QVector
with theQPair
objects - In a method, I would like to check an element the
QPair
exists onQVector
, Is there any way to check in a line of code?
code:
void someMethod() { bool checkbox_item_there=QVector.anymethod("string1","string2")// using str1, str2 can we check, Is there any method there? }
- I have
-
-
hi @thippu
from the doc : http://doc.qt.io/qt-5/qvector.html
int i = vector.indexOf("str1");
if (i != -1)
cout << "First occurrence of str1 is at position " << i << endl;not sure this is fully what you need
-
I'd go with a lambda and
std::any_of
:any_of( checkBoyItems.cbegin(), checkBoyItems.cend(), [&str1, &str2]( const QPair< QString, QString >& item ) { return item.first == str1 && item.second == str2; );
But, honestly, why the one line constraint?
-
@jsulm Thank you.
-
But, honestly, why the one line constraint?
Because I would not want the loop to consume more time and I did not know this method.
Thank you all -
@LeLev Thanks.
-
@thippu said in how to get true or false in a single line code on QVector?:
But, honestly, why the one line constraint?
Because I would not want the loop to consume more time and I did not know this method.
Thank you allLines of code are pretty meaningless in relation to app performance: Depending on the situation, writing more lines can in fact improve the over all speed. That solely depends on the compiler.
In fact, as soon as you build your progam in release mode, the code you've written looses virtually all meaning. Debug is a different case entirely.