[Daily learning] STL vector, stlvector
This Thursday, I learned the time relation of the vector. It is not until now.
Now we will summarize some common usage of vector.
Vector requires an array name. Because vector itself is an uncertain-length array, we use a as an example. This is equivalent to creating an array a [], but there is no given length.
Statement Syntax:
Header file vector
Vector <int> a indicates that an indefinite array named a is created and its base type is integer.
The vector <int> a [31] sentence means to create a subscript from an array 0-30, and each array element is an uncertain-length array. The base type is an integer.
Note that in the second sentence, it is equivalent to defining a two-dimensional array. Its first coordinate length (which can be considered as the number of rows) is a definite 30 rows, but the length of the Second coordinate is unknown, can be added or deleted at will
If the second sentence is written, the following function operation takes a [1] as an example. It should be a [1]. size (), a [1]. push_back (I ).
Common functions:
A. size () read array size [must be enclosed in brackets] The read size is the actual size: array a [5], subscript is a [0]-a [4]. The return value of this function is 5.
A. resize () changes the array size because the vector is an indefinite array. resize (x) indicates that the element of the subscript [0-(x-1)] is retained as a string of x length.
A. push_back () Add the element without any element to the end of the array and place it in the first position.
A. pop_back () Delete the last element
A. clear () clear array
A. empty () Check whether array a is empty
(I have never used these two)
Vector can be directly assigned a value, or can be used as a function parameter or return value. However, I have not encountered any problems yet.
The first vector program I came into contact with was the word flip written by TY bacteria (codevs1205). The idea was to input a sentence to flip her. I love you, and you love I.
This is just a very simple question, but at the time I was just transferred to my western family, I had to read it.
The ultimate version of the program I wrote is like this.
#include<iostream>#include<string>using namespace std;int main(){string a[500];string s;int i=0;while (cin>>s){a[i]=s;i++;} for (i=i-1;i>=0;i--){cout<<a[i]<<" ";}return 0;}
This is a simple method. At this time, good guys, TY, wrote a code that looks more subtle.
# Include <iostream> # include <vector> # include <string> using namespace std; main () {string str; vector <string> a; // open a string array, name: a while (cin> str) {// if str exist return 1 else return 0. push_back (str); // if it's not the first put it behind the last} for (int I =. size ()-1; I> = 0; -- I) cout <a [I] <"; // output in the opposite way}I felt novel and interesting, so I carefully asked each sentence what the meaning of each sentence. The comment on the right should be hard to understand the English I left at the time.
The idea is very clear and simple to open a vector a, each reading a string (getline is useless, stream reading is used, stream reading and C standard reading are read spaces, getline is read to the end of the row ), put it in the reverse order at the end of the array.
The second program is The ruka Example 5-2 Uva101-The Blocks Problem block.
Questions require simulated block movement:
There are n (0 <n <25) Fast blocks and 5 operations:
Move a onto B before moving a to B, put the blocks on a and B back to their original positions.
Move a over B before moving a to the stacked wood where B is located, put the blocks on a back into the original blocks.
Pile a onto B puts the blocks including a itself and above on B, and puts the blocks above B back to the original position.
Pile a over B will move the building blocks including a and above to the pile where B is located.
The quit command ends. If AB is in the same heap in the first four actions, no changes are made.
When I read the code provided by ruka, I silently found that it was very bad that I did not dare to start the function name variable name, especially when I attended NOIP2014 in the same window, because I used the LINUX link. an unsupported name leads to a compilation error. After five points are missing, the name of the variable in Chinese Pinyin is an English void, which is really evil. Therefore, I have to follow ruka's example and use the complex function name that is said to be commonly used by programmers. TUT
Put the code on
//vector#include<iostream>#include<vector>#include<string>using namespace std;const int maxn=30;int n;vector<int> p[maxn];void find_block(int x,int &px,int &hx){for (px=0;px<n;px++){for (hx=0;hx<p[px].size();hx++){if (p[px][hx]==x) return;}}}void size_clear(int px,int hx){for (int i=hx+1;i<p[px].size();i++){int b=p[px][i];p[b].push_back(b);}p[px].resize(hx+1);}void pile(int px,int hx,int p2){for (int i=hx;i<p[px].size();i++){p[p2].push_back(p[px][i]);}p[px].resize(hx);}void print(){ for (int i=0;i<n;i++){ cout<<i<<':'; for (int j=0;j<p[i].size();j++) cout<<' '<<p[i][j]; cout<<endl; }}int main(){cin>>n;int a,b;string s1,s2;for (int i=0;i<n;i++) p[i].push_back(i);while (cin>>s1>>a>>s2>>b){int pa,pb,ha,hb;find_block(a,pa,ha);find_block(b,pb,hb);if (pa==pb) continue;if (s2=="onto") size_clear(pb,hb);if (s1=="move") size_clear(pa,ha);pile(pa,ha,pb);}print();return 0;}
This code reduces the writing and usage of functions by analyzing the relationship.
Also
void find_block(int x,int &px,int &hx){for (px=0;px<n;px++){for (hx=0;hx<p[px].size();hx++){if (p[px][hx]==x) return;}}}
The px and hx variables are defined in the main function and are not copied. They are used directly. A closer look, the original function parameters are passed and referenced. This is a bit like a variable in Pas. what are the specific differences between the parameters? I still don't know. According to Comrade Leo, he usually uses pointers (it seems to be purely personal, this guy is very fond of pointers, class, constructor, and so on.) He also said that the pointer is referenced by the father. How can he not have a pointer? The bottom layer of the reference is the pointer, but the reference is faster.
The advantage of referencing in this section is that the px and hx values in the main function are constantly assigned when we do not need to transmit parameters to find the row and column positions. When they find the correct values directly exit this process. Two variables in a program have been assigned a value in the search process. I'm amazed at this.
I have received the result that both POJ1208 and Uva101 are AC. I'm glad that this is the first question O (∩ _ ∩) O ~ Of a except A + B on POJ ~
---------------- The following are digress --------------------
I posted a lollipop yesterday and got eight items. The full set of rainbow is left for Yang. The other two are waiting for them to eat.
5 days countdown
-- I think the mountains are high, the water is cooled, and the moon stars are pale.