STL data Structure part of the original tutorial, to detailed information please Baidu

Source: Internet
Author: User

Data Structure Section

Data structure, is supposed to be a school of the children's paper for a year, the Internet of things children paper for six months. However, we do not need such meticulous study, in fact, too much knowledge can be used on the line, now let us use a lesson to the base of the data structure.

What is a data structure, and a simple understanding is how it is stored. We have learned the list before, so in fact most of the data structures are modified by the list to be implemented,

The first is the list of doubly linked list , this familiar bar!

Be sure to pay attention to the fact that the containers are similar, learn a back on the brush will be.

Header files are #include <list>

Definition is list <int> L1; Constructs an empty list of L1 of type int

List <int> L1 (3); There are three elements with a value of 0

List <int> L1 (3,1),; There are three elements with a value of 1

We can compare to get started, and array contrast, actually is the same

Int a[100]; this is in the definition array, also the specified type is OK,

1: To find the first element how to find it:

Array direct a "0";

List L1.front (); Note that parentheses must have, in fact, this is a function return value is his first element 2: Find the last element,

Array a "99";

List L1.back ();

3: Access the middle element, this is a bit difficult, to use pointers,

First we want to define pointers:

List<int>::iterator it; This form remembers can be, container < type >::iterator it it = L1.begin (); Returns a pointer to the start position

it = L1.end (); Returns the pointer to the next position in the end position

How to add and remove it??

cin>>a;

L1.push_front (a); add a to chain header

L1.push_back (a); to the tail

L1.   Pop_front (); Delete a header, no parameters

L1.    Pop_back (); Delete one tail, no parameters

L1.clear () Delete all.

L1.erase (it, it+5); Delete one or a region

L1. Remove (4) Delete all 4 in the list

L1. Remove_if (a function) deletes the number that satisfies the condition,

L1. Size () returns the number of elements

L1. Empty () Determines whether NULL is True, NULL is true, otherwise false false.

L1. Reverse () Reverse linked list

L1. Sort () is sorted

L1. Sort (greater<int>) inverse sort

Take a question and practice it and ask for a big value. and sub-decimal values. All the and. To find the length. Required to be implemented within 30 lines.

#include <iostream>

#include <list>

#include <numeric>

using namespace Std;

int main ()

{

list<int>l1;

List<int>::iterator it, it1;

int a,i,j=5;

while (j--)

{

cin>>a;

L1.push_front (a);

}

i = Accumulate (L1.begin (), L1.end (), 0);

L1.sort ();

L1.pop_back ();

L1.pop_front ();

it = L1.begin ();

It1 =--l1.end ();

cout<<*it<< "" <<*it1<< "<<l1.size () <<" "<<i<<endl;

}

list<int>l1;

int a,j=5;

while (j--) {

cin>>a;

L1.push_front (a);

}

L1.sort ();

Cout<<*++l1.begin () << "<<* (--(--l1.end ())) <<" "<<l1.size () <<endl;

Cout<<accumulate (L1.begin (), L1.end (), 0) <<endl;

Sum up. Linked lists are an advantage in addition to random access. The list function is very powerful. Because when you add data and delete data, you don't move large amounts of data like an array. Now that we can understand a structure, let's talk about some of the remaining structures quickly. Mainly by the chain list to solve the problem is generally some large-scale data, because the array can not be stored to use, but the STL has an indefinite long array, but also can replace the function, I hope you see a lot of this list of the introduction, because the other structure actually use the same function.

In my blog There is also an article above is the list of test code, you can directly copy to test each function module

Stack and queue of data structures

Stack this is more common, mainly used to solve the parentheses paired with the PS Nanyang Question 2. Maze, this kind of use of the memory capacity of the stack, in practical use, the state of electronic equipment is generally used to save the stack, often the computer out of trouble when it will say what stack overflow, is such a problem.

Queues are also common, let's say, we go to the cafeteria to eat. On the line, first-out structure.

His homework is to be able to operate on a fair basis. ps:1197: Egg queue

So how do you use such a structure?

Note that they are actually linked lists, but the operation of the numbers is limited.

Stack

Queue

Header file

Stack

Queue

Defined

stack<int> S1;

queue<int>q1;

Determine if empty

S1.empty ()

Q1.empty ()

Find the number of elements

S1.size ()

Q1.size ();

Delete First element

S1.pop ()

Q1.pop ()

Get the first element value

S1.top ();

Q1.front ()

Adding new elements

S1.push ()

