Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

  • Qt 4.8
  • Qt3SupportLight
  • Q3PtrList

Q3PtrList Class

TheQ3PtrList class is a template class that provides a list.More...

Header:#include <Q3PtrList>
Inherits:Q3PtrCollection
Inherited By:

Q3StrList

Public Functions

Q3PtrList()
Q3PtrList(const Q3PtrList<type> & list)
~Q3PtrList()
voidappend(const type * item)
type *at(uint index)
intat() const
uintcontains(const type * item) const
uintcontainsRef(const type * item) const
type *current() const
Q3LNode *currentNode() const
intfind(const type * item)
intfindNext(const type * item)
intfindNextRef(const type * item)
intfindRef(const type * item)
type *first()
type *getFirst() const
type *getLast() const
voidinSort(const type * item)
boolinsert(uint index, const type * item)
boolisEmpty() const
type *last()
type *next()
voidprepend(const type * item)
type *prev()
boolremove(uint index)
boolremove()
boolremove(const type * item)
boolremoveFirst()
boolremoveLast()
voidremoveNode(Q3LNode * node)
boolremoveRef(const type * item)
boolreplace(uint index, const type * item)
voidsort()
type *take(uint index)
type *take()
type *takeNode(Q3LNode * node)
voidtoVector(Q3GVector * vec) const
booloperator!=(const Q3PtrList<type> & list) const
Q3PtrList<type> &operator=(const Q3PtrList<type> & list)
booloperator==(const Q3PtrList<type> & list) const

Reimplemented Public Functions

virtual voidclear()
virtual uintcount() const

Protected Functions

virtual intcompareItems(Q3PtrCollection::Item item1, Q3PtrCollection::Item item2)
virtual QDataStream &read(QDataStream & s, Q3PtrCollection::Item & item)
virtual QDataStream &write(QDataStream & s, Q3PtrCollection::Item item) const

Detailed Description

TheQ3PtrList class is a template class that provides a list.

Q3ValueList is an STL-compatible alternative to this class.

Define a template instanceQ3PtrList<X> to create a list that operates on pointers to X (X*).

The list class is indexable and has acurrent index and acurrent item. The first item corresponds to index position 0. The current index is -1 if the current item is 0.

Items are inserted withprepend(),insert() orappend(). Items are removed withremove(),removeRef(),removeFirst() andremoveLast(). You can search for an item usingfind(),findNext(),findRef() orfindNextRef(). The list can be sorted withsort(). You can count the number of occurrences of an item withcontains() orcontainsRef(). You can get a pointer to the current item withcurrent(), to an item at a particular index position in the list withat() or to the first or last item withgetFirst() andgetLast(). You can also iterate over the list withfirst(),last(),next() andprev() (which all updatecurrent()). The list's deletion property is set withsetAutoDelete().

Example:

class Employee{public:    Employee() : sn(0 ) { }    Employee(constQString& forename,constQString& surname,int salary )        : fn( forename ), sn( surname ), sal( salary )    { }void setSalary(int salary ) { sal= salary; }QString forename()const {return fn; }QString surname()const {return sn; }int salary()const {return sal; }private:QString fn;QString sn;int sal;};Q3PtrList<Employee> list;list.setAutoDelete( TRUE );// the list owns the objectslist.append(new Employee("John","Doe",50000) );list.append(new Employee("Jane","Williams",80000) );list.append(new Employee("Tom","Jones",60000) );Employee*employee;for ( employee= list.first(); employee; employee= list.next() )    cout<< employee->surname().latin1()<<", "<<            employee->forename().latin1()<<" earns "<<            employee->salary()<< endl;cout<< endl;// very inefficient for big listsfor (uint i=0; i< list.count();++i )if ( list.at(i) )        cout<< list.at( i )->surname().latin1()<< endl;

The output is

Doe, John earns50000Williams, Jane earns80000Jones, Tom earns60000DoeWilliamsJones

Q3PtrList has several member functions for traversing the list, but using aQ3PtrListIterator can be more practical. Multiple list iterators may traverse the same list, independently of each other and of the current list item.

