1、字符串处理
#include <string>
using namespace std;
string substr=str.substr(int i,int length);
string substr=str.substring(int i,int j);
2、哈希表基本操作
- 使用头文件 “unordered_set.h”
- 创建哈希表,使用unordered_set,注意两者区别:
#include <unordered_set>
using namespace std;
unordered_set<string> set1;
auto set2=unordered_set<string> ();
unordered_set<string> set3(set1.begin(), set1.end());
unordered_set<int> set4(arr,arr+5);
for(string str : set1) {
cout << str << endl;
}
for(unordered_set<string>::iterator it = set1.begin(); it != set1.end(); ++it){
cout << *it << endl;
}
set1.insert(str);
if(set1.erase(str)) cout<<"ok!"<<endl;
if(set1.find(str)!=set1.end()) cout<<"find!"<<endl;
3、容器类常用操作
- 获取 vector < int > 的最大值、最小值:
#include <vector>
using namespace std;
vector<int> arr;
arr.insert(1);
arr.insert(2);
int maxVal = *max_element(arr.begin(),arr.end());
int minVal = *min_element(arr.begin(),arr.end());
auto result = minmax_element(vec.begin(), vec.end());
int minVal = *result.first;
int maxVal = *result.second;