算法题常用库中的工具
在过去的算法竞赛中,我常常接触到一些库中的工具,例如函数、容器、类等等,但是总是零零散散的,记忆也不是很牢,每次都是用到了现查。于是我用这篇blog做一个简单的整理
头文件algorithm
max()函数
在 C++ 中,max 函数是一个非常实用的函数,用于比较两个或更多数值并返回其中的最大值。这个函数定义在 algorithm 头文件中。
int main() {
int a = 10;
int b = 20;
int max_value = std::max(a, b); // 使用max函数比较a和b,并将结果存储在max_value中
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
通常的使用方式如下:
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都是迭代器),返回一个指向被删除的最后一个元素的下一个字符的迭代器.
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!";
/****************第1种用法:earse(pos,n)**************************/
str.erase(6, 2);
cout << "<" << str << ">" << endl; // <hello e world!>
/****************第2种用法:earse(position)***********************/
str1.erase(8); //如果pos不是迭代器位置,则删除该位置及之后的所有字符
cout << "<" << str1 << ">" << endl; // <hello th>
str2.erase(str2.begin()); //如果pos是迭代器,则
cout << "<" << str2 << ">" << endl; // <ello the world!>
/****************第3种用法:earse(fist,last)**********************/
str3.erase(str3.begin() + 3, str3.end()-3);
cout << "<" << str3 << ">" << endl; // <helld!>
str4.erase(str4.begin() + 3, str4.end());
cout << "<" << str4 << ">" << endl; // <hel>
system("pause");
return 0;
}
资料来自于:
CSDN
vector
vector在指定位置插入元素
使用vector类的内置方法,insert
int main() {
std::vector<int> numbers = {1, 2, 3, 5};
auto it = numbers.begin() + 3; // 指定插入位置
numbers.insert(it, 4); // 在位置 3 插入元素 4
numbers.insert(it, {4, 5, 6}); // 在位置 3 插入多个元素 4, 5, 6
std::cout << "After insertion: ";
for(int num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
vector在指定位置删除元素
使用vector类的内置方法,erase
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5};
auto it = numbers.begin() + 2; // 指定删除位置
numbers.erase(it); // 删除位置 2 的元素
auto start = numbers.begin() + 2; // 指定起始删除位置
auto end = numbers.begin() + 5; // 指定结束删除位置
numbers.erase(start, end); // 删除从位置 2 到位置 5 的元素
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()方法便捷地访问元素值:
// 使用 at() 方法访问元素
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
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
元组的使用:设置值&获取值