- 字符串到整数
- #include <string>
- #include <cstdlib>
- using namespace std;
- int StringToInt(string str) {
- return atoi(str.c_str());
- }
复制代码
- 整数到字符串
- #include <string>
- #include <cstdlib>
- using namespace std;
- string IntToString(int num) {
- char str[100];
- return itoa(num, str, 10);
- }
复制代码
进制转换
- 十进制转其他进制
- #include <string>
- #include <cstdlib>
- using namespace std;
- string DecToOther(int dec) {
- char str[100];
- return itoa(dec, str, 2);//把2改为其他数字即可转换为其他进制
- }
复制代码
- 其他进制转换为十进制
- #include <string>
- #include <cstdlib>
- using namespace std;
- int OtherToDec(string str) {
- const char *s = str.c_str();
- char *stop;
- return strtol(s, &stop, 2);//把2改为其他数字即可由其他进制改为十进制
- }
复制代码
排序
- #include <algorithm>
- #include <vector>
- using namespace std;
- struct Node {
- int x;
- int y;
- };
- //如果x不相等则按x降序排列,x相等则按y升序排列
- bool com(Node &a, Node &b) {
- if (a.x != b.x) return a.x > b.x;
- else return a.y < b.y;
- }
- int main() {
- vector<Node> vec;
- //...给vec添加数据,略
- sort(vec.begin(), vec.end(), com);
- return 0;
- }
复制代码
string容器实用函数
- #include <iostream>
- #include <string>
- #include <iterator>
- using namespace std;
- int main() {
- //迭代器访问等基础不总结了,0基础的同学可以参考《算法笔记》
- string a = "xxxyyy";
- string b = "zzz";
- string s2 = a.substr(3, 2);//取子串。s2为"yy"
- string::size_type n = a.find("yy");//n为3
- a.find("yyx");//返回string::npos
- string s3 = a.replace(0, 3, "fff");//s3为"fffyyy"
- string::iterator p = a.insert(a.begin(), '_');//返回成功插入后指向'_'的迭代器
- a.insert(a.find('y'), "_str_");//在第一个找到'y'的下标前面插入一个指定字符串"_str_"
- }
复制代码
set容器的使用
- #include <set>
- using namespace std;
- int main() {
- set<int> s;
- for (int i = 0; i < 5; ++i) {
- s.insert(i*i);//添加元素
- }
- s.find(4);//查找返回迭代器,找不到则返回end()
- s.erase(4);//删除
- return 0;
- }
复制代码
map容器的使用
- #include <iostream>
- #include <map>
- #include <iterator>
- using namespace std;
- int main() {
- map<char, int> mp;
- mp['c'] = 1;//添加元素
- mp['b'] = 2;
- mp['a'] = 3;
- map<char, int>::iterator p = mp.begin();//使用迭代器遍历
- for (; p != mp.end(); ++p) {
- //输出顺序是a,b,c,因为map使用红黑树实现,自动排序的
- cout << p->first << " " << p->second << endl;
- }
- cout << mp['b'] << endl;//使用下标访问,输出2
- map<char, int>::iterator it = mp.find('b');//it指向<'b',1>
- cout << it->second << endl;//输出2
- mp.erase(it);//删除it指向的<'b',1>
- mp.erase('a');//删除<'a',3>,只剩下<'c',1>
- cout << mp.size() << endl;
- return 0;
- }
复制代码
队列的使用
- #include <iostream>
- #include <queue>
- #include <iterator>
- using namespace std;
- int main() {
- queue<int> q;
- for (int i = 1; i <= 5; ++i) {
- q.push(i);//入队
- }
- if (q.empty()) {
- cout << q.front() << " ";
- }
- cout << q.back() << endl;//输出队首1和队尾5
- if (q.empty()) {
- q.pop();//队首1出队
- }
- return 0;
- }
复制代码
控制小数点后精度位数及补齐整数位数
- #include <cstdio>
- #include <cstdlib>
- int main() {
- int n = 9;
- double pi = 3.1415926;
- printf("%02d\n", n);//输出09
- printf("0.3f\n", pi);//输出3.142
- return 0;
- }
复制代码
cctype头文件中处理字符的函数
文件操作(19年考了,20基本不会考)
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <vector>
- using namespace std;
- int main() {
- //打开文件,清空内容,重新写入。ofstream::out指示以写模式打开
- ofstream out1("D\\abc.txt", ofstream::out);
- string s;
- while (getline(cin, s)) {
- out1 << s << endl;
- }
- out1.close();
- //打开文件,向文件追加内容。
- //ofstream::out指示以写模式打开,ofstream::app指示写操作前定位到文件末尾
- ofstream out2("D\\abc.txt", ofstream::out | ofstream::app);
- while (getline(cin, s)) {
- out2 << s << endl;
- }
- out2.close();
- //读取文件
- ifstream in("D\\abc.txt");
- vector<string> vecs;
- while (getline(in, s)) {
- vecs.push_back(s);//保存到vector方便后续处理。若只需打印则无需保存。
- }
- in.close();
- return 0;
- }
复制代码
求最大公约数
- int gcd(int a, int b) {
- if (b == 0)return a;
- else return gcd(b, a%b);
- }
复制代码
求最小公倍数
求得A和B的最大公约数是C,则最小公倍数是A*B / C(防止溢出可以写为A/C * B)
函数指针
- //声明一个函数指针类型ptask,该型指针指向一个函数,该函数返回void,输入参数为空。
- typedef void(*ptask)();
- void task() {
- //...
- }
- int main() {
- ptask p = task;
- p();//通过函数指针调用函数
- return 0;
- }
复制代码