[問題] qt的list容器可以作條件count嗎

看板C_and_CPP作者 (chec)時間7年前 (2017/02/22 11:00), 編輯推噓1(104)
留言5則, 2人參與, 最新討論串1/1
開發平台(Platform): (Ex: Win10, Linux, ...) win7 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) vs2015 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) qt 5.7 問題(Question): 請問qt的list容器可以做條件count嗎 例如我有一個 mylist[5] = {5, 7, 1, 2, 4}; 想要找出這個mylist裡面大於5的數字 python 可以寫成 newList = [i for i in mylist if i>=5] newList.count(); 餵入的資料(Input): 請問Qt的容器裡面有類似的寫法嗎 還是只能這樣 QList<int> mylist; mylist << 5 << 7 << 1 << 2 <<4; QList<int> newList for(int i = 0; i < list.size(); ++i) { if(mylist[i]>=5){ newList.append(mylist[i]); } } qDebug()<< newList.count(); 預期的正確結果(Expected Output): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.124.46.66 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1487732422.A.764.html

02/22 11:40, , 1F
用 c++ 函式庫的 count_if + c++11 的 lambda 就行
02/22 11:40, 1F

02/22 11:42, , 2F
QList 有遵循 STL API http://d.pr/l1hO
02/22 11:42, 2F

02/22 11:42, , 3F
啊輸給樓上了 XD
02/22 11:42, 3F

02/22 11:45, , 4F
差點就輸了XD 剛剛我在測試 也把 code 貼上來吧
02/22 11:45, 4F

02/22 11:50, , 5F
我有用到 c++14 的功能就是了https://goo.gl/CRt4V0
02/22 11:50, 5F
文章代碼(AID): #1OhFx6Ta (C_and_CPP)