[namespace hdk] ordered_vector

功能:

  1. 已重载[]运算符
  2. 已重载+运算符(合并)
  3. 已重载+=运算符
  4. 已重载构造函数
  5. clear()
  6. it() 以std::vector形式返回自身
  7. print(char=' ',char='\n') 输出,第一个参数为分隔符,第二个参数为结束符
  8. count(x) 查找x的出现次数
  9. find(x) 判断x是否出现,是返回1,否则返回0
  10. empty() 判断当前是否为空
  11. size() 返回当前元素个数
  12. lower_bound(x) 查找第一个大于等于x的位置,返回其下标
  13. upper_bound(x) 查找第一个大于x的位置,返回其下标
  14. insert(x) 插入一个元素x
  15. remove(x) 删除一个等于x的元素,未找到则不执行
  16. remove_all(x) 删除所有等于x的元素
  17. remove_smaller(x,bool=false) 删除全部小于x的元素 (bool=true 时为小于等于)
  18. remove_larger(x,bool=false) 删除全部大于x的元素 (bool=true 时为大于等于)
  19. findpre(x) 寻找小于x的最近元素,返回其下标,或者返回-1
  20. findnext(x) 寻找大于x的最近元素,返回其下标,或者返回-1
  21. nearestabs(x) 寻找x与当前所有元素之差的最小值,不存在则返回0
  22. remove_nearestabs(x) 寻找x与当前所有元素之差的最小值,并将其删除,不存在则返回0
  23. locate_nearestabs(x) 寻找x与当前所有元素之差的最小值,返回其下标,或者返回-1
  24. find_maxnum(x) 寻找第x大的数,返回其值,或者返回-1
  25. find_minnum(x) 寻找第x小的数,返回其值,或者返回-1

定义:

ordered_vector<int> a;
ordered_vector<int> b();
ordered_vector<int> c({1,2,3,3,4,5,6,7});
ordered_vector<int> d({});
std::vector<int> s{1,2,3,4,5};
ordered_vector<int> d(s);

代码:

#include<bits/stdc++.h>
using namespace std;
namespace hdk{
	template<typename T>
	class ordered_vector{
		private:
			std::vector<T>v;
			inline void push_back(T x){
				v.push_back(x);
			}
		public:
			ordered_vector(vector<T> x={}){
				v=x;
			}
			inline void clear(){
				v.clear();
			}
			inline int lower_bound(T x){
				int loc=std::lower_bound(v.begin(),v.end(),x)-v.begin();
				return loc;
			}
			inline int upper_bound(T x){
				int loc=std::upper_bound(v.begin(),v.end(),x)-v.begin();
				return loc;
			}
			inline void insert(T x){
				v.insert(v.begin()+lower_bound(x),x);
			}
			inline void remove(T x){
				int pos=lower_bound(x);
				if(pos!=v.size() and v[pos]==x) v.erase(v.begin()+pos);
			}
			inline void remove_all(T x){
				while(1){
					int pos=lower_bound(x);
					if(pos==v.size()) break;
					if(v[pos]!=x) break;
					v.erase(v.begin()+pos);
				}
			}
			inline std::vector<T> it(){
				return v;
			}
			T operator [](int x){
				if(x==-1) return -1;
				return v[x];
			}
			inline void remove_larger(int x,bool include_this=false){
				int pos=upper_bound(x);
				for(int i=v.size()-1;i>=pos;--i){
					v.erase(v.begin()+i);
				}
				if(include_this) remove_all(x);
			}
			inline void print(char devide=' ',char ending='\n'){
				for(T i:v) cout<<i<<devide;
				cout<<ending;
			}
			inline void remove_smaller(int x,bool include_this=false){
				int pos=lower_bound(x);
				for(int i=pos-1;i>=0;--i){
					v.erase(v.begin()+i);
				}
				if(include_this) remove_all(x);
			}
			inline int findpre(T x){
				int pos=lower_bound(x)-1;
				if(pos<0) return -1;
				return pos;
			}
			inline int findnext(T x){
				int pos=upper_bound(x);
				if(pos>=v.size()) return -1;
				return pos;
			}
			inline T nearestabs(T x){
				int p=findpre(x),n=findnext(x),ans=0x7fffffff;
				if(p>=0) ans=min(ans,x-v[p]);
				if(n>=0) ans=min(ans,v[n]-x);
				return ans;
			}
			inline T remove_nearestabs(T x){
				if(v.empty()) return 0;
				int p=findpre(x),n=findnext(x),ans=0x7fffffff;bool isp=0;
				if(p>=0) if(x-v[p]<ans) ans=x-v[p],isp=1;
				if(n>=0) if(v[n]-x<ans) ans=v[n]-x,isp=0;
				if(isp) v.erase(v.begin()+p);
				else v.erase(v.begin()+n);
				return ans;
			}
			inline int locate_nearestabs(T x){
				if(v.empty()) return -1;
				int p=findpre(x),n=findnext(x),ans=0x7fffffff;bool isp=0;
				if(p>=0) if(x-v[p]<ans) ans=x-v[p],isp=1;
				if(n>=0) if(v[n]-x<ans) ans=v[n]-x,isp=0;
				if(isp) return p;
				else return n;
			}
			inline int count(T x){
				int b=lower_bound(x),e=upper_bound(x);
				return e-b;
			}
			inline bool find(T x){
				return count(x);
			}
			inline bool empty(){
				return v.empty();
			}
			inline int size(){
				return v.size();
			}
			inline int find_maxnum(int rank){
				if(rank>v.size() or rank<1) return -1;
				return v[v.size()-rank];
			}
			inline int find_minnum(int rank){
				if(rank>v.size() or rank<1) return -1;
				return v[rank-1];
			}
			ordered_vector<T> operator +(ordered_vector<T> x){
				ordered_vector<T> ans;
				int i=0,j=0;
				while(i<size() and j<x.size()){
					if(v[i]<x[j]){
						ans.push_back(v[i]);
						i++;
					}
					else{
						ans.push_back(x[j]);
						j++;
					}
				}
				while(i<size()){
					ans.push_back(v[i]);
					i++;
				}
				while(j<x.size()){
					ans.push_back(x[j]);
					j++;
				}
				return ans;
			}
			void operator +=(ordered_vector<T> x){
				*this=(*this)+x;
			}
	};
}
using namespace hdk;
posted @ 2024-07-04 11:50  HaneDaniko  阅读(23)  评论(1编辑  收藏  举报
/**/