1. 键盘输入
(1)get方法:
char name[50];
cin.get(name,50);
cin.get();
(2)getline方法:获取一整行句子,句子中可能包含空格等
#include <string> // getline的头文件
string s;
getline(cin,s);
(3)cin:
string s;
cin>>s; //cin遇到空格则停止,因此如果输入字符串有空格则不能使用cin
例如我输入:bbb ccc
结果是:bbb
(4)输入N行,每行是由空格分开的多个字符串,但是不知道有多少个
void help() {
int N;
cin >> N;
vector<vector<string>> info(N,vector<string>());
// 输入每行
for (int i = 0; i < N; i++) {
char c = '1';
string s;
while (c != '\n') { // 若是换行符,则结束该行的读取
cin >> s; // 读取一个字符串
c = getchar(); // 读取一个字符
info[i].push_back(s);
}
}
// 计算
// ...
}
测试用例:
2
aa 1 2
bb 1 2
2. 字符串函数
(1)子字符串函数
//substr有2种用法:
string s = "0123456789";
string sub1 = s.substr(5); //只有一个数字5表示从下标为5开始一直到结尾:sub1 = “56789”
string sub2 = s.substr(5, 3); //从下标为5开始截取长度为3位:sub2 = “567”
//substr,而不是subStr,笔试的时候写错了,--!
(2)atoi()和stoi()函数的用法和区别
相同点:
①都是C++的字符处理函数,把数字字符串转换成int输出。
不同点:
①atoi()的参数是 const char* ,因此对于一个字符串str我们必须调用 c_str()的方法把这个string转换成 const char类型的,而stoi()的参数是const string,不需要转化为 const char*;
②stoi()的头文件#include< string>。
string s="123";
int a;
a = atoi(s.c_str());
②stoi()会做范围检查,默认范围是在int的范围内的,如果超出范围的话则会runtime error!
string s="12333333333333333";
int a;
a = stoi(s); //报错,因此在不清楚字符串长度的情况下,少使用stoi
而atoi()不会做范围检查,如果超出范围的话,超出上界,则输出上界,超出下界,则输出下界;
代码:
string s="12333333333333333";
int a;
a = atoi(s.c_str()); //a=2147483647
(3)字符串大小比较
定义:在C++中定义了几种用于比较字符串的运算符,用于逐一比较string对象中的字符,并且有大小写敏感。并且定义了两字符串比较的规则:两字符串从第一个字符开始比较,如果前n个都相同,则第n+1个大的字符串比较大;也就是说并不是短的字符串就一定小。
例如Cap,cap,boat排序为,Cap,boat,cap
int main()
{
int n;
while(cin>>n)
{
vector<string> ss(n);
for(int i=0;i<n;i++)
{
cin>>ss[i];
}
for(int i=0;i<ss.size();i++)
{
for(int j=i+1;j<ss.size();j++)
{
//定义:在C++中定义了几种用于比较字符串的运算符,用于逐一比较string对象中的字符,
//并且有大小写敏感。并且定义了两字符串比较的规则:两字符串从第一个字符开始比较,
//如果前n个都相同,则第n+1个大的字符串比较大;也就是说并不是短的字符串就一定小,
//例如Cap,cap,boat排序为,Cap,boat,cap
if(ss[i]>ss[j]) //C++字符串比较,
{
swap(ss[i], ss[j]); //交换,swap函数在std中
}
}
}
for(auto &s:ss)
cout<<s<<endl;
}
return 0;
}
3. 字符数组char [] 与字符串string 相互转换:
//char [] -> string
char ch[4] = { 'a','b','c','d' };
//方法1
string s(ch); //这里会随机拷贝,直至遇到'\0',不安全
s = s.substr(0, 4); //截取字符数组的长度
//方法2
string s1;
for (auto c : ch)
s1 += c;
//string -> char []
string s="abcd";
char * ch=new char [s.length()];
strcpy(ch, s.c_str()); //c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同
4. sort 函数:
包含头文件:#include< algorithm>
//默认升序排列
vector<int> nums({ 1,3,6,4,9,7 });
sort(nums.begin(), nums.end()); //nums={1,,3, 4, 6, 7, 9}
若果要降序排列,则自定义降序函数:
bool cmp(int x, int y)
{
return x > y;
}
int main()
{
vector<int> nums({ 1,3,6,4,9,7 });
sort(nums.begin(), nums.end(),cmp); //nums={9, 7,6, 4,3, 1}
return 0;
}
5. 大顶堆与小顶堆
包含头文件:#include< queue>
vector<int> nums({ 1,4,3,7 });
priority_queue<int, vector<int>, less<int>> big_q; //大顶堆,或者priority_queue<int> q; 默认的是大顶堆
priority_queue<int, vector<int>, greater<int>> small_q; //小顶堆
for (auto e : nums)
{
big_q.push(e);
small_q.push(e);
}
//big_q={7,4,3,1}
//small_q={1,4,3,7}
(1)less这是大顶堆,按值大的优先,值大的在最上面。greater这是小顶堆,按值小的优先,值小的在最上面。
(2)自定义cmp函数:
struct cmp
{
bool operator()(const int &a,const int &b)//这里的&是引用
{
return a>b;//最大堆
return a<b;//最小堆
}
};
priority_queue< int, vector<int>, cmp >
①注意,一般ACM中用结构体内含“bool operator()(const int &a,const int &b)”。这其实等价于Class cmp,不过更省事,当然也不规范(不需要规范)。
②return就是希望如何排列为true。如果希望由大到小,就将大到小的情况return;反则亦然。和sort的自定义cmp是一样的。
6. 二分查找函数: lower_bound、upper_bound 、binary_search
头文件: #include < algorithm>
二分查找的函数有 3 个:
lower_bound(起始地址,结束地址,要查找的数值) 返回的是数值 第一个 出现的位置。
upper_bound(起始地址,结束地址,要查找的数值) 返回的是 第一个大于待查找数值 出现的位置。
binary_search(起始地址,结束地址,要查找的数值) 返回的是是否存在这么一个数,是一个bool值。
注意:使用二分查找的前提是数组有序。
7. map与set
(1)基本知识
#include<map> //map, multimap
#include<set> //set, multiset
#include<unordered_map> //unordered_map, unordered_multimap
#include<unordered_set> //unordered_set, unordered_multiset
unordered_map不允许重复,键一样则算重复。
unordered_multimap可以重复,允许有键一样的。
(2)multiset、multimap的删除问题
//multiset的例子
multiset<int> mst{ 0,1,1,2,3 };
mst.erase(1); //1全部被删除, mst={0,2,3}
multiset<int> mst1{ 0,1,1,2,3 };
mst1.erase(mst1.find(1)); //只删除1个1, mst1={0,1,2,3}
//multimap的例子
multimap<int, int> mmp{ {0,5},{1,6},{1,7},{2,8},{3,9} };
mmp.erase(1); //1全部被删除, mmp={{0,5},{2,8},{3,9}}
multimap<int,int> mmp1{ {0,5},{1,6},{1,7},{2,8},{3,9} };
mmp1.erase(mmp1.find(1)); //只删除1个1, mmp1={{0,5},{1,7},{2,8},{3,9}}
8. 各种结构需要包含的头文件
//系统调用
#include<iostream>
#include<windows.h> //hrtimer, LARGE_INTEGER, QueryPerformanceFrequency(Counter)
#include<sys/timeb.h> //timeb
#include<cstdlib> //atof,atoi,atol..., rand,srand, calloc,free,malloc,realloc...
//数据结构
#include<vector> //vector
#include<map> //map, multimap
#include<unordered_map> //unordered_map, unordered_multimap
#include<unordered_set> //unordered_set, unordered_multiset
#include<queue> //priority_queue
#include<deque> //deque
#include<string> //string
//多线程
#include<thread> //thread
#include<mutex> //mutex
#include<condition_variable> //condition_variable
//其他
#include<algorithm> //sort, min, max,
#include<limits.h> //INT_MAX, CHAR_BIT, LONG_MAX, LLONG_MAX
#include<float.h> //FLT_MAX, DBL_MAX