In the example above we make the callsetAutoDelete(true). Enabling auto-deletion tells the list to delete items that are removed. The default is to not delete items when they are removed but this would cause a memory leak in the example because there are no other references to the list items.

When inserting an item into a list only the pointer is copied, not the item itself, i.e. a shallow copy. It is possible to make the list copy all of the item's data (deep copy) when an item is inserted.insert(),inSort() andappend() call the virtual functionQ3PtrCollection::newItem() for the item to be inserted. Inherit a list and reimplementnewItem() to have deep copies.

When removing an item from a list, the virtual functionQ3PtrCollection::deleteItem() is called.Q3PtrList's default implementation is to delete the item if auto-deletion is enabled.

The virtual functioncompareItems() can be reimplemented to compare two list items. This function is called from all list functions that need to compare list items, for instance remove(const type*). If you only want to deal with pointers, there are functions that compare pointers instead, for instanceremoveRef(const type*). These functions are somewhat faster than those that callcompareItems().

List items are stored asvoid* in an internal Q3LNode, which also holds pointers to the next and previous list items. The functionscurrentNode(),removeNode(), andtakeNode() operate directly on the Q3LNode, but they should be used with care. The data component of the node is available through Q3LNode's getData() function.

TheQ3StrList class is a list ofchar*. It reimplementsnewItem(),deleteItem() andcompareItems(). (But seeQStringList for a list of Unicode QStrings.)

See alsoQ3PtrListIterator.

Member Function Documentation

Q3PtrList::Q3PtrList()

Constructs an empty list.

Q3PtrList::Q3PtrList(constQ3PtrList<type> & list)

Constructs a copy oflist.

Each item inlist isappended to this list. Only the pointers are copied (shallow copy).

Q3PtrList::~Q3PtrList()

Removes all items from the list and destroys the list.

All list iterators that access this list will be reset.

See alsosetAutoDelete().

void Q3PtrList::append(consttype * item)

Inserts theitem at the end of the list.

The inserted item becomes the current list item. This is equivalent toinsert( count(), item ).

item must not be 0.

See alsoinsert(),current(), andprepend().

type * Q3PtrList::at(uint index)

Returns a pointer to the item at positionindex in the list, or 0 if the index is out of range.

Sets the current list item to this item ifindex is valid. The valid range is0..(count() - 1) inclusive.

This function is very efficient. It starts scanning from the first item, last item, or current item, whichever is closest toindex.

See alsocurrent().

int Q3PtrList::at() const

This is an overloaded function.

Returns the index of the current list item. The returned value is -1 if the current item is 0.

See alsocurrent().

[virtual]void Q3PtrList::clear()

Reimplemented fromQ3PtrCollection::clear().

Removes all items from the list.

The removed items are deleted ifauto-deletion is enabled.

All list iterators that access this list will be reset.

See alsoremove(),take(), andsetAutoDelete().

[virtual protected]int Q3PtrList::compareItems(Q3PtrCollection::Item item1,Q3PtrCollection::Item item2)

This virtual function compares two list items.

Returns:

  • zero ifitem1 ==item2
  • nonzero ifitem1 !=item2

This function returnsint rather thanbool so that reimplementations can return three values and use it to sort by:

  • 0 ifitem1 ==item2
  • > 0 (positive integer) ifitem1 >item2
  • < 0 (negative integer) ifitem1 <item2

inSort() requires that compareItems() is implemented as described here.

This function should not modify the list because some const functions call compareItems().

The default implementation compares the pointers.

uint Q3PtrList::contains(consttype * item) const

Returns the number of occurrences ofitem in the list.

ThecompareItems() function is called when looking for theitem in the list. IfcompareItems() is not reimplemented, it is more efficient to callcontainsRef().

This function does not affect the current list item.

See alsocontainsRef() andcompareItems().

uint Q3PtrList::containsRef(consttype * item) const

Returns the number of occurrences ofitem in the list.

Calling this function is much faster thancontains() becausecontains() comparesitem with each list item usingcompareItems(), whereas his function only compares the pointers.

This function does not affect the current list item.

See alsocontains().

[virtual]uint Q3PtrList::count() const

