C++ stringstream类的用法
一、类型转换-数字->字符串
#include<bits/stdc++.h>
using namespace std;
int main()
{
stringstream ss;
int num = 35;
string s;
ss << num;
ss >> s;
cout << s << endl;
return 0;
}
二、类型转换--字符串->数字
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s = "123";
stringstream ss;
int num;
ss << s;
ss >> num;
cout << num << endl;
return 0;
}
三、以空格解析字符串
#include <iostream>
#include <sstream>
#include<string>
using namespace std;
int main(void)
{
stringstream ss;
string s="2 3 4 5 6";
int sum = 0;
ss << s;
int num;
while (ss>>num) {
sum += num;
}
cout << sum << endl;
return 0;
}
二、欧几里得算法
int gcd(int a,int b){
if(b==0)return a;
gcd(b,a%b);
}
三、二分查找答案
const int N = 0x3f3f3f3f;
int main()
{
int ans, l, r;
int l = a, r = b;
while (l <= r) {
mid = (l + r) / 2;
if (check(mid)) {
ans = mid;
mid + l + 1;
}
else mid = r - 1;
}
cout << ans << endl;
return 0;
}
四、双端队列deque
push_back 在末尾插入元素,不断扩张队列
push_front 在队头覆盖元素 不会扩张队列
#include<bits/stdc++.h>
using namespace std;
int main()
{
deque<int> d;
d.push_back(1);
d.push_back(2);
cout << d.front() << endl; //队头
cout << d.back() << endl;//队尾
d.push_front(10);
cout << d[0] << ' ' << d[1] << endl;
return 0;
}
五、优先队列
1、优先队列的定义
priority_queue<a,vector<a>,greater<a> >que1;
priority_queue<a,vector<a>,less<a> >que2;
因为优先队列其实是一个堆
greater 是从小到大
less是从大到小
默认是从大到小
2、重载运算符
bool operator<(A a1,A a2){
return a1.r<a2.r;
}
bool operator(A a1,A a2){
return a1.l>a2.l;
}
大于号>的重载对应了greater的重载,是根据重载规则从小到大排序
小于号<的重载对应了less的重载,是根据重载规则从大到小排序
六、set
set里面每个元素只存有一个key值,它支持高效的关键字查询操作,比如检查一个关键字是否在set中。如果这个key值之前存在的话就不插入。
#include<bits/stdc++.h>
using namespace std;
int main()
{
set<int> s;
s.insert(2);
set<int>::iterator iter;
iter = s.find(2);
cout << *iter << endl;
s.erase(2);
cout << s.count(2) << endl;
cout << s.count(1) << endl;
return 0;
}
七、map
#include<bits/stdc++.h>
using namespace std;
int main()
{
map<int, string> m;
m[1] = "a";
m[2] = "b";
cout << m[1] << endl;
cout << m[2];
map<int, string>::iterator iter;
iter = m.begin();
for (iter; iter != m.end(); ++iter) {
cout << iter->first << ' ' << iter->second << endl;
}
return 0;
}
八、reverse
reverse反转字符串
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main() {
string N;
cin>>N;
reverse(N.begin(), N.end());
cout<<N<<endl;
}
reverser反转字符数组
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int main() {
char a[101];
cin.getline(a,sizeof(a));
int m=strlen(a);
reverse(a,a+m);
puts(a);
}
九、快速幂
typedef long long ll;
ll quick_pow(ll x,ll n,ll m){
ll res=1;
while(n>0){
if(n&1)res=res*x%m;
x=x*x%m;
n>>=1;
}
return res;
}
十、去重函数 unique
#include <iostream>
#include <algorithm> // sort(), unique()
#include <functional> // less<int>()
using namespace std;
int main()
{
int i;
int a[10] = { 0, 7, 7, 6, 1, 1, 5, 5, 8, 9 };
sort( a, a + 10, less<int>() ); // 排序
int n = unique(a, a + 10) - a; // 去重
for ( i = 0; i < n; i++) // 注意i < n
cout << a[i] << " "; // 0 1 5 6 7 8 9
}
十一、map中的count和find函数
#include<bits/stdc++.h>
using namespace std;
int main()
{
map<string, int> m;
m.insert(make_pair("test1", 1));
m["test2"] = 2;
map<string, int>::iterator it;
it = m.find("test2");
cout << it->second << endl;
cout << m.count("test1") << endl;
cout<<m.count("test2")<<endl;
return 0;
}