STL简介
容器类
algorithm中的函数
#include<algorithm>
using namespace std;
一些示例
queue#
#include<iostream>
#include<queue>
using namespace std;
/*push(); pop(); empty(); size()*/
/*queue: front(), back();*/
/*priority_queue && stack: top()*/
struct fruite
{
string name;
double price;
/*overloading '>' is prohibited*/
friend bool operator < (fruite a, fruite b)
{
return a.price < b.price;
}
};
int main()
{
/*queue*/
queue<int> q;
if(q.empty() == true) cout<<"Queue is Empty!\n";
else cout<<"Queue is not Empty!\n";
for(int i = 0; i < 5; i++)
q.push(i);
cout<<q.front()<<endl;
cout<<q.back()<<endl;
while(q.empty() != true)
q.pop();
cout<<q.size()<<endl;
cout<<"-----"<<endl;
/*priority_queue*/
priority_queue<int> p;
// priority_queue<int, vectoe<int>, less<int>) p;
// priority_queue<int, vector<int>, greater<int>> p;
p.push(1);
p.push(10);
p.push(7);
p.push(6);
cout<<"size = "<<p.size()<<endl;
cout<<p.top()<<endl;
p.pop();
cout<<p.top()<<endl;
cout<<"-----"<<endl;
/*Overload operator '<'*/
priority_queue<fruite> f;
struct fruite f1, f2, f3;
if(f.empty() == true) cout<<"f is Empty!"<<endl;
f1.name = "Apple";
f1.price = 4.3;
f2.name = "Banana";
f2.price = 2.3;
f3.name = "Grape";
f3.price = 5.0;
f.push(f1);
f.push(f2);
f.push(f3);
cout<<f.top().name<<endl;
}
string#
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int main()
{
string s = "abc";
cin>>s;
cout<<s;
printf("\n");
string::iterator it;
/*it左闭右开区间:[ s.begin(), s.end() )*/
/*相关函数都遵循左闭右开原则*/
for(it = s.begin(); it < s.end(); it++)
printf("%c ", *it);
printf("\n");
for(int i = 0; i < s.length(); i++)
printf("%c ", s[i]);
printf("\n");
string str1="hello", str2=" everyone!", str3;
str3 = str1 + str2;
cout<<str3<<endl;
if(str1>str2) cout<<"str1>str2"<<endl;
/*1. insert()*/
str1 = "abcde"; str2 = "fghij";
/*insert(pos, str2)*/
str1.insert(5, str2);
cout<<str1<<endl;
str1 = "abcde";
/*insert(it, it1, int2)*/
str1.insert(str1.end(), str2.begin(), str2.begin()+2);
cout<<str1<<endl;
/*2. erase()*/
str1 = "abcde";
/*erase(it)*/
str1.erase(str1.begin()); /*bcde*/
cout<<str1<<endl;
/*erase(it1, it2)*/
str1.erase(str1.begin()+2, str1.end()); /*bc*/
cout<<str1<<endl;
/*erase(pos, len)*/
str1 = "abcde";
str1.erase(1, 3);
cout<<str1<<endl;
/*3. substr(pos, len)*/
str1 = "abcde";
cout<<str1.substr(0,4)<<endl;
/*4. find()*/
str1 = "Thank you for your smlie!";
str2 = "you";
str3 = "me";
/*find(str2)*/
if(str1.find(str2) != string::npos)
cout<<str1.find(str2)<<endl;
if(str1.find(str2, 7) != string::npos)
cout<<str1.find(str2, 7)<<endl;
/*find(pos, str2)*/
if(str1.find(str3) != string::npos)
cout<<str1.find(str3)<<endl;
else
cout<<"I don't know where is 'str3'."<<endl;
/*5. replace()*/
str1 = "Maybe you will turn around.";
str2 = "will not";
str3 = "Surely";
/*replace(pos,len,str2)*/
str1.replace(10,4,str2);
cout<<str1<<endl;
/*replace(it1,it2,str2)*/
str1.replace(str1.begin(), str1.begin()+5, str3);
cout<<str1<<endl;
}
reverse#
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
int a[10] = {10,11,12,13,14,15};
reverse(a, a+4);
for(int i = 0; i < 6; i++)
printf("%d ", a[i]);
}
sort#
#include<stdio.h>
#include<algorithm>
using namespace std;
#define n 7
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
int i,j,k;
int a[7] = {3,23,4,42,43,12,34};
char s[7] = {'a','c','q','e','w','h','e'};
sort(a,a+n,cmp);
sort(s,s+n);
for(i = 0; i < 7; i++)
printf("%d ", a[i]);
printf("\n");
for(i = 0; i < 7; i++)
printf("%c ", s[i]);
}
cmp函数
//从大到小排序
bool cmp(int a, int b) return a > b;
bool cmp(char a, char b) return a > b;
//结构体数组排序
/*struct node包含x,y两个成员*/
bool cmp(node a, node b) return a.x > b.x;
/*若a.x == b.x则比较y*/
bool cmp(node a, node b)
{
if(a.x != a.x) return a.x > b.x;
else return a.y > b.y;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用