Reimplemented fromQ3PtrCollection::count().

Returns the number of items in the list.

See alsoisEmpty().

type * Q3PtrList::current() const

Returns a pointer to the current list item. The current item may be 0 (implies that the current index is -1).

See alsoat().

Q3LNode * Q3PtrList::currentNode() const

Returns a pointer to the current list node.

The node can be kept and removed later usingremoveNode(). The advantage is that the item can be removed directly without searching the list.

Warning: Do not call this function unless you are an expert.

See alsoremoveNode(),takeNode(), andcurrent().

int Q3PtrList::find(consttype * item)

Finds the first occurrence ofitem in the list.

If the item is found, the list sets the current item to point to the found item and returns the index of this item. If the item is not found, the list sets the current item to 0, the current index to -1, and returns -1.

ThecompareItems() function is called when searching for the item in the list. IfcompareItems() is not reimplemented, it is more efficient to callfindRef().

See alsofindNext(),findRef(),compareItems(), andcurrent().

int Q3PtrList::findNext(consttype * item)

Finds the next occurrence ofitem in the list, starting from the current list item.

If the item is found, the list sets the current item to point to the found item and returns the index of this item. If the item is not found, the list sets the current item to 0, the current index to -1, and returns -1.

ThecompareItems() function is called when searching for the item in the list. IfcompareItems() is not reimplemented, it is more efficient to callfindNextRef().

See alsofind(),findNextRef(),compareItems(), andcurrent().

int Q3PtrList::findNextRef(consttype * item)

Finds the next occurrence ofitem in the list, starting from the current list item.

If the item is found, the list sets the current item to point to the found item and returns the index of this item. If the item is not found, the list sets the current item to 0, the current index to -1, and returns -1.

Calling this function is much faster thanfindNext() becausefindNext() comparesitem with each list item usingcompareItems(), whereas this function only compares the pointers.

See alsofindRef(),findNext(), andcurrent().

int Q3PtrList::findRef(consttype * item)

Finds the first occurrence ofitem in the list.

If the item is found, the list sets the current item to point to the found item and returns the index of this item. If the item is not found, the list sets the current item to 0, the current index to -1, and returns -1.

Calling this function is much faster thanfind() becausefind() comparesitem with each list item usingcompareItems(), whereas this function only compares the pointers.

See alsofindNextRef(),find(), andcurrent().

type * Q3PtrList::first()

Returns a pointer to the first item in the list and makes this the current list item; returns 0 if the list is empty.

See alsogetFirst(),last(),next(),prev(), andcurrent().

type * Q3PtrList::getFirst() const

Returns a pointer to the first item in the list, or 0 if the list is empty.

This function does not affect the current list item.

See alsofirst() andgetLast().

type * Q3PtrList::getLast() const

Returns a pointer to the last item in the list, or 0 if the list is empty.

This function does not affect the current list item.

See alsolast() andgetFirst().

void Q3PtrList::inSort(consttype * item)

Inserts theitem at its sorted position in the list.

The sort order depends on the virtualcompareItems() function. All items must be inserted with inSort() to maintain the sorting order.

The inserted item becomes the current list item.

item must not be 0.

Warning: Using inSort() is slow. An alternative, especially if you have lots of items, is to simplyappend() orinsert() them and then usesort(). inSort() takes up to O(n) compares. That means inserting n items in your list will need O(n^2) compares whereassort() only needs O(n*log n) for the same task. So use inSort() only if you already have a presorted list and want to insert just a few additional items.

See alsoinsert(),compareItems(),current(), andsort().

bool Q3PtrList::insert(uint index, consttype * item)

Inserts theitem at positionindex in the list.

Returns TRUE if successful, i.e. ifindex is in range; otherwise returns FALSE. The valid range is 0 tocount() (inclusively). The item is appended ifindex ==count().

The inserted item becomes the current list item.

item must not be 0.

See alsoappend(),current(), andreplace().

bool Q3PtrList::isEmpty() const

Returns TRUE if the list is empty; otherwise returns FALSE.

See alsocount().

type * Q3PtrList::last()

