vector的用法
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
bool cmp(int a, int b) {
return a < b;
}
int main()
{
vector<int> vec;
vec.push_back(1); //向后添加一个数
for (auto it = vec.begin(); it != vec.end(); it++) { // 指针类型
cout << *it;
}
vec.push_back(1);
vec.push_back(5);
vec.push_back(2);
//auto == std:: vector<int> :: iterator
sort(vec.begin(),vec.end(),cmp);
for (auto it =vec.begin(); it != vec.end(); it++){
cout << *it;
}
sort(vec.begin(), vec.end(), less<int>());// 从大到小
sort(vec.begin(), vec.end(), greater<int>());// 从小到大
vec.pop_back(); // 弹出后面的数
vec.erase(unique(vec.begin(), vec.end()), vec.end()); // 去重
vec.reverse(vec.begin(),vec.end());
return 0;
}
string的用法
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
void Stringsplit(const string& str, const char split, vector<string>& res)
{
if (str == "") return;
//在字符串末尾也加入分隔符,方便截取最后一段
string strs = str + split;
size_t pos = strs.find(split);
// 若找不到内容则字符串搜索函数返回 npos
while (pos != strs.npos)
{
string temp = strs.substr(0, pos);
res.push_back(temp);
//去掉已分割的字符串,在剩下的字符串中进行分割
strs = strs.substr(pos + 1, strs.size());
pos = strs.find(split);
}
}
// 使用字符串分割
void Stringsplit(const string& str, const string& splits, vector<string>& res)
{
if (str == "") return;
//在字符串末尾也加入分隔符,方便截取最后一段
string strs = str + splits;
size_t pos = strs.find(splits);
int step = splits.size();
// 若找不到内容则字符串搜索函数返回 npos
while (pos != strs.npos)
{
string temp = strs.substr(0, pos);
res.push_back(temp);
//去掉已分割的字符串,在剩下的字符串中进行分割
strs = strs.substr(pos + step, strs.size());
pos = strs.find(splits);
}
}
int main()
{
string str;
cin >> str;
cout << str<<endl;
for (int i = 0; i < str.size(); i++) {
if (str[i] =='1') {
cout << i << " " << "为1 ";
}
}
str.substr(0); //截取从0 到 最后的字符串
string s = str.substr(1, 4); //截取从位置1以后的4个字符
cout << "返回4的小标" << str.find('4')<<endl;
//按位置分割
// 使用字符串分词
vector<string> strList2;
string str2("ThisABCisABCaABCtest");
Stringsplit(str2, "ABC", strList2);
for (auto s : strList2)
cout << s << " ";
cout << endl;
return 0;
}
stack用法
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int main()
{
stack<int> st;
st.push(1);
st.push(2);
st.push(5);
st.push(8);
while (!st.empty()) {
cout<< st.top()<<endl; // 查看栈顶元素
st.pop();
}
return 0;
}
queue队列的用法
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int main()
{
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
while(q.size()) { // 可以用q.empty()
cout << q.front();//从第0个开始 先进先出原则
q.pop();
}
return 0;
}
deque双端队列
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int main()
{
deque<int> dq;
dq.push_front(1);
dq.push_back(5);
dq.push_front(10);
dq.push_back(10);
while (!dq.empty()) {
cout <<dq.front()<<" ";
cout <<dq.back()<<" ";
dq.pop_back();
dq.pop_front();
}
return 0;
}
priority_queue<类型,容器,排序(默认小顶堆)>用法
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
struct student {
int age;
int number;
bool operator<(const student& ee) const {
return age > ee.age; // 从年龄小的到大的
}
};
int main()
{
/*priority_queue<int, vector<int>, less<int>> pq; //大顶堆
pq.push(1);
pq.push(12);
while (pq.size()) {
cout << pq.top()<<" ";
pq.pop();
}*/
priority_queue<student, vector<student>> pq;
student a[3];
for (int i = 0; i < 3; ++i) {
int age;
int number;
cin >> age >> number;
a[i].age = age, a[i].number = number;
pq.push(a[i]);
}
while (!pq.empty()) {
student t = pq.top();
cout << t.age << t.number<<endl;
pq.pop();
}
return 0;
}
set用法(以红黑树为基础)
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int main()
{
set<int> s;//有排序
s.insert(5);
s.insert(4);
s.insert(3);
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
{
auto j = s.find(5);
if (j != s.end()){
cout << *j;
}
}
s.erase(3);
auto j = s.lower_bound(3); // 大于等于3的第一个数
return 0;
}
map用法(以红黑树为基础)
#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int main()
{
map<int, int> m;
int nums[5] = { 1,5,3,4,5 };
for (int i = 0; i < 5; i++) {
auto it = m.find(4 - nums[i]);
if (it != m.end()) {
cout<< it->second;
break;
}
m[nums[i]] = i;
}
m.insert({ 1,2 });
m.count(1);//键值的个数
m.clear();
return 0;
}
unordered_map(基于哈希表)
#include<bits/stdc++.h>
#include<iostream>
#include<unordered_map>
using namespace std;
int main()
{
unordered_map<int,int> m;
int nums[5] = { 1,5,3,4,5 };
for (int i = 0; i < 5; i++) {
auto it = m.find(4 - nums[i]);
if (it != m.end()) {
cout<< it->second;
break;
}
m[nums[i]] = i;
}
m.insert({ 1,2 });
cout <<m.count(1);//键值的个数
m.clear();
return 0;
}
unordered_set(基于哈希表)
#include<bits/stdc++.h>
#include<iostream>
#include<unordered_map>
#include<unordered_set>
using namespace std;
int main()
{
unordered_set<int> s;
//查看重复数字
int nums[] = {1,3,5,4,4};
for(int i = 0; i < 5; i++) {
if (s.find(nums[i]) != s.end()) {
cout << "找到重复的数字了数字为:" << nums[i] << endl;
}
s.insert(nums[i]);
}
return 0;
}