Headsong | course design of data structure and algorithm - algorithm and competition (Chapter 5) - Fundamentals of C + + and STL I

STL is the Standard Template Library of C + +. Its full English name is Standard Template Library. It is a little complex and has many operations, but it is very practical. The purpose of STL is to standardize common components, so that there is no need to redevelop, and ready-made components can be used to improve programming efficiency. It was developed by Alexander Stepanov and others while working in HP Labs. Fundamentally speaking, STL is a collection of containers such as vector, set and map.

This training mainly sets up six levels to teach and practice vector, set and map:

  • Level 1: introduce the relevant operations of vector and design some tasks. At the same time, you need to call the relevant operations of vector to complete them;
  • The second level: a two-dimensional dynamic array application is designed. Using vector can easily solve this problem;
  • The third level: introduce the relevant operations of set, and use these basic operations to solve the set small tasks;
  • Level 4: solve the 26 English letters in an English text based on set (case sensitive);
  • The fifth level: introduce some operations of key value mapping map, use map to maintain a student achievement management system, and provide query function;
  • Level 6: use the key value pair map to count the number of 26 English letters in an English text (case sensitive).

Level 1: dynamic array of STL template: operation of vector vector

Task description

This task: carefully read the relevant operations of vector below, and use vector to complete the functions of inserting, deleting and sorting n integer sequences.

Relevant knowledge

In order to complete this task, you need to master: 1. The concept of vector; 2. Insert elements; 3. Delete elements; 4. Sort vectors based on sort; 5. Traversal vector; 6. Empty the vector.

Concept of vector

vector: a sequential container, similar to an array, but better than an array. Arrays cannot be expanded dynamically, which may cause memory waste and access out of bounds when the program is running. vector can make up for this defect by dynamically allocating and expanding memory. It has fast random access, slow insertion and deletion in the middle, but fast insertion and deletion at the end.

As the class template of the basic array, the vector is included in the vector header file. The definition method is as follows (note that it is used together with algorithm and using namespace std):

  • Vector < int > V1, define an element of type int   Integer vector v1
  • Vector < string > v2 (10) defines a vector v2 whose element type is string string, the initial storage space size is 10, and each element is initially an empty string
  • Vector < node > v3 defines an element as a vector v3 of node type, where node is generally a user-defined data type such as a structure

Insert element

Insert an element into the vector by calling push_ The back () method is implemented (insert at the end of the vector). In addition, you can directly insert elements at the specified location through subscript access (provided that the location has been allocated memory space), as shown in the following example:

1 vector <int> vec; // Create an integer vector vec
2 vec.push_back(1); // towards vec Insert an element 1
3 vec.push_back(2); // towards vec Insert an element 2
4 vec[1] = 3; // Insert element 3 directly in position 1, and the original element 2 is covered by element 3
5 // at present vec Include 1, 3 Two elements

Delete element

The deletion of the vector corresponds to the insertion, including the deletion of the end of the queue and the deletion of the specified position: the end of the queue is deleted by calling pop_back() method. Note that it does not return deleted elements; The deletion of the specified position is based on the iterator. The pointer of the array corresponding to the iterator points to the storage address of the vector. The element where the iterator position pos is located is deleted by calling the erase(iterator pos) method. The examples based on the above vec are as follows:

1 vec.pop_back(); // Element 3 deleted
2 vector<int>::iterator pos = vec.begin(); //Define a vector<int>Iterator for pos,And point vec First address of
3 cout<<*pos; // Like a pointer, through*Access the value on the address, and the output is 1
4 vec.erase(pos); // Delete iterator address pos And its elements, at present pos by vec The first address, the element value is 1. After deletion, the element is empty and does not contain any elements

Sorting vectors based on sort

Vector is an array based class template, which can also be sorted by sort function, as follows:

sort(vec.begin(), vec.end()); // Default sort from small to large

Ergodic vector

The traversal of vectors can be accessed by subscript and iterator:

1 for(int i=0;i<vec.size();i++) // size()Returns the current vector vec Size of
2     cout<<vec[i];
3 for(vector<int>::iterator it=vec.begin();it!=vec.end();it++)
4     cout<<*it;

Empty vector

The vector is cleared by calling the clear() method. After clearing, the vector size becomes 0:

1 vec.clear()

Programming requirements

The programming task of this level is to complete the code between Begin and End in the code fragment main on the right. The specific requirements are as follows:

  • Create a vector vec of integer type;
  • Read the data: the number of sequences n, and N integers, and insert the vector vec;
  • Delete the duplicate elements in vector vec through erase operation: retain the elements that appear for the first time and delete the duplicate elements that appear after;
  • Use the Algorithm template function sort to sort the elements in the vector vec from small to large;
  • Traverse vector vec and output: elements are separated by spaces and line breaks are added at the end \ n;
  • Call clear to empty the vector.

Test description

The platform will automatically compile the completed code, generate several groups of test data, and then judge whether the program is correct according to the output of the program.

The following is a test example of the platform:

Test input:


1 2 3 1 2 3 4

Expected output:

1 2 3 4


Input format:

Line 1: number of sequences n

Line 2: n integer sequences

Output format:

First line: traverse and output vectors, separated by spaces, and wrap at the end \ n

The second line: non student output. The value 0 is used to detect whether the vector is empty

Start your mission. I wish you success!


 1 //
 2 //  main.cpp
 3 //  step1
 4 //
 5 //  Created by ljpc on 2018/7/23.
 6 //  Copyright ? 2018 year ljpc. All rights reserved.
 7 //
 9 #include <iostream>
10 #include <algorithm>
11 #include <vector>
12 using namespace std;
14 int main(int argc, const char * argv[]) {
17     // Please supplement the code here to complete this task
18     /********* Begin *********/
19     // 1.Construct integer vector vec
20     vector<int> vec;
22     // 2.Read data: number of sequences n,as well as n An integer and stored in a vector vec
23     int n;
24     cin>>n;
25     for(int i=0; i<n; i++) {
26         int m;
27         cin>>m;
28         vec.push_back(m);
29     }
31     // 3.Delete vector vec Duplicate elements in
32     sort(vec.begin(),vec.end());
33     auto last = std::unique(vec.begin(),vec.end());
34     vec.erase(last,vec.end());
37     // 4.use Algorithm template function sort yes vec Sort: from small to large
38     sort(vec.begin(),vec.end());
40     // 5.Ergodic vector vec And output, elements are separated by spaces and end with line breaks'\n'
41     for(vector<int>::iterator it=vec.begin();it!=vec.end();it++) cout<<" "<<*it;
42     cout<<endl;
44     // 6.Empty vector vec
45     vec.clear();
47     /********* End *********/
48     printf("%d\n", int(vec.size()));
50     return 0;
51 }


Posted by zuperxtreme on Sun, 31 Oct 2021 07:53:02 -0700