算法题常用库中的工具
在过去的算法竞赛中,我常常接触到一些库中的工具,例如函数、容器、类等等,但是总是零零散散的,记忆也不是很牢,每次都是用到了现查。于是我用这篇blog做一个简单的整理
头文件algorithm
max()函数
在 C++ 中,max 函数是一个非常实用的函数,用于比较两个或更多数值并返回其中的最大值。这个函数定义在 algorithm 头文件中。
1 2 3 4 5 6 7 8 9 10 11
| #include <iostream> #include <algorithm>
int main() { int a = 10; int b = 20; int max_value = std::max(a, b);
std::cout << "Max value is: " << max_value << std::endl; return 0; }
|
在这个例子中,std::max(a, b) 将返回 a 和 b 中的较大值,并将结果存储在 max_value 中。
sort()函数
头文件cmath
取整函数
round()
ceil()
floor()
字符串相关
strcpy
substr
getline
通常的使用方式如下:
1 2 3 4 5
| int a; string str; cin>>a; cin.ignore(); getline(cin,str);
|
注意在使用getline读取一行信息之前,如果输入有回车,要使用cin.ignore()来处理回车
erase
- basic_string & erase(size_type pos=0, size_type n=npos);即erase(pos,n);从给定起始位置pos处开始删除, 删除n个字符, 返回值修改后的string对象引用。比如erase(0,1)就是删除第一个字符。
- iterator erase(const_iterator position)即erase(positon),删除迭代器位置处的单个字符(position是个string类型的迭代器), 并返回下个元素的迭代器。
如果position 不是迭代器位置,则删除该位置及之后的所有字符。
- iterator erase(const_iterator first, const_iterator last)即erase(first,last);删除迭代器(first, last)区间的所有字符(first和last都是迭代器),返回一个指向被删除的最后一个元素的下一个字符的迭代器.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include<iostream> #include<string> using namespace std; int main() { string str = "hello the world!"; string str1 = "hello the world!"; string str2 = "hello the world!"; string str3 = "hello the world!"; string str4 = "hello the world!"; str.erase(6, 2); cout << "<" << str << ">" << endl; str1.erase(8); cout << "<" << str1 << ">" << endl; str2.erase(str2.begin()); cout << "<" << str2 << ">" << endl; str3.erase(str3.begin() + 3, str3.end()-3); cout << "<" << str3 << ">" << endl; str4.erase(str4.begin() + 3, str4.end()); cout << "<" << str4 << ">" << endl; system("pause"); return 0; }
|
资料来自于:
CSDN
vector
vector在指定位置插入元素
使用vector类的内置方法,insert
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #include <iostream> #include <vector>
int main() { std::vector<int> numbers = {1, 2, 3, 5}; auto it = numbers.begin() + 3; numbers.insert(it, 4); numbers.insert(it, {4, 5, 6});
std::cout << "After insertion: "; for(int num : numbers) { std::cout << num << " "; } std::cout << std::endl;
return 0; }
|
vector在指定位置删除元素
使用vector类的内置方法,erase
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #include <iostream> #include <vector>
int main() { std::vector<int> numbers = {1, 2, 3, 4, 5}; auto it = numbers.begin() + 2; numbers.erase(it);
auto start = numbers.begin() + 2; auto end = numbers.begin() + 5; numbers.erase(start, end);
std::cout << "After deletion: "; for(int num : numbers) { std::cout << num << " "; } std::cout << std::endl;
return 0; }
|
关于vector插入与删除的总结,相同点是都可以使用迭代器进行,并且在操作一个元素的时候都是使用一个迭代器来指定操作元素的位置;在操作多个元素的时候插入还是使用一个迭代器,删除使用两个迭代器来指定区间。
插入,是插入于指定迭代器的位置,原本位置及之后的元素,均向后挪动(挪动多少取决于你插入元素的多少)
删除,删除指定位置的元素,或指定区间的元素
注意begin()获取的迭代器对应位置是首元素,end()迭代器对应位置是尾元素的下一位
map
queue
stack
deque
双端队列,支持队列的基本操作。但是是用push_front, pop_back
另外还有push_back, pop_front
还支持从任意位置删除元素,与vector类似,使用erase()即可
并且可以使用at()方法便捷地访问元素值:
1 2 3 4 5 6 7 8 9
|
std::cout << "\n使用 at() 方法访问元素:" << std::endl; for (size_t i = 0; i < dq.size(); ++i) { std::cout << "dq.at(" << i << ") = " << dq.at(i) << std::endl; }
|
这样的方法便捷地访问deque的值
accumulate
1 2 3 4 5 6 7 8 9
| #include<numeric> using namespace std; int a[10] = {1,2,3,4,5,6,7,8,9,10}; vector<int> b = {1,2,3,4,5};
int sum_a = accumulate(a,a+10,0); int sum_b = accumulate(b.begin(),b.end(),0);
|
注意accumulate最后一个参数是必要的,含义是在累加指定迭代器范围的容器中的元素后,加上第三个元素,并且按照第三个元素的数据类型返回求和结果。
所以即使不用额外的累加,也需要使用第三个元素,int型的时候,第三个元素通常置0即可。
tuple
元组的使用:设置值&获取值