STL data discretization

Discretization can be done when the data is only related to the relative size between them, not how much.

Sort first, and then delete duplicate elements, which is the corresponding value of the index element after discretization.

Assuming that the sequence to be discretized is a[n], and b[n] is a copy of the sequence a[n], the above three steps are:

sort(sub_a,sub_a+n);
int size = unique ( sub_a , sub_a + n )- sub_a ; //size is the number of elements after discretization
for ( i = 0 ; i < n ; i ++)
	 a [ i ]= lower_bound ( sub_a , sub_a + size , a [ i ])- sub_a +  1 ; //k corresponds to b[i] after discretization the value of

For step 3, if the discretized sequence is 0, 1, 2, ..., size - 1, lower_bound is used, and from 1, 2, 3, ..., size, upper_bound is used, and lower_bound returns the first one A pointer to a value not less than b[i], and upper_bound returns a pointer to the first value greater than b[i]. Of course, in this question, you can also use lower_bound and then add 1 to get the same result as upper_bound, both of which are For in order to arrange the sequence. Using STL discretization greatly reduces the code size and the structure is fairly clear.


Guess you like

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