生活随笔
收集整理的這篇文章主要介紹了
QT学习笔记(十):通用算法示例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
QT學習筆記(十):通用算法示例
std是C++標準庫統一使用的命名空間(namespace)的名稱,C++標準庫中的名字全部都在std這個命名空間中,std也就是英文"standard"(標準)的縮寫。
#include
<QCoreApplication>
#include
<QVector>
#include
<QStringList>
#include
<QDebug>#include
<functional>
#include
<algorithm> int main(int argc
, char *argv
[])
{QCoreApplication
a(argc
, argv
);QStringList list
;list
<< "one" << "two" << "three";qDebug() << QObject
::tr("std::copy算法:");QVector
<QString> vect(3);std
::copy(list
.begin(), list
.end(), vect
.begin());qDebug() << vect
;qDebug() << endl
<< QObject
::tr("std::equal算法:");bool ret1
= std
::equal(list
.begin(), list
.end(), vect
.begin());qDebug() << "euqal: " << ret1
; qDebug() << endl
<< QObject
::tr("std::find算法:");QList
<QString>::iterator i
= std
::find(list
.begin(), list
.end(), "two");qDebug() << *i
; qDebug() << endl
<< QObject
::tr("std::fill算法:");std
::fill(list
.begin(), list
.end(), "eleven");qDebug() << list
; QList
<int> list1
;list1
<< 3 << 3 << 6 << 6 << 6 << 8;qDebug() << endl
<< QObject
::tr("std::count算法:");int countOf6
= std
::count(list1
.begin(), list1
.end(), 6);qDebug() << "countOf6: " << countOf6
; qDebug() << endl
<< QObject
::tr("std::lower_bound算法:");QList
<int>::iterator j
= std
::lower_bound(list1
.begin(), list1
.end(), 5);list1
.insert(j
, 5);qDebug() << list1
; QList
<int> list2
;list2
<< 33 << 12 << 68 << 6 << 12;qDebug() << endl
<< QObject
::tr("std::sort算法:");std
::sort(list2
.begin(), list2
.end());qDebug() << list2
; qDebug() << endl
<< QObject
::tr("std::stable_sort算法:");std
::stable_sort(list2
.begin(), list2
.end());qDebug() << list2
; qDebug() << endl
<< QObject
::tr("std::greater算法:");qSort(list2
.begin(), list2
.end(), std
::greater
<int>());qDebug() << list2
; qDebug() << endl
<< QObject
::tr("std::swap算法:");double pi
= 3.14;double e
= 2.71;std
::swap(pi
, e
); qDebug() << "pi:" << pi
<< "e:" << e
; return a
.exec();
}
運行結果:
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的QT学习笔记(十):通用算法示例的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。