Returns a pointer to the last item in the list and makes this the current list item; returns 0 if the list is empty.

See alsogetLast(),first(),next(),prev(), andcurrent().

type * Q3PtrList::next()

Returns a pointer to the item succeeding the current item. Returns 0 if the current item is 0 or equal to the last item.

Makes the succeeding item current. If the current item before this function call was the last item, the current item will be set to 0. If the current item was 0, this function does nothing.

See alsofirst(),last(),prev(), andcurrent().

void Q3PtrList::prepend(consttype * item)

Inserts theitem at the start of the list.

The inserted item becomes the current list item. This is equivalent toinsert( 0, item ).

item must not be 0.

See alsoappend(),insert(), andcurrent().

type * Q3PtrList::prev()

Returns a pointer to the item preceding the current item. Returns 0 if the current item is 0 or equal to the first item.

Makes the preceding item current. If the current item before this function call was the first item, the current item will be set to 0. If the current item was 0, this function does nothing.

See alsofirst(),last(),next(), andcurrent().

[virtual protected]QDataStream & Q3PtrList::read(QDataStream & s,Q3PtrCollection::Item & item)

Reads a list item from the streams and returns a reference to the stream.

The default implementation setsitem to 0.

See alsowrite().

bool Q3PtrList::remove(uint index)

Removes the item at positionindex in the list.

Returns TRUE if successful, i.e. ifindex is in range; otherwise returns FALSE. The valid range is0..(count() - 1) inclusive.

The removed item is deleted ifauto-deletion is enabled.

The item after the removed item becomes the new current list item if the removed item is not the last item in the list. If the last item is removed, the new last item becomes the current item.

All list iterators that refer to the removed item will be set to point to the new current item.

See alsotake(),clear(),setAutoDelete(),current(), andremoveRef().

bool Q3PtrList::remove()

This is an overloaded function.

Removes the current list item.

Returns TRUE if successful, i.e. if the current item isn't 0; otherwise returns FALSE.

The removed item is deleted ifauto-deletion is enabled.

The item after the removed item becomes the new current list item if the removed item is not the last item in the list. If the last item is removed, the new last item becomes the current item. The current item is set to 0 if the list becomes empty.

All list iterators that refer to the removed item will be set to point to the new current item.

See alsotake(),clear(),setAutoDelete(),current(), andremoveRef().

bool Q3PtrList::remove(consttype * item)

This is an overloaded function.

Removes the first occurrence ofitem from the list.

Returns TRUE if successful, i.e. ifitem is in the list; otherwise returns FALSE.

The removed item is deleted ifauto-deletion is enabled.

ThecompareItems() function is called when searching for the item in the list. IfcompareItems() is not reimplemented, it is more efficient to callremoveRef().

Ifitem is NULL then the current item is removed from the list.

The item after the removed item becomes the new current list item if the removed item is not the last item in the list. If the last item is removed, the new last item becomes the current item. The current item is set to 0 if the list becomes empty.

All list iterators that refer to the removed item will be set to point to the new current item.

See alsoremoveRef(),take(),clear(),setAutoDelete(),compareItems(), andcurrent().

bool Q3PtrList::removeFirst()

Removes the first item from the list. Returns TRUE if successful, i.e. if the list isn't empty; otherwise returns FALSE.

The removed item is deleted ifauto-deletion is enabled.

The first item in the list becomes the new current list item. The current item is set to 0 if the list becomes empty.

All list iterators that refer to the removed item will be set to point to the new current item.

See alsoremoveLast(),setAutoDelete(),current(), andremove().

bool Q3PtrList::removeLast()

Removes the last item from the list. Returns TRUE if successful, i.e. if the list isn't empty; otherwise returns FALSE.

The removed item is deleted ifauto-deletion is enabled.

The last item in the list becomes the new current list item. The current item is set to 0 if the list becomes empty.

All list iterators that refer to the removed item will be set to point to the new current item.

See alsoremoveFirst(),setAutoDelete(), andcurrent().

void Q3PtrList::removeNode(Q3LNode * node)

Removes thenode from the list.

This node must exist in the list, otherwise the program may crash.

The removed item is deleted ifauto-deletion is enabled.

