zoukankan      html  css  js  c++  java
  • 【c/c++】刷算法题常用的函数,操作和容器汇总

    刷OJ常用函数汇总

    #include<iostream>
    #include<algorithm>
    #include<functional>
    
    #include<vector>
    
    #include<set>
    #include<unordered_set>
    
    //map中涉及pair,添加map头文件的同时会自动添加pair头文件
    #include<map>
    #include<unordered_map>
    
    #include<stack>
    
    #include<queue>
    #include<deque>
    
    #include<string>
    #include<array>
    
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    
    //using namespace::some_name
    using std::cout;
    using std::cin;
    using namespace std;
    
    //结构体中的构造函数
    //编译器默认提供无参构造函数,自定义构造函数之后,则不提供无参构造
    struct student {
    	int id;
    	char gender;
    	string name;
    	//自定义无参构造,初始化成员变量
    	student() { id = 0; gender = 'm'; name = ""; }
    	student(int _id, char _gender, string _name) {
    		this->id = _id;
    		this->gender = _gender;
    		this->name = _name;
    	}
    };
    struct teacher {
    	int id;
    	char gender;
    	string name;
    	//自定义无参构造,初始化成员变量
    	teacher() { id = 0; gender = 'm'; name = ""; }
    	teacher(int _id,char _gender,string _name):id(_id),gender(_gender),name(_name) {}
    };
    
    
    void f_constant() {
    	//#define name_of_constant value_of_constant
    	#define pi 3.14
    	//const修饰的常量 const type_name variable
    	const int ci = 12;
    }
    
    void f_bit_operation() {
    	//1.左移			<<
    	cout <<"左移: 1<<3   "<< (1<<3) << endl;
    	//2.右移			>>
    	cout << "右移: 16>>3   " << (16 >> 3) << endl;
    	//3.按位与		&
    	cout << "按位与:8 & 9   " << (8 & 9)<< endl;
    	//4.按位或		|
    	cout << "按位或:8 | 9	 " << (8 | 9) << endl<< endl;
    	//5.位异或		^
    	cout << "位异或:8 ^ 9	" <<(8 ^ 9)<< endl;
    	//6.位取反		~
    	cout << "位取反:~8		" << (~8) << endl;
    
    }
    
    
    
    void f_char() {
    	char c1;
    	c1 = getchar();
    	putchar(c1);
    }
    
    //变长数组,可随意调整数组长度,自动初始化为0,false
    void f_vector() {
    	//初始化
    	vector<int> v0(10);
    	vector<int> v1 = { 1,2,3,4 };
    	vector<int> v2(10, 2);
    	vector<int> v3;
    	vector<int> v4(v1);
    
    	//常用操作
    	v1.push_back(1);
    	v1.pop_back();
    
    	auto pb = v1.begin();
    	auto pe = v1.end();
    	
    	v1.size();
    	v1.resize(100);
    	v1.clear();
    	v1.empty();
    
    	v1.insert(v1.begin()+2,10);
    	v1.erase(v1.begin() + 3);
    	v1.erase(v1.begin(),v1.begin()+3);
    }
    
    //内部自动有序不含重复元素(红黑树实现)
    void f_set() {
    	//初始化
    	set<int> s0;
    	set<int> s1 = { 1,2,3,4,5 };
    	set<int> s2(s1);
    
    	//常用操作
    	auto pb = s0.begin();
    	auto pe = s0.end();
    	s0.insert(10);//set自动去除重复元素,且自动有序(multiset可重复,unordered_set无序)
    	//s0.erase(s0.find(10));
    	auto p_tmp = s0.find(10);//p_tmp是set<int>::iterator类型
    	cout << *p_tmp << endl;
    	s0.erase(p_tmp);	//依据迭代器删除单个元素
    	s0.erase(10);		//依据元素的值删除单个元素
    	s0.erase(s0.begin(), s0.end());//删除迭代器指定的一串元素
    	s0.size();
    	s0.clear();
    }
    
    //map				(红黑树实现)键唯一且有序,
    //multimap			一个键对应多个值,
    //unordered_map		(散列实现)只映射不排序
    void f_map() {
    	//初始化
    	map<char, int> mp;
    	mp['a'] = 0;
    	mp['b'] = 1;
    	mp['c'] = 2;
    	mp['d'] = 3;
    
    	//使用迭代器访问map
    	for (auto pb = mp.begin(); pb != mp.end(); pb++) {
    		cout << pb->first << " " << pb->second << endl;
    		cout << (*pb).first << " " << (*pb).second << endl;
    	}
    
    	//使用键访问map
    	cout << mp['a'] << " " << mp['b'] << mp['c'] << mp['d'] << endl;
    
    	//常用操作
    	auto it = mp.find('a');//find(key)返回键为key的映射的迭代器
    	cout << it->first << " " << it->second << endl;
    
    	mp.erase(mp.find('a'));//erase(it),删除迭代器指定的元素
    	mp.erase('b');//erase(key),删除key指定的元素
    	mp.erase(mp.find('c'), mp.end());//erase(it1,it2),删除[it1,it2)指定的元素
    	cout << mp.size() << endl;
    	mp.clear();
    
    }
    
    //后进先出,只能使用top()访问栈顶元素
    void f_stack() {
    	//初始化
    	stack<int> st;
    	for (int i = 0; i < 10; i++) {
    		st.push(i);
    	}
    	cout << st.top() << endl;//9
    	st.pop();
    	cout << st.top() << endl;//8
    	//常用操作 empty(),size()
    }
    
    void f_queue() {
    	//初始化
    	queue<int> q;
    
    	for (int i = 0; i < 10; i++) {
    		q.push(i);	//压入队列
    	}
    
    	//常用操作
    	cout << q.front() << " " << q.back() << endl;//只访问而不删除
    	cout << q.size() << endl;
    	q.pop();//队首元素出队列
    	cout << q.empty() << endl;//是否为空队列
    }
    
    //双端队列,两端都可插入和删除
    void f_deque() {
    	//初始化
    	deque<int> dq;
    	for (int i = 0; i < 10; i++) {
    		dq.push_back(i);
    		dq.push_front(i);
    	}
    	cout << dq.front() << " "<<dq.back() << endl;
    	dq.pop_front();
    	dq.pop_front();
    	cout << dq.size() << endl;
    	cout << dq.empty() << endl;
    	dq.clear();
    	for (auto pb = dq.begin(); pb != dq.end(); pb++) {
    		cout << *pb << endl;
    	}
    }
    
    //优先队列,使用堆实现默认将当前队列最大元素置于队首的容器(大根堆)
    /*
    没有front()和back()函数,只能通过top()函数访问队首元素,也就是优先级最高的元素(优先级可自定义)
    */
    
    //结构体内部重载小于号(且只能重载小于号)实现自定义优先级
    struct fruit {
    	string name;
    	int price;
    	friend bool operator<(fruit f1, fruit f2) {
    		return f1.price < f2.price;
    	}
    };
    
    //结构体外实现自定义优先级
    struct animal {
    	string name;
    	int weight;
    };
    struct cmp {
    	bool operator()(animal a1, animal a2) {
    		return a1.weight > a2.weight;
    	}
    };
    void f_priority_queue() {
    	priority_queue<int> pq;//默认情况下为大根堆
    	for (int i = 0; i < 10; i++) {
    		pq.push(i);
    	}
    	cout << pq.top();//9,默认是大根堆
    	//常用操作:push(),top(),pop(),empty(),size()
    
    	//自定义优先级
    	priority_queue<int, vector<int>, less<int> > pq1;//用<做比较,大根堆
    	priority_queue<long long, vector<long long>, greater<long long> >pq2;//用>作比较,小根堆
    	priority_queue<double, vector<double>, less<double> > pq3;//大根堆
    	priority_queue<char, vector<char>, less<char> > pq4;//大根堆
    	priority_queue<string, vector<string>, less<string> > pq5;//大根堆
    	//给结构体定义优先级
    	//结构体内部重载小于号(且只能重载小于号)
    	priority_queue<fruit> q1;//如果想以价格低的水果为优先级高,那么只需要把return中的小于号改为大于号
    	fruit f1, f2, f3;
    	f1.name = "apple";
    	f1.price = 1;
    	f2.name = "banana";
    	f2.price = 2;
    	f3.name = "orange";
    	f3.price = 3;
    	q1.push(f1);
    	q1.push(f2);
    	q1.push(f3);
    	cout << q1.top().name << " " << q1.top().price << endl;//orange 3
    	
    	//结构体外自定义cmp结构体实现自定义优先级
    	priority_queue<animal, vector<animal>, cmp> q2;
    	animal a1, a2, a3;
    	a1.name = "monkey";
    	a1.weight = 1;
    	a2.name = "snake";
    	a2.weight = 2;
    	a3.name = "horse";
    	a3.weight = 3;
    	q2.push(a1);
    	q2.push(a2);
    	q2.push(a3);
    	cout << q2.top().name << " " << q2.top().weight << endl;//monkey 1
    }
    
    //需要引入utility头文件,但由于map中已经引入了utility,所以可以省略
    //pair是将两个元素绑在一起作为一个合成元素
    /*
    pair具有以下两个用途:
    1.用以代替二元结构体及其构造函数,可以节省编码时间
    2.作为map的键值对来进行插入
    */
    void f_pair() {
    	//初始化
    	{
    		pair<string, int> p1;
    		pair<string, int> p2("this is a key of a pair", 1);
    		pair<string, int>("this is an anonymous pair", 2);//有点类似于匿名内部类
    		auto p3 = pair<string, int>("this is an anonymous pair", 2);
    		auto p4 = make_pair<string, int>("make_pair", 3);
    		pair<string, int> p5;
    		p5.first = "hello world!";
    		p5.second = 4;
    		cout << p5.first << " " << p5.second << endl;
    	}
    	
    	//pair的比较
    	/*
    	先以first的大小作为标准,仅当first相等时才去判断second的大小
    	*/
    	pair<int, int> p6(5, 10);
    	pair<int, int> p7(5, 15);
    	pair<int, int> p8(10, 5);
    	if (p6 < p8) cout << "p1<p8" << endl;//true
    	if (p6 <= p8) cout << "p1<=p8" << endl;//true
    	if (p6 < p7)cout << "p1<p7" << endl;//true
    
    	//作为map的键值对元素
    	map<string, int> mp;
    	mp.insert(make_pair("make_pair",1));
    	mp.insert(pair<string, int>("pair key", 2));
    	pair<string, int> pair_tmp("another pair", 3);
    	mp.insert(pair_tmp);
    }
    
    void f_string() {
    	char c_type_str[] = "this is a c type string,using a char array";
    	cout << "c style string: " << c_type_str << endl;
    	cin.getline(c_type_str, sizeof(c_type_str) / sizeof(c_type_str[0]));
    	
    	//convert a c style string to string
    	string cpp_type_str(c_type_str);
    	string cpp_type_str2 = c_type_str;
    	cout << "string cpp_type_str: " << cpp_type_str << endl;
    	
    	//convert a string to c style string
    	const char* c_type_str2 = cpp_type_str.c_str();
    	c_type_str2 = "another c style string";
    
    	//读取行
    	string str_getline;
    	getline(cin, str_getline);//从标准输入中读取整行并赋值给str
    
    	//访问string中的单个元素
    	string str = "this is a demo string";
    	cout << str[2];		//用下标访问str中的元素
    	cout << (*(str.begin()))<<" "<< (*(str.begin()+4)) << endl;	//使用迭代器访问str中的元素
    
    	cout<<str.length();
    	cout << str.size();
    	str.insert(3,"tmp_str");//insert(pos,string),在str[3]处插入tmp_str
    	string str_tmp = "this is a tmp string for insert";
    
    	//str.insert(it,it2,it3);it为原字符串欲插入位置,it2,it3待插入字符串的首尾迭代器
    	//在str的指定迭代器位置,插入str_tmp迭代器指定的一段字符串
    	str.insert(str.begin(),str_tmp.begin(),str_tmp.begin()+4);
    	str.erase(str.begin() + 2);
    	str.erase(str.begin(), str.end() - 1);
    	str.erase(3,2);//从3号位置删除2个字符
    	str.clear();
    	str.empty();
    	str.substr(0,5);//substr(pos,len);截取0号开始的5个字符子串
    	cout << string::npos << endl;
    	str = "thank you very much!";
    	str_tmp = "thank";
    	cout << ((str.find(str_tmp)) == (string::npos)) << endl;
    	//str.replace(pos,len,str2);把pos开始的长度为len的子串替换为str2
    	str.replace(10,4,str_tmp);
    	//str.replace(it1,it2,str2);把it1和it2之间的子串替换为str2
    	str.replace(str.begin(),str.begin()+3,str_tmp);
    }
    
    //基本数据类型使用自定义cmp函数实现排序
    bool cmp1(int a, int b) {
    	return a > b;//降序排序
    }
    
    struct book {
    	int num;
    	double price;
    };
    
    //自定义compare函数实现一级排序,按照book的价格降序排序
    bool cmp2(book b1, book b2) {
    	return b1.price > b2.price;
    }
    
    //自定义compare函数实现二级排序,按照book的价格降序排序,编号升序
    bool cmp3(book b1, book b2) {
    	return b1.price == b2.price ? b1.num<b2.num : b1.price>b2.price;
    }
    
    //自定义compare函数实现字符串字典序降序排序
    bool cmp4(string str1, string str2) {
    	return str1 > str2;
    }
    
    //自定义compare函数实现按字符串长度降序
    bool cmp5(string str1, string str2) {
    	return str1.length() > str2.length();
    }
    
    void f_algorithm() {
    	//0. max(),min(),abs(),swap()
    	int a = 1, b = -2;
    	cout << max(a, b) << " " << min(a, b) << endl;
    	cout << abs(b) << endl;
    	swap(a, b);
    	//1.排序,sort,默认升序
    	int ia[] = { 4,5,62,1,5,79,2 };
    	for (int i = 0; i < 7; i++) {
    		cout << ia[i];
    	}
    	sort(ia, ia + 7);
    	for (int i = 0; i < 7; i++) {
    		cout << ia[i];
    	}
    
    	//自定义compare函数
    	//1.基本数据类型使用自定义compare函数
    	sort(ia, ia + 7, cmp1);//自定义cmp1中使用>号实现降序排序(默认情况下升序排序)
    	for (int i = 0; i < 7; i++) {
    		cout << ia[i];
    	}
    	//2.1自定义结构体使用自定义compare函数实现一级排序
    	book arr_book[10];
    	for (int i = 0; i < 10; i++) {
    		arr_book[i].num = i * i;
    		arr_book[i].price = i / (arr_book[i].num);
    	}
    	sort(arr_book, arr_book + 10, cmp2);
    	//2.2自定义结构体使用自定义compare函数实现二级排序
    	sort(arr_book, arr_book + 10, cmp2);
    	
    	//3.容器的排序
    	//vector举例,实际上与数组相差不大,注意cmp函数的自定义即可
    	vector<int> vi;
    	for (int i = 0; i < 10; i++) {
    		vi.push_back(i);
    	}
    	sort(vi.begin(), vi.end(), cmp1);//cmp1中使用>号,实现降序排序
    	//string举例
    	//	1.compare实现字典序降序
    	string arr_str[] = { "aaab","aab","ab" };
    	sort(arr_str, arr_str + 3, cmp4);
    	//	2.compare实现按字符串长度降序
    	sort(arr_str, arr_str + 3, cmp5);
    
    	//4.使用functional中提供的模板函数(需加入functional头文件)
    	sort(ia, ia + 7, less<int>());
    	sort(ia, ia + 7, greater<int>());
    
    	//2.翻转,reverse(it1,it2),翻转[it1,it2)之间的元素
    	vector<int> v1 = { 0,1,2,3,4,5,6,7,8,9 };
    	for (auto it = v1.begin(); it != v1.end(); it++) {
    		cout << *it << " ";
    	}
    	reverse(v1.begin(), v1.end());
    	for (auto it = v1.begin(); it != v1.end(); it++) {
    		cout << *it << " ";
    	}
    
    	string str = "you know how much I like you";
    	reverse(str.begin(), str.end());
    	cout << str << endl;
    	//3.next_permutation(),给出一个序列在全排列中的下一个序列
    	//next_permutation(it1,it2)在到达全排列的最后一个时会返回false,使用do-while输出最开始的排列123
    	int arr[] = { 1,2,3 };
    	do {
    		for (int i = 0; i < 3; i++) {
    			cout << arr[i];
    		}
    	} while (next_permutation(arr, arr + 3));
    
    	//4.fill(it1,it2,value)赋予[it1,it2)迭代器指定区间元素值value
    	int arr2[] = { 1,2,3,4,5 };
    	fill(arr2, arr2 + 5, 6666);
    
    	//5.lower_bound()和upper_bound(),用于有序数组和容器中
    	/*
    	lower_bound(first,last,val),Returns an iterator pointing to the first element 
    	in the range [first, last) that is not less than (i.e. greater or equal to) 
    	value, or last if no such element is found.
    	
    	upper_bound(first,last,val),Returns an iterator pointing to the first element 
    	in the range [first, last) that is greater than value, or last if no such 
    	element is found.
    	*/
    	int arr_i[10] = { 1,2,2,3,4,4,5,5,5,5 };
    	int* lowerpos = lower_bound(arr_i, arr_i + 10, 2);
    	int* upperpos = upper_bound(arr_i, arr_i + 10, 5);
    }
    
    void f_cctype() {
    	//常用函数,
    	string str = "this is a random string with alphabetic character a-zA-Z and decimal digits:0123456789";
    	cout<<isalpha(str[10])<<endl;			//是否为字母
    	cout << isdigit(str[100]) << endl;		//是否为数字
    	cout << isalnum(str[0]) << endl;		//是否为字母或者数字
    }
    
    
    //常用的数学函数
    void f_math() {
    	cout <<"abs(-1) " << abs(-1) << endl;
    	cout <<"floor(2.1) " << floor(2.1) << endl;
    	cout <<"ceil(2.1) "<< ceil(2.1) << endl;
    	cout << "pow(2.0,3.0) " << pow(2.0, 3.0) << endl;
    	cout << "sqrt(4.0)" << sqrt(4.0) << endl;
    	cout << "log(1) " << log(1) << endl;
    	cout << "round(4.4) " << round(4.4) << " round(4.5) " << round(4.5) << endl;
    	cout << "(int)(4.4 + 0.5) "<< (int)(4.4 + 0.5) <<" (int)(4.5 + 0.5) " << (int)(4.5 + 0.5) << endl;
    }
    
    //冒泡排序
    void f_bubble_sort() {
    	const int sz = 10;
    	int a[sz] = { 9,3,1,5,2,4,8,7,10,6 };
    	for (int i = 0; i < sz-1; i++) {
    		for (int j = 0; j < sz - i - 1; j++) {
    			if (a[j] < a[j + 1]) {
    				swap(a[j],a[j+1]);
    			}
    		}
    	}
    	for (int tmp : a) {
    		cout << tmp << " ";
    	}
    	cout << endl;
    }
    
    //是否为闰年
    bool isleapyear(int year) {
    	return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
    
    //最大公约数(greatest common divisor)
    //辗转相除法,也叫欧几里得算法
    long long gcd(long long a,long long b) {
    	return b == 0 ? a : gcd(b, a % b);
    }
    
    //最小公倍数(lowest common multiple)
    //最小公倍数 = a*b/最大公约数
    long long lcm(long long a,long long b) {
    	return a * b / (gcd(a,b));
    }
    
    //进制转换
    //其它进制转换为十进制,str为base进制下的字符串表示
    /*
    long strtol( const char *str, char **str_end, int base);需要使用'#include<cstdlib>'
    丢弃所有空白字符(通过调用isspace()进行标识),直到找到第一个非空白字符为止,然后使用尽
    可能多的字符来形成有效的以n为基(其中n = base)的整数表示并将其转换为一个整数值(十进制)。
    */
    long xtod(string str,int base) {
    	char* tmp;
    	return strtol(str.c_str(), &tmp, 8);
    }
    
    //十进制转换为其它进制,digital为10进制数,r为需要转换的目标进制,返回目标进制数
    string dtox(int digital, int r) {
    	string result = "";
    	const char s[37] = "0123456789abcdefghijklmnopqrstuvwxyz";
    	if (digital == 0) {
    		return "0";
    	}
    	while (digital != 0) {
    		int tmp = digital % r;
    		result += s[tmp];
    		digital /= r;
    	}
    	reverse(result.begin(), result.end());
    	return result;
    }
    
    
    //string和其他基本数据类型的相互转换
    void f_convert() {
    	//string to int/long/long long/double
    	string str = "123456789";
    	int i = stoi(str);
    	long l = stol(str);
    	long long ll = stoll(str);
    	double db = stod(str);
    
    	//int/long/long long/double to string
    	string str_i = to_string(i);
    	string str_long = to_string(l);
    	string str_long_long = to_string(ll);
    	string str_db = to_string(db);
    }
    
    
    int main9() {
    	//f_bit_operation();
    	//f_math();
    	//f_bubble_sort();
    	//f_string();
    	return 0;
    }
    
  • 相关阅读:
    104.Maximum Depth of Binary Tree
    103.Binary Tree Zigzag Level Order Traversal
    102.Binary Tree Level Order Traversal
    101.Symmetric Tree
    100.Same Tree
    99.Recover Binary Search Tree
    98.Validate Binary Search Tree
    97.Interleaving String
    static静态初始化块
    serialVersionUID作用
  • 原文地址:https://www.cnblogs.com/ericling/p/14426310.html
Copyright © 2011-2022 走看看