Q1.push ()

Returns the tail element value

No

Q1.back ();

The queue is used slightly less, so let's mainly parse, the parentheses match the code

int main ()

{

Char b[10005];

Stack<char> A;

scanf ("%s", b);

int L=strlen (b), p=0;

if (b[0]== ') ' | | b[0]== '] | | b[0]== '} ')

printf ("no\n");

Else

{

for (int i=0;i<l;i++)

{

if (b[i]== ' | | | b[i]== ' [' | | b[i]== ' {') A.push (B[i]);

else if (!a.empty () && (A.top () +1==b[i]| | A.top () +2==b[i])) {

cout<< "" <<a.top () +1<< "" <<a.top () +2<<endl;a.pop ();

Else p=1;//a.top+1 +2 The ASC code difference is 1 2 because () "" {}.

}

if (A.empty () &&p==0) printf ("yes\n");

else printf ("no\n");

}

}

return 0;

}

Map and set these two are less used, so I just describe his nature, there are students in the future to use the knife can go to my blog to view, there are special description.

Map is a mapping that represents a single correspondence, such as a number and an individual. The comparison is similar to the way we use structs to achieve functionality. Feature is the process of automatic sequencing.

Set is the set, any data exists only one, no duplication, and orderly. In the process of adding elements, the order is started. So you can use them when you have a high demand for time and memory.

Vector, which is called vectors, is also an indefinite long array, with the same usages and arrays.

Vector<int> v;

V[100] = "0";

It is mainly used to compensate for the case that the array cannot be determined by how large. Complementary to list, the main advantage is that it can be accessed quickly and randomly.

String strings

The reason for discarding the char* string is to choose the string class in the C + + standard library because he does not have to worry about the adequacy of the memory, the length of the string, and so on, and as a class, his integrated operation function is sufficient to fulfill the needs of most of our cases (even 100%). We can use = To do the assignment, = = to compare, + do concatenation (is not very simple?). We can do it as a basic data type for C + +.
Well, get to the point ...
First, in order to use the string type in our program, we must include the header file. As follows:
#include <string>//Note This is not string.h string.h is a C string header file

1. Declaring a C + + string
Declaring a string variable is simple:
String Str;
So we declare a string variable, but since it is a class, there are constructors and destructors. The above declaration does not pass in parameters, so the default constructor of string is used directly, and the function is to initialize STR to an empty string. The constructor and destructor for the string class are as follows:
a) string s; Generates an empty string s
b) string S (str)//Copy constructor generates a copy of STR
c) string s (STR,STRIDX)//The part of "starting at position Stridx" within the string str as the initial value of the string
d) string s (Str,stridx,strlen)//The part of the string str "starting at STRIDX and at most strlen" as the initial value of the string
e) string s (CStr)//C string as the initial value of s
f) string S (Chars,chars_len)//The first chars_len character of the C string as the initial value of the string s.
g) string S (num,c)//Generate a string containing num characters c
h) string S (beg,end)//The initial value of the string s as a character in the interval beg;end (not including the end)
i) s.~string ()//Destroy all characters, free memory
It's all very simple, and I won't explain it.


2. String manipulation functions
Here is the focus of the C + + string, I first put the various operational functions listed, do not like to see all the functions of the people can find their favorite functions, and then to see his detailed explanation.
A) =,assign ()//Assign new value
b) Swap ()//Exchange two strings of content
c) +=,append (), push_back ()//Add characters at the tail
d) Insert ()//Insert character
e) Erase ()//delete character
f) Clear ()//delete all characters
g) Replace ()//replace character EF BB BF 3C 3f78 6D 6C
h) +//concatenation string
i) ==,!=,<,<=,>,>=,compare ()//Compare strings
j) Size (), Length ()//number of characters returned
k) max_size ()//The maximum possible number of characters returned
L) empty ()//Determine if the string is empty
m) Capacity ()//return the character capacity before reallocation
N) reserve ()//retain a certain amount of memory to accommodate a certain number of characters
o) [], at ()//Access single character
p) >>,getline ()//read a value from stream
Q) <<//write value to stream
r) Copy ()//Assign a value to a c_string
s) c_str ()//return content as C_string
T) data ()//returns the content as a character array
u) substr ()//return a substring
V) Find function
W) begin () end ()//provide an STL-like iterator support
x) Rbegin () rend ()//Reverse iterator
Y) Get_allocator ()//Return Configurator

STL data Structure part of the original tutorial, to detailed information please Baidu

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.