|
STL (Standard Template Library), namely, the standard template library, an industrial-strength, efficient C ++ library. It is housed in the C ++ standard library (C ++ Standard Library), and is the ANSI / ISO C ++ standard part of the great revolutionary. The library contains a lot of basic data structures and basic algorithms in computer science in the common. For the majority of C ++ programmers it provides an extensible application framework, reflecting the high degree of software reusability.
Experience the STL list
STL class list provided, is a two-way circular linked list class. From every element (node), you can access a front element and a rear element. In STL, list provides a series of member functions, but in reality is the complete list of the basic operations, and other data structures are not as basic operations, but in engineering commonly used functions.
E.g:
int data [6] = {3,5,7,9,2,4};
list < int> lidata (data, data + 6); // list is a template class with < int> showed that the data elements of type int
lidata.push_back (6);
...
When the list is initialized, space applications is 6, stored under the data of the six elements, when the lidata insert the first seven elements of "6", list apply for a new node element, is inserted into the list list, the data storage structure
list used in the program
The following program demonstrates the use of a more complete list of the process. We can see, just use #include in the header (this header provide a statement of class list), list class preset function that is capable of easy to use, and the type of data elements, relying on template mechanism can be used freely.
#include < iostream>
#include < list>
#include < numeric>
#include < algorithm>
using namespace std;
// Create a list of examples of container LISTINT
typedef list < int> LISTINT;
// Create a list of examples of container LISTCHAR
typedef list < char> LISTCHAR;
int main ()
{
// --------------------------
// Handle integer data list containers
// --------------------------
// Create a list object named listOne with LISTINT
LISTINT listOne;
// Declare i as the iterator
LISTINT :: iterator i;
// Add the front face listOne container data
listOne.push_front (2);
listOne.push_front (1);
// Add data from the vessel for listOne
listOne.push_back (3);
listOne.push_back (4);
// In front of the display data listOne backwards
cout << "listOne.begin () --- listOne.end ():" << endl;
for (i = listOne.begin (); i = listOne.end ();! ++ i)
cout << * i << "";
cout << endl;
// Display the data from the back listOne
LISTINT :: reverse_iterator ir;
cout << "listOne.rbegin () --- listOne.rend ():" << endl;
for (ir = listOne.rbegin (); ir = listOne.rend ();! ir ++)
{
cout << * ir << "";
}
cout << endl;
// Use the STL accumulate (cumulative) algorithm
int result = accumulate (listOne.begin (), listOne.end (), 0);
cout << "Sum =" << result << endl;
cout << "------------------" << endl;
// --------------------------
// Handle character data list containers
// --------------------------
// Create a list object named listOne with LISTCHAR
LISTCHAR listTwo;
// Declare i as the iterator
LISTCHAR :: iterator j;
// Add the front face listTwo container data
listTwo.push_front ( 'A');
listTwo.push_front ( 'B');
// From the container to add data for listTwo
listTwo.push_back ( 'x');
listTwo.push_back ( 'y');
// In front of the display data listTwo backwards
cout << "listTwo.begin () --- listTwo.end ():" << endl;
for (j = listTwo.begin (); j = listTwo.end ();! ++ j)
cout << char (* j) << "";
cout << endl;
// Use the STL max_element algorithm for listTwo largest element and display
j = max_element (listTwo.begin (), listTwo.end ());
cout << "The maximum element in listTwo is:" << char (* j) << endl;
return 0;
}
list class member functions
The list provides a rich member functions to achieve a variety of operations on the list. This gives the user the convenience, hidden behind, rigorous software engineering processes manufactured products, to ensure the quality. Therefore, in the development of the actual project, the following member function, or the establishment of feeling it.
assign () assign to the list
back () returns the last element
clear () Removes all elements
empty () if the list is empty, it returns true
end () returns the end iterator
erase () removes an element
front () returns the first element
get_allocator () returns the list of Configurator
insert () to insert an element in the list
max_size () returns the maximum number of elements in the list can hold
merge () merge two list
pop_back () to delete the last element
pop_front () removes the first element
push_back () at the end of the list to add an element
push_front () in the list of add an element to the head
rbegin () returns a pointer to the first element of a reverse iterator
remove () removes elements from the list
remove_if () to remove elements specified conditions
rend () points to end list reverse iterator
resize () changes the size of the list of
reverse () reverse elements of the list
size () Returns the number of elements in the list
sort () to sort the list
splice () merge two list
swap () swaps two list
unique () to delete duplicate elements in list
With STL, learn what data structure to do?
Oh, STL so well defined data storage structure became categories, including basic operations and frequently used functions are implemented by member functions, know the interface, you're done, every minute thing.
Yes, the problem came. There are so convenient "infrastructure", a lot of effort to learn what data structures class?
First, as a professional, you may be doing the "bottom", and the development of better stuff than the STL, you can have this ideal, this demand has been;
Second, the socket will read the manual writing code is the code slave, knows its internal round after round, to be able to navigate through the current library, you can use a good infrastructure, produce a truly high-quality products. Said here know that not every line of code to the nuanced and clear, but its internal principle of "feeling", which is the professional should have. Expert to use something, you have to become experts.
Third, again full of tools, there is always imperfect, when faced with a demand, there is no infrastructure, only the own making. No data structure skills, find someone else made it. Easy to find someone else, but the problem is the key when you withdraw, the withdrawal go?
Fourth, the data structure is professional knowledge, not just to work, or to guide thinking. Even if only that ability, the most important in the end is the hand or brain is more important, is not self-evident.
On the way we know these know why they are, progressive bar. Based data structure, practical STL, can have.
Just just now, in answering a student district, he said "content too much too complicated." I replied: "Is there any way to life is to struggle?." When crossed the mountain, turn back, but only a small hill, because you're already in high places. |
|
|
|