The first item in the list will become the new current list item. The current item is set to 0 if the list becomes empty.

All list iterators that refer to the removed item will be set to point to the item succeeding this item or to the preceding item if the removed item was the last item.

Warning: Do not call this function unless you are an expert.

See alsotakeNode(),currentNode(),remove(), andremoveRef().

bool Q3PtrList::removeRef(consttype * item)

Removes the first occurrence ofitem from the list.

Returns TRUE if successful, i.e. ifitem is in the list; otherwise returns FALSE.

The removed item is deleted ifauto-deletion is enabled.

Equivalent to:

if ( list.findRef( item )!=-1 )    list.remove();

The item after the removed item becomes the new current list item if the removed item is not the last item in the list. If the last item is removed, the new last item becomes the current item. The current item is set to 0 if the list becomes empty.

All list iterators that refer to the removed item will be set to point to the new current item.

See alsoremove(),clear(),setAutoDelete(), andcurrent().

bool Q3PtrList::replace(uint index, consttype * item)

Replaces the item at positionindex with the newitem.

Returns TRUE if successful, i.e.index is in the range 0 tocount()-1.

See alsoappend(),current(), andinsert().

void Q3PtrList::sort()

Sorts the list by the result of the virtualcompareItems() function.

The heap sort algorithm is used for sorting. It sorts n items with O(n*log n) comparisons. This is the asymptotic optimal solution of the sorting problem.

If the items in your list support operator<() and operator==(), you might be better off with Q3SortedList because it implements thecompareItems() function for you using these two operators.

See alsoinSort().

type * Q3PtrList::take(uint index)

Takes the item at positionindex out of the list without deleting it (even ifauto-deletion is enabled).

Returns a pointer to the item taken out of the list, or 0 if the index is out of range. The valid range is0..(count() - 1) inclusive.

The item after the removed item becomes the new current list item if the removed item is not the last item in the list. If the last item is removed, the new last item becomes the current item. The current item is set to 0 if the list becomes empty.

All list iterators that refer to the taken item will be set to point to the new current item.

See alsoremove(),clear(), andcurrent().

type * Q3PtrList::take()

This is an overloaded function.

Takes the current item out of the list without deleting it (even ifauto-deletion is enabled).

Returns a pointer to the item taken out of the list, or 0 if the current item is 0.

The item after the removed item becomes the new current list item if the removed item is not the last item in the list. If the last item is removed, the new last item becomes the current item. The current item is set to 0 if the list becomes empty.

All list iterators that refer to the taken item will be set to point to the new current item.

See alsoremove(),clear(), andcurrent().

type * Q3PtrList::takeNode(Q3LNode * node)

Takes thenode out of the list without deleting its item (even ifauto-deletion is enabled). Returns a pointer to the item taken out of the list.

This node must exist in the list, otherwise the program may crash.

The first item in the list becomes the new current list item.

All list iterators that refer to the taken item will be set to point to the item succeeding this item or to the preceding item if the taken item was the last item.

Warning: Do not call this function unless you are an expert.

See alsoremoveNode() andcurrentNode().

void Q3PtrList::toVector(Q3GVector * vec) const

Stores all list items in the vectorvec.

The vector must be of the same item type, otherwise the result will be undefined.

[virtual protected]QDataStream & Q3PtrList::write(QDataStream & s,Q3PtrCollection::Item item) const

Writes a list item,item to the streams and returns a reference to the stream.

The default implementation does nothing.

See alsoread().

bool Q3PtrList::operator!=(constQ3PtrList<type> & list) const

Compares this list withlist. Returns TRUE if the lists contain different data; otherwise returns FALSE.

Q3PtrList<type> & Q3PtrList::operator=(constQ3PtrList<type> & list)

Assignslist to this list and returns a reference to this list.

This list is first cleared and then each item inlist isappended to this list. Only the pointers are copied (shallow copy) unlessnewItem() has been reimplemented.

bool Q3PtrList::operator==(constQ3PtrList<type> & list) const

Compares this list withlist. Returns TRUE if the lists contain the same data; otherwise returns FALSE.

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp