C++ 常用库: www.cnblogs.com/King-of-Dar…
map用法: zhuanlan.zhihu.com/p/127860466
string用法: blog.csdn.net/buzeng8617/…
vector用法: www.runoob.com/w3cnote/cpp…
cin, getline,getchar等各种输入: blog.csdn.net/bat67/artic…
- map声明及获取长度等
#include <map>
map<int,int> keyValue;
int length = keyValue.size();
- map中查找及插入元素
// 通过迭代器使用find
map<int,int>::iterator ite = mapValue.find(a);
if(ite != keyValue.end()){
// 通过[key]查找值
keyValue[a] += b;
}
else{
keyValue.insert(pair<int,int>(a,b))
//或者 keyValue[a] = b;
}
- 迭代输出map
// 迭代输出map
map<int,int>::iterator begin = keyValue.begin();
map<int,int>::iterator end = keyValue.end();
for( ; begin!=end; begin++){
cout << begin->first << begin->second;
}
-
数字转字符串, 字符串转数字
1. 数字转字符串 to_string() (若为浮点数会自动补零,不推荐)
int a;
cin >> a;
string s = to_string(a);
//#include <sstream>
double a = 111.333;
string s;
s = to_string(a);
cout << s;
2. 数字转字符串 stringstream
// #include <sstream>
double a = 111.333;
string s;
stringstream b;
// << 方向重要
b << a;
b >> s;
cout << s;
3.字符串转数字
// #include <sstream>
string s = "111.333";
double a;
stringstream b;
// << 方向重要
b << s;
b >> a;
cout << a;
4. 字符串转数字 string 中的 stoi stod stof等函数
int a;
string s = "11";
a = stoi(s);
- 大小写字母
stl库函数
空白符:isspace()
判断字母(不区分大小写):isalpha();
大写字母:isupper();
小写字母:islower();
数字:isdigit();
字母和数字:isalnum();
转化为大写:toupper();
转化为小写:tolower();
6. vector
// 创建
// 一维
vector<string> queue;
vector<int> queue(n,1);
int a = queue[1];
int len = queue.size();
// 二维
vector<int vector<int> > table(2, vector<int>(2,0));
int array[100][100];
//#include<algorithm>
// 翻转
reverse(queue.begin(), queue.end())
7. 保留小数位, 实际小数位小于要保留的位数, 那么自动补零
double a;
cout << fixed << setprecision(2) << a;
8. string中的查找,截取字符串
string s = "hwllo";
// 截取
string s2 = s.substr(0,2);
s[0] // "h"
s.find(s) 返回下标;
1. s.find(str / char)
s.find("e") == -1; // true
s.find('e') == -1; // true
s.find('w') != string::nops // true
2. s.find(str / char, pos)
9. 获取ascii字符码值, 直接用字符 - 0, 如 'c' - 0;
- 字符串按字典序排序
// #include <iostream>
vector<string> words;
sort(words.begin(),words.end());
11. #include <math.h>
pow(1,3);
int b = sqrt(a);