下面来讲解一下qSort() qCopy()qFill()的用法。
qSort()包含在<QtAlgorithms>中,其中主要包含有
http://harmattan-dev.nokia.com/docs/library/html/qt4/qtalgorithms.html#qSort
1.void qSort ( RandomAccessIterator.begin(), RandomAccessIterator.end() ) 2.void qSort ( RandomAccessIterator.begin(), RandomAccessIterator.end(), LessThan lessThan ) 3.void qSort ( Container ; container )
3的函数相当与第一个函数 container.begin(),container.end().
第一个函数特殊之处在于,可以指定是哪几个index的数列之间进行排序。例如 qSort(container.begin()+1,container.end()-1);其中container is a certain container such as list Linklst ……..上个函数的就是排序第二个到倒数第二个之间的数据。
另外在QtAlgorithms中还有一个powerful的函数那就是qCopy()
OutputIterator qCopy ( RandomAccessIterator.begin(), RandomAccessIterator.end(), OutputIterator,begin2() )
其中input 与 output container 可以不同也可以相同,但是如果相同的话,可以能会覆盖原有的数据。例如我要对QStringList中的几个数据排序,作为输入容器,输出容器可以选择QVector
vect qCopy ( List.begin(), List.end(), vector.begin() +n) 最后vector使用foreach(QString item,vector)输出,前n个数据为空,后面为拷贝过来等的数据。
void qFill ( ForwardIterator begin, ForwardIterator end, const T & value )
可以在一个特定的容器填充某个值。
QStringList list; list <<"one" <<"two" << "three"; qFill(list.begin(), list.end(), "eleven"); // list: [ "eleven", "eleven", "eleven" ] qFill(list.begin() + 1, list.end(), "six"); // list: [ "eleven", "six", "six" ]
InputIterator qFind ( InputIterator.begin, InputIterator.end, const T value )
这个函数在特定的容器内搜说value ,对于特定的类型,要重载=符号这样才可以进行比较。
QStringList list; list << "one" << "two" << "three"; QStringList::iterator i1 = qFind(list.begin(), list.end(), "two"); // i1 == list.begin() + 1返回当前指针。 QStringList::iterator i2 = qFind(list.begin(), list.end(), "seventy"); // i2 == list.end()找不到就会返回结尾指针。