Sort sorted vector usage and basic usage of vector

Both vector and set in C++ are very convenient containers. The
sort method is a standard function in the algorithm header file, which can perform efficient sorting. The default is to sort elements from small to large.
Applying the sort method to vector and set can achieve many A sort that meets your own needs
First, the sort method can sort static arrays

#include<iostream>
using namespace std;
int main(){
    int a[10] = { 9, 0, 1, 2, 3, 7, 4, 5, 100, 10 };
    sort(a, a +10);
    for (int i = 0; i < 10; i++)
        cout << a[i] << endl;
    return 0;
}

Here you can see that it is sort(a, a+10), but the array a has only 9 elements in total, why is it a+10 instead of a+9?

Because the sort method actually does not take the number corresponding to the last address,

Moreover, the value taken out by end() of these containers of vector, set, and map is not actually the last value, and the previous one of end is the last value!

You need to use prev(xxx.end()) to remove the last element in the container.

Use the sort function on vectors:

The first case: basic types, such as vector, vector, vector are also possible

	vector<int> v(n);
	for(int i = 0;i < n;i++){
		cin >> v[i];
	}
	cin >> k;
	sort(v.begin(),v.end(),cmp);
	for(int i = 0;i < k;i++){
		cout << v[i] << endl;
	}

The following is a blind spot area encountered when brushing the question about the use of vector

#include <iostream>
#include <vector>
using namespace std;
int main(){
	int m,temp;
	cin >> m;
	vector<int> v(m); 
	for(int i = 0;i < m;i++){
		cin >> temp;
		v.push_back(temp);
	}
	vector<int> :: iterator it; 
	it = v.begin();
	while(it != v.end()){
		cout << *it << " A " << endl;
		it ++; 
	}
	return 0;
}

If you enter a random number to give the running result of this code, the running
result:

5
1 2 3 4 5
0 A
0 A
0 A
0 A
0 A
1 A
2 A
3 A
4 A
5 A

If you are not very familiar with the use of this vector or you are not particularly familiar with the use of the push_back function, you will really be pitted by this function.

vector v(10); // Directly define an int array with a length of 10, the default value of these 10 elements is 0

 for (int i = 0; i < 10; i++) {
		a.push_back(i); // 在vector a的末尾添加⼀个元素i
 }

Because the vector v(m) has been given before to directly define an int array with a length of m, the default value of these m elements is 0, and the push_back() function adds elements at the end, because the vector container has a variable length. container.

I refreshed my level again. It's really vegetables, really water.

The correct usage code of vector is given below

#include <iostream>
#include <vector>
using namespace std;
int main(){
	int m;
	cin >> m;
	vector<int> v(m); 
	for(int i = 0;i < m;i++){
		cin >> v[i];
	}
	for(int j = 0;j < m;j++){
		cout <<  v[j] << " A" << endl;	
	}
	return 0;
}

operation result

5
1 2 3 4 5
1 A
2 A
3 A
4 A
5 A

The second case: Use a custom structure to perform the sort algorithm.
At this time, you need to define a comparison function yourself, because the sort algorithm is based on the elements in the container that can be compared pairwise, and then sorted from small to large, so you need to customize How is it less than ('<')

I'll write this example next time. After all, the use of these tools still needs to be reflected in the brush questions. If I encounter similar problems, I'm updating it. The school is heavy and I'll brush the questions first.

Vector defines the use of one-dimensional arrays The
initial length of the definition vector is m, which is to initialize m vectors with a value of 0

	vector<int> v(m); 
	for(int i = 0;i < m;i++){
		cin >> temp;
		v.push_back(temp);
	}

One is a vector array, how many vector arrays

	vector<int> t[m];
	sort(t[x].begin(),t[x].end());
	for(int i = 1;i<= n;i++){
		int ts,id;
		cin >> ts >> id;
		t[id].push_back(ts);
	}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325679096&siteId=291194637