Qlist
Qlist<t> is a QT generic container class. It stores a sequence of values and provides an indexed-based approach to data access and quick Insert and delete operations.
Qlist<t>, Qlinkedlist<t>, and qvector<t> provide similar functionality, here are some overviews:
1. In most cases, it is best to use qlist. Its index-based API is more convenient than the Qlinkedlist iterator-based API. The way it stores data also makes it faster than qvector access to data. And it has fewer code extensions in the executable file.
2. A real linked list is required to ensure that the time complexity of inserting data in the middle is constant, using an iterator to save the element, which can be used qlinkedlist.
3. If you want the elements of the container to be in adjacent memory, you can use Qvector.
Inside the,qlist<t> is an array of pointers to the type T. If T itself is a pointer type or not greater than the basic data type of the pointer, or if T is a Qt shared class,qlist<t> The element is persisted directly in the pointer array. For a linked list of less than 1000 elements, such an array represents a quick insert in the middle and runs an index-based method of saving money. In addition, the prepend () and append () operations are also fast because qlist pre-allocates memory on both ends of its internal array. It is important to note that for non-list items that are larger than the pointer, each append or insert new item needs to allocate memory on the heap, and if such a large number of insert and add operations are required, it is best to select Qvector because Qvector allocates memory on a single heap.
It is important to note that in the life of the list, the internal array is only likely to become larger and impossible to shrink. An internal array can be freed only by a destructor or when a list is assigned to another list by an assignment function.
To make qlist as efficient as possible, its member function does not validate the validity of the input. In addition to IsEmpty (), the other member functions assume that the list is non-null. A member function that uses an index parameter always assumes that the index value is in a valid range. This means that the Qlist function may fail to invoke. If you define qt_no_debug at compile time, this failure will not be captured. If Qt_no_debug is not defined, Q_assert () or q_assert_x () will catch these failures by ejecting the appropriate information.
To avoid failure, call IsEmpty () before calling the other member function. If the member function uses an index parameter, check to see if the index is in a valid range.
Similar to Qvector, Qlist is also used for implicit sharing.
qlist<int>integerlist;
Integerlist.push_back (1);
Integerlist.append (5);
Integerlist.setsharable (FALSE);
Qlist<int>list (integerlist);
List= integerlist;
Integerlist.setsharable (FALSE);
cout<< "&list.at (0):" <<&list.at (0) <<endl;
cout<< "&integerlist.at (0):" <<&integerlist.at (0) <<endl;
Integerlist.setsharable (false); The address that is displayed after canceling the comment is different.
Std::list
Std::list is a sequential container that allows for insert and delete operations at any location, and can be iterated in both directions.
The list container is implemented as a doubly linked list, and a doubly linked list can store its elements in a different and unrelated location. The order in which it is associated is persisted by a link to the preceding element and a link to the following element.
List is similar to Forward_list. The main difference is that Forward_list is a single-linked list, so it can only be iterated forward, so it's smaller and more efficient.
Compared to other basic standard containers (array, vector and deque), list is more efficient in inserting, extracting, moving data, so it is common to use lists, such as sorting algorithms, in dense algorithms.
The biggest drawback oflistSand forward_lists is the lack of direct access to elements by location, compared to other sequential containers, which also require additional memory to keep the linked information.
Qlist and std::list comparison constructors:
Qlist constructor:
Qlist::qlist ()
Qlist::qlist (Constqlist<t> & Other)
List constructor:
Version c++98:
Explicit list (constallocator_type& alloc = Allocator_type ());
Explicit list (size_type N, const value_type& val = value_type (), const allocator_type& alloc = Allocator_type ());
Template <class inputiterator> list (inputiterator first, Inputiterator last,
Const allocator_type& alloc = Allocator_type ());
List (const list& x);
Version c++11:
Explicit list (const allocator_type& alloc = Allocator_type ());
List (size_type N, const value_type& val, const allocator_type& alloc = Allocator_type ());
Template <class inputiterator> list (inputiterator first, Inputiterator last,
Const allocator_type& alloc = Allocator_type ());
List (const list& x);
List (const list& x, const allocator_type& alloc);
List (list&& x);
List (list&& x, const allocator_type& alloc);
List (initializer_list<value_type> il,
Const allocator_type& alloc = Allocator_type ());
The constructors of Qlist are much more than the default constructors and copy constructors. Std::list.
Qlist-specific functions:
void Qlist::append (const T & value) inserts value at the end of the list
void Qlist::append (constqlist<t> & value) Inserts the element of the linked list value at the end of the list
The function actually calls the Qlist<t> & |
operator+= (const qlist<t> & Other) |
BOOL Qlist::contains (const t& value) const Determines whether a linked list contains an element value
int Qlist::count (const t& value) Const Statistics The number of elements in the list with values of Balue
void Qlist::move (int from,int to) moves the value of the index from to the location indexed to
Qlist<qstring> list;
List << "A" << "B" << "C" << "D" << "E" << "F";
List.move (1, 4);
List: ["A", "C", "D", "E", "B", "F"]
Some of the actions for deletion:
void qlist::removeat (int i)
BOOL Qlist::removeone (const T & value)
void Qlist::removefirst ()
void Qlist::removelast ()
T qlist::takeat (int i)
T Qlist::takefirst ()
T Qlist::takelast ()
How to convert to other types of containers:
Qset<t> Qlist::toset () const
Std::list<t> qlist::tostdlist () const
Qvector<t> Qlist::tovector () const
Supported operator Operations:
BOOL Qlist::operator!= (constqlist<t> & other) const
Qlist<t> qlist::operator+ (const qlist<t> & other) const
Qlist<t> & qlist::operator+= (const qlist<t> & Other)
Qlist<t> & qlist::operator+= (const T & value)
Qlist<t> &QList::operator<< (const qlist<t> & Other)
Qlist<t> &QList::operator<< (const T & value)
Qlist<t> & qlist::operator= (const qlist<t> & Other)
BOOL qlist::operator== (constqlist<t> & other) const
T & qlist::operator[] (int i)
Const T & qlist::operator[] (int i) const
Qdatastream & operator<< (Qdatastream & Out, const qlist<t> & list)
Qdatastream & Operator>> (Qdatastream & In, qlist<t> & list)
Std::list-specific functions:
Version splicec++98:
void Splice (iterator position, list& x);
void Splice (iterator position, list& X, iterator i);
void Splice (iterator position, list& X, iterator first, iterator last);
Version c++11:
void Splice (const_iterator position, list& x);
void Splice (const_iterator position, list&& x);
void Splice (const_iterator position, list& x, const_iterator i);
void Splice (const_iterator position, list&& x, const_iterator i);
void Splice (const_iterator position, list& X, Const_iterator First, const_iterator last);
void Splice (const_iterator position, list&& X, Const_iterator First, const_iterator last);
The function is to transfer the elements of the linked list X from the X to the linked list and insert them from the position position. The operation does not call any element's constructor or destructor. Will change the size of both linked lists at the same time.
Std::list<int>mylist1, Mylist2;
std::list<int>::iteratorit;
for (int i=1; i<=4; ++i)
Mylist1.push_back (i);
for (int i=1; i<=3; ++i)
Mylist2.push_back (I*10);
it =mylist1.begin ();
++it;
Mylist1.splice (it, mylist2);
cout<< "Mylist1.size ():" <<mylist1.size () <<endl;
cout<< "Mylist2.size ():" <<mylist2.size () <<endl;
it =mylist1.begin ();
cout<< "Mylist1:";
for (it; it!=mylist1.end (); ++it)
{
cout<<*it<< "";
}
cout<<endl;
it =mylist2.begin ();
cout<< "Mylist2:";
for (it; it!=mylist2.end (); ++it)
{
cout<<*it<< "\ t";
}
Sort operation
void sort ();
Template <class compare>
void sort (Compare comp);
Version 1 is used for the < comparison operation, and version 2 is compared with comp . These two sorts are done with a strict weak sort. The values are stable with the same value, and the relative positions are unchanged after sorting. Element is moved in the container, the entire process does not call any constructors, destructors, or copies of any elements.
Unique operation:
void Unique ();
Template <class binarypredicate>
void Unique (binarypredicate binary_pred);
Version 1 without parameters removes elements other than the first element for each set of identical values, for example, there are successive values of a a A, which deletes the next two a, and the value retains the first A.
Version 2 uses binary_pred as the comparison function, it is important to note that the function will call Binary_pred (*i,* (i-1) for each comparison pair, and if the comparison returns true, I will be removed from the list.
Boolsame_integral_part (double first, double second)
{
return (int (first) ==int (second));
}
Structis_near
{
BOOL operator () (double First, Doublesecond)
{
Return (Fabs (First-second) <5.0);
}
};
Double mydoubles[]={12.15, 2.72, 73.0, 12.77, 3.14,
12.77, 73.35, 72.25, 15.3, 72.25};
Std::list<double> mylist (MYDOUBLES,MYDOUBLES+10);
Mylist.sort ();
Std::list<double>::iterator it =mylist.begin ();
cout<< "after sort () MyList:";
int i = 0;
for (it; it! = Mylist.end (); ++it, ++i)
{
cout<<*it<< "";
if (i = = 5)
{
cout<<endl;
}
}
cout<<endl;
Mylist.unique ();
it = Mylist.begin ();
cout<< "after unique () MyList:";
for (it; it! = Mylist.end (); ++it)
{
cout<<*it<< "";
}
cout<<endl;
Mylist.unique (Same_integral_part);
it = Mylist.begin ();
cout<< "after unique (same_integral_part) MyList:";
for (it; it! = Mylist.end (); ++it)
{
cout<<*it<< "";
}
cout<<endl;
Mylist.unique (Is_near ());
it = Mylist.begin ();
cout<< "after unique (same_integral_part) MyList:";
for (it; it! = Mylist.end (); ++it)
{
cout<<*it<< "";
}
cout<<endl;
Merge operation:
C++98 version:
void merge (list& x);
Template <class compare>
void merge (list& x, Compare comp);
C++11 version:
void merge (list& x);
void merge (list&& x);
Template <class compare>
void merge (list& x, Compare comp);
Template <class compare>
void merge (list&& x, Compare comp);
This function merges the elements of X into the linked list in the appropriate order, provided that the two lists are already sorted.
This action removes elements from the x that are inserted into the list, like splice, which does not invoke constructors or destructors, but simply transfers.
Version 2 specifies a comparison operation function that strictly weakly sorts the elements. Call this function, the list must be already sorted, for no sort of list, you can use splice.
It is also stable for the same value, and its position is the same as the relative position in X.
Boolmycomparison (double first, double second)
{
return (int (first) <int (second));
}
Std::list<double>first, second;
First.push_back (3.1);
First.push_back (2.2);
First.push_back (2.9);
Second.push_back (3.7);
Second.push_back (7.1);
Second.push_back (1.4);
First.sort ();
Second.sort ();
First.merge (second);
(second is now empty)
Second.push_back (2.1);
First.merge (Second,mycomparison);
Std::cout << "After Mergefirst contains:";
For (Std::list<double>::iteratorit=first.begin (); It!=first.end (); ++it)
Std::cout << ' << *it;
Std::cout << ' \ n ';
Std::cout << "After Mergesecond contains size:" <<second.size () <<endl;
If we try to comment out the ordered two lines of code, the failed assertion message pops up. The merge function checks to see if the list is sorted.
First.sort ();
Second.sort ();
Reverse Operation:
C++98 version:
void reverse ();
C++11 version:
void reverse () noexcept;
This function reverses the list of links.
std::list<int>mylist;
for (int i=1; i<10; ++i)
Mylist.push_back (i);
Std::cout << "Beforereverse mylist contains:" <<endl;
For (Std::list<int>::iteratorit=mylist.begin (); It!=mylist.end (); ++it)
Std::cout << ' << *it;
Std::cout << ' \ n ';
Mylist.reverse ();
Std::cout << "After reversemylist contains:" <<endl;
For (Std::list<int>::iteratorit=mylist.begin (); It!=mylist.end (); ++it)
Std::cout << ' << *it;
Std::cout << ' \ n ';
http://blog.csdn.net/hai200501019/article/details/11747475
Comparison of Qlist and Std::list