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;
}