1 5 5.1 Qt (Generic Containers) Java STL <QtAlgorithms> STL (Generic Algorithms) QList<T>, QLinkedList<T>, QVector<T>, QStack<T>, QQueue<T> QMap<Key, T>, QMultiMap<Key, T>. QHash<Key, T>, QMultiHash<Key, T> QSet<T> 5.1.1 QList<T> T QList QList::append() QList::prepend() QList::insert() QList QLinkedList<T> QList QList QList QLinkedList QVector<T> QStack<T> QVector (LIFO) QVector push(), pop(), top() QQueue<T> QList (FIFO) QList enqueue(), dequeue(), head()
2 5 5.1.2 QMap<Key, T> Key T ( ) 1 1 QMap QHash QMultiMap<Key, T> QMap 1 QHash<Key, T> QMap API QHash QMultiHash<Key, T> QHash QSet<T> 5.1.3 QByteArray QString QByteArray char * QString Unicode QString 16 QChar QChar Unicode 4.0 5.2 QList QMap Qt 5.2.1 QList void QList::append ( const T & value ) QList & QList::operator<< ( const T & value ) QList & QList::operator<< ( const QList & other ) append() operator<< operator<< T QList::value ( int i ) const T QList::value ( int i, const T & defaultvalue ) const i i i at() T & QList::operator[] ( int i )
5.2 3 const T & QList::operator[] ( int i ) const const T & QList::at ( int i ) const i i (0 <= i < size()) int QList::size () const bool QList::isEmpty () const void QList::clear () size() 1 isempty() clear() QDataStream & operator<< ( QDataStream & out, const QList<T> & list ) QDataStream & operator>> ( QDataStream & in, QList<T> & list 5.2.2 QMap iterator QMap::insert ( const Key & key, const T & value ) iterator QMap::insertMulti ( const Key & key, const T & value ) key value insert() *1 insertmulti() iterator STL const T QMap::value ( const Key & key ) const const T QMap::value ( const Key & key, const T & defaultvalue ) const T & QMap::operator[] ( const Key & key ) const T QMap::operator[] ( const Key & key ) const values() int QMap::size () const bool QMap::isEmpty () const void QMap::clear () size() isempty() 1 clear() *1 insertmulti()
4 5 QDataStream & operator<< ( QDataStream & out, const QMap<Key, T> & map ) QDataStream & operator>> ( QDataStream & in, QMap<Key, T> & map ) QList<Key> QMap::keys () const QList<Key> QMap::keys ( const T & value ) const QList<T> QMap::values () const QList<T> QMap::values ( const Key & key ) const keys() QList<Key> values() QList<T> insertmulti() 5.3 Qt 5.3.1 QList<T> O(1) O(n) O(log n) O(log n) QLinkedList<T> O(n) O(1) O(1) O(1) QVector<T> O(1) O(n) O(n) O(log n) 5.1 5.3.2 QMap<Key, T> O(log n) O(log n) O(log n) O(log n) QHash<Key, T> O(1) O(n) O(log n) O(n) QSet<Key> O(1) O(n) O(log n) O(n) 5.2
5.4 Java 5 5.4 Java Qt 4 Java Java Qt STL QList<T>, QQueue<T> QListIterator<T> QMutableListIterator<T> QLinkedList<T> QLinkedListIterator<T> QMutableLinkedListIterator<T> QVector<T>, QStack<T> QVectorIterator<T> QMutableVectorIterator<T> QSet<T> QSetIterator<T> N/A QMap<Key, T>, QMultiMap<Key, T> QMapIterator<Key, T> QMutableMapIterator<Key, T> QHash<Key, T>, QMultiHash<Key, T> QHashIterator<Key, T> QMutableHashIterator<Key, T> 5.3 Java Java next() previous() 5.4.1 QListIterator QList<T>, QLinkedList<T>, QVector<T>, QSet<T> QListIterator const T & QListIterator::next () const T & QListIterator::previous () void QListIterator::toFront () ( ) void QListIterator::toBack ()
6 5 ( ) bool QListIterator::hasNext () const bool QListIterator::hasPrevious () const const T & QListIterator::peekNext () const const T & QListIterator::peekPrevious () const bool QListIterator::findNext ( const T & value ) bool QListIterator::findPrevious ( const T & value ) QListIterator 1 #include <QtCore> 2 3 int main(int argc, char *argv[]) 4 { 5 QList<int> list; 6 list << 1 << 2 << 3; 7 8 QListIterator<int> i(list); 9 i.tofront(); // 10 while (i.hasnext()) { 11 qdebug() << i.next(); 12 }
5.4 Java 7 13 14 i.toback(); 15 while (i.hasprevious()) { 16 qdebug() << i.previous(); 17 } 18 19 return 0; 20 } 5,6 int 8 9 12 14 17 STL 5.4.2 QMutableListIterator QListIterator QMutableListIterator QMutableListIterator QListIterator void QMutableListIterator::remove () void QMutableListIterator::setValue ( const T & value ) const 100 remove(). 1 QMutableListIterator<int> i(list); 2 3 while(i.hasnext()){ 4 if(qabs<int>(i.next()) >= 100){ 5 i.remove(); 6 } 7 } -99 99 1 while(i.hasnext()){ 2 i.setvalue(qbound<int>(-99, i.next(), 99)); 3 }
8 5 5.4.3 QMapIterator QMapIterator QListIterator next(), previous(), tofront(), toback(), hasnext(), hasprevious(), peeknext(), peekprevious(), findnext(), findprevious() next(), previous(), peeknext(), peekprevious() key() value() QMap<T> QHash<T> QMapIterator QMap 1 #include <QtCore> 2 3 int main(int argc, char *argv[]) 4 { 5 QMap<QString, QString> map; 6 map.insert("japan", QString::fromLocal8Bit(" ")); 7 map.insert("norway", QString::fromLocal8Bit(" ")); 8 9 QMapIterator<QString, QString> i(map); 10 while (i.hasnext()) { 11 qdebug() << i.peeknext().key() << i.peeknext().value(); 12 i.next(); 13 } 14 15 return 0; 16 } 10 13 while(i.hasnext()){ QMap<QString, QString>::const_iterator ci = i.next(); qdebug() << ci.key() << ci.value(); } while(i.hasnext()){ qdebug() << i.peeknext().key() << i.next().value(); // } C++ peeknext() next()
5.5 STL 9 5.4.4 QMutableMapIterator QMutableListIterator QListIterator QMutableMapIterator QMapIterator remove(), setvalue() 1 #include <QtCore> 2 3 int main(int argc, char *argv[]) 4 { 5 QMap<QString, QString> map; 6 map.insert("japan", "Tokyo"); 7 map.insert("norway", "Oslo"); 8 9 QMutableMapIterator<QString, QString> i(map); 10 while (i.hasnext()) { 11 i.setvalue(i.next().value().toupper()); 12 qdebug() << i.peekprevious().value(); 13 } 14 15 return 0; 16 } 5.5 STL Qt Java STL STL Java Qt STL STL
10 5 QList<T>, QQueue<T> QList<T>::const_iterator QList<T>::iterator QLinkedList<T> QLinkedList<T>::const_iterator QLinkedList<T>::iterator QVector<T>, QStack<T> QVector<T>::const_iterator QVector<T>::iterator QSet<T> QSet<T>::const_iterator N/A QMap<Key, T>, QMultiMap<Key, T> QMap<Key, T>::const_iterator QMap<Key, T>::iterator QHash<Key, T>, QMultiHash<Key, T> QHash<Key, T>::const_iterator QHash<Key, T>::iterator 5.4 STL Java STL begin() end() *2 end() STL ++ * QList::const_iterator 1 #include <QtCore> 2 3 int main(int argc, char *argv[]) 4 { 5 QList<int> list; 6 list << 1 << 2 << 3; 7 8 QList<int>::const_iterator i; 9 i = list.begin(); 10 while (i!= list.end()) { 11 qdebug() << *i++; 12 } 13 14 i = list.end(); 15 while (i!= list.begin()) { 16 qdebug() << *--i; 17 } *2 past-the-end [begin, end) begin end
5.6 foreach 11 18 19 return 0; 20 } 5.6 foreach foreach Qt C++ Qt C++ foreach foreach (variable, container) statement; foreach QList<int> 1 #include <QtCore> 2 3 int main(int argc, char *argv[]) 4 { 5 QList<int> list; 6 list << 300 << 400 << 250 << 600; 7 8 int sum = 0; 9 foreach (int n, list) { 10 sum += n; 11 } 12 qdebug("sum: %d", sum); 13 14 return 0; 15 } keys() foreach 1 #include <QtCore> 2 3 int main(int argc, char *argv[]) 4 { 5 QMap<QString, QString> map; 6 map.insert("japan", QString::fromLocal8Bit(" ")); 7 map.insert("norway", QString::fromLocal8Bit(" ")); 8 9 foreach (QString s, map.keys()) {
12 5 10 qdebug() << s << map.value(s); 11 } 12 13 return 0; 14 } 5.7 Qt <QtAlgorithms> Qt STL <algorithm> <QtAlgorithms> STL <algorithm> STL 5.7.1 STL STL 5 STL 5 2 ==!= * ++ Qt * ++ Qt const Qt const -- Qt const
5.7 13 i += n i -= n i + n n + i i - n i - j i[n] i < j i n i n i n i n i j *(i + n) j i QList, QLinkedList, QVector const 5.7.2 <QtAlgorithm> InputIterator OutputIterator ForwardIterator BiIterator RandomAccessIterator InputIterator qfind ( InputIterator begin, InputIterator end, const T & value ) [begin, end) value value end RandomAccessIterator qbinaryfind ( RandomAccessIterator begin, RandomAccessIterator end, const T & value ) RandomAccessIterator qlowerbound ( RandomAccessIterator begin, RandomAccessIterator end, const T & value ) RandomAccessIterator qupperbound ( RandomAccessIterator begin, RandomAccessIterator end, const T & value ) [begin, end) value value end [begin, end) qbinaryfind() qlowerbound() qupperbound() value void qsort ( BiIterator begin, BiIterator end ) void qsort ( BiIterator begin, BiIterator end, LessThan lessthan )
14 5 void qsort ( Container & container ) void qstablesort ( BiIterator begin, BiIterator end ) void qstablesort ( BiIterator begin, BiIterator end, LessThan lessthan ) void qstablesort ( Container & container ) qsort() [begin, end) lessthan operator<() container qsort(container.begin(), container.end()) qstablesort() qsort() 1 class caseinsensitivelessthan 2 { 3 public: 4 inline bool operator()(const QString &s1, const QString &s2) const 5 { 6 return s1.tolower() < s2.tolower(); 7 } 8 }; 9... 10 QList<QString> list; 11 list << "alpha" << "BETA" << "gamma" << "DELTA"; 12 qsort(list.begin(), list.end(), caseinsensitivelessthan()); 13... qgreater<t>() qgreater<t>() <QtAlgorithm> 12 qsort(list.begin(), list.end(), qgreater<qstring>()); STL <functional> STL 12 qsort(list.begin(), list.end(), std::greater<qstring>()); OutputIterator qcopy ( InputIterator begin1, InputIterator end1, OutputIterator begin2 ) BiIterator2 qcopybackward ( BiIterator1 begin1, BiIterator1 end1, BiIterator2 end2 ) qcopy() [begin1, end1) [begin2,...) qcopybackward() [begin1, end1) [..., end2) bool qequal ( InputIterator1 begin1, InputIterator1 end1, InputIterator2 begin2 ) [begin1, end1) [begin2,...)
5.8 15 void qcount ( InputIterator begin, InputIterator end, const T & value, Size & n ) [begin, end) value n void qfill ( ForwardIterator begin, ForwardIterator end, const T & value ) [begin, end) value void qdeleteall ( ForwardIterator begin, ForwardIterator end ) void qdeleteall ( const Container & container ) [begin, end) C++ delete container qdeleteall(container.begin(), container.end()) void qswap ( T & var1, T & var2 ) var1 var2 5.8 5.8.1 Qt Qt (Implicit Data Sharing) Qt delete qdeleteall() clear() 1 #include <QtCore> 2 3 int main(void) 4 { 5 QList<QString*> list; 6 list.append(new QString("alpha")); 7 list.append(new QString("beta")); 8 list.append(new QString("gamma")); 9 10 QListIterator<QString*> i(list); 11 while (i.hasnext()) { 12 qdebug() << *i.next();
16 5 13 } 14 15 qdeleteall(list); // 16 list.clear(); // i 17 18 return 0; 19 } 5.8.2 Qt QBitArray QBitmap QBrush QByteArray QCursor QDir QDomImplementation QDomNodeList QDomNode QDomNamedNodeMap QFileInfo QFont QFontInfo QFontMetrics QFontMetricsF QGLColormap QHash QIcon QImage QKeySequence QLinkedList QList QLocale QMap QMovie QMultiHash QMultiMap QPalette QPen QPicture QPixmap QPointArray QQueue QRegExp QRegion QSqlField QSqlQuery QSqlRecord QStack QString QStringList QTextCursor QTextDocumentFragment QTextFormat QUrl QVariant QVector QX11Info 1 0 (shallow copy) (deep copy) 1 QString s1, s2;
5.8 17 2 s1 = "hello, "; // 1 3 s2 = s1; // 2 4 5 s2.append("world."); // 1 CPU : STL 5.8.3 QList<T>, QByteArray, QString ( Qt 4.0.0 ) 1. IP32 *3 QList<T> 20 QByteArray QString 16 2. a 64 8 24, 32, 40, 48, 56, 64 b 64 4096 2 64, 128, 256, 512, 1024, 2048, 4096 c 4096 4096 4096, 8192, 12288, 16384, 20480, 24576, 28672,... QVector<T> memcpy() 1.5 QHash<T> 2 QVector<T>, QHash<Key, T>, QSet<T>, QString, QByteArray 3 capacity() (QHash QSet ) *3 IP32 int 32
18 5 reserve(size) size squeeze() reserve() squeeze()