字符串与数字 互转
#include <sstream>
#include <string>
int x = 1232;
stringstream ss;
ss << x;
string s = ss.str();
int num;
stringstream ss(s);
ss >> num;
C++定义排序规则
相关练习:剑指 Offer 45. 把数组排成最小的数sort(strs.begin(), strs.end(), [](string& x, string& y){ return x + y < y + x; });
- lambda 表达式参考 https://blog.csdn.net/guihunkun/article/details/116162749
cmp 函数参考 https://blog.csdn.net/qq_40640074/article/details/113367413
案例
对vector 中 pair
的第二位进行排序,然后再对第一位字符串排序 - cmp
```cpp
bool cmp(const pair
&a, pair &b) { if (a.second != b.second) { return a.second > b.second; } else { return a.first < b.first; } }
- cmp
```cpp
bool cmp(const pair
sort(cntVec.begin(), cntVec.end(), cmp);
- lambda,针对比较简单的排序,比如仅对第二位排序
```cpp
sort(cntVec.begin(), cntVec.end(), [](const pair<string,int>&a, const pair<string,int>&b) {return a.second > b.second;});
输入带空格的字符串
string s;
getline(cin, s);
substr
sort()从大到小排
参考sort(a.begin(),a.end(),greater<int>())
数组求和
accumulate(height.begin(), height.end(), 0)
从输入中按,分割字符串
例题
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
int main (){
string s;
vector<string> v;
vector<string>::iterator it;
while(cin>>s){
stringstream ssr(s);
string split;
while(getline(ssr,split,',')) // ,为分割
v.push_back(split);
if(cin.get()=='\n'){
sort(v.begin(),v.end());
for(it=v.begin();it!=v.end();it++){
cout<<*it;
if(it!=v.end()-1) cout<<",";
}
cout<<endl;
v.clear();
}
}
return 0;
}
优先队列用法
文件操作
内置位计数函数
- 计算二进制表达 x 中 1 的个数
__builtin_popcount(x);