以下是STL算法的一些基本用法,希望对大家有帮助哦!
最好是可以照着自己敲一次,理解记忆这些用法~~~
代码段:
#include<bits/stdc++.h>
using namespace std;
class Student
{
public:
Student(int _age, char* _name)
{
this->age = _age;
strcpy(this->name, _name);
}
void printS()
{
cout << this->age<<"+"<<this->name<< endl;
}
public:
int age;
char name[100];
};
struct sort_func //伪函数,类似java选择器
{
bool operator() (const Student &s1, const Student &s2)
{
return s1.age < s2.age;
}
};
int main()
{
//_________________________string____________________________________________________
/*//字符串的初始化
string s1="abc"; //char* s1
string s1("abc");
string s3(4,'a');
cout<<s1<<"+"<<s2<<"+"<<s3<<endl;
//字符串 数组的遍历
for(int i=0;i<s1.length()+3;i++)
cout<<s1[i]<<endl;
//cout<<s1.at(i)<<endl; //开大数组时,at会抛出异常
//字符串 迭代器的遍历
string::iterator it;
for(it=s1.begin();it!=s1.end();it++)
{
cout<<*it<<endl;
}
//字符串复制
string s1("abcdef");
char buf[100]={0};
s1.copy(buf,6,0); //将s1从0开始的6个字符复制给buf
cout<<buf<<endl;
//连接字符串
string s2="abc";
string s3="def";
s2=s2+s3;
s2.append(s3);
cout<<s2<<endl;*/
//字符串的查找
/*string s3="aaahddaaajafaaa";
int index=s3.find("aaa"); //找到了输出0,找不到输出-1
cout<<index<<endl;
//字符串的替换
string s3="aaahddaaajafaaa";
int cnt=0;
while(s3.find("aaa",cnt)!=-1)
{
int index=s3.find("aaa");
s3.replace(index,3,"AAA"); //从index开始替换3个字节
cnt=index+3;
}
string::iterator it;
for(it=s3.begin();it!=s3.end();it++)
{
cout<<*it;
}
cout<<endl;
//字符串的删除、插入
string s1="aaahddaaajafaaa";
int cnt=0;
while(s1.find("aaa",cnt)!=-1)
{
int index=s1.find("aaa",cnt);
s1.erase(index,3); //从index开始的3个字符
s1.insert(index,"AAA");
cnt=index+3;
}
for(string::iterator it=s1.begin();it!=s1.end();it++)
{
cout<<*it;
}
cout<<endl;
//字符的大小写转化
string s1 = "asdsjf";
string s2;
s2.resize(s1.size());
transform(s1.begin(), s1.end(), s2.begin(),toupper);//转为大写,重载函数toupper
transform(s1.begin(), s1.end(), s2.begin(),tolower);//转为小写,重载函数tolower
for (string::iterator it = s1.begin(); it != s1.end(); it++) {
cout << *it << endl;
}
//_______________vector______________________________________________
//初始化
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
int size=v1.size();
int num=count(v1.begin(),v1.end(),3); //统计3的个数
int last=v1.back(); //获取最后一个数
int first=v1.front(); //获取第一个数
v1.pop_back(); //删除最后一个数
for(vector<int>::iterator it = v1.begin();it!=v1.end();it++)
{
cout<<*it;
}
cout<<endl;
for(vector<int>::reverse_iterator it =v1.rbegin();it!=v1.rend();it++) //逆向遍历
cout<<*it;
cout<<endl;
//插入
vector<int>v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
vector<int> v2;
v2.push_back(4);
v2.push_back(5);
v2.push_back(6);
v1.insert(v1.begin(),4); //{4,1,2,3}
for(vector<int>::iterator it=v1.begin();it!=v1.end();it++)
{
cout<<*it;
}
cout<<endl;
v1.insert(v1.begin(),2,33); //{33,4,1,2,3}
v1.insert(v1.end(),v2.begin(),v2.end()); //{33,4,1,2,3,4,5,6}
vector<int>::iterator it=find(v1.begin(),v1.end(),3);
cout<<"3的下标是:"<<distance(v1.begin(),it)<<endl; //通过两个指针的距离求下标
//_______stack___________________________________
//先进后出
stack<int> s1;
for(int i=0;i<10;i++)
{
s1.push(i);
}
cout<<s1.size()<<endl;
while(!s1.empty())
{
int m=s1.top(); //删除栈顶
cout<<m<<endl;
s1.pop();
}
//____________queue________________________________
//先进先出
queue<int> q1;
q1.push(1);
q1.push(2);
q1.push(3);
q1.push(4);
cout<<"队列长度:"<<q1.size()<<endl;
while(!q1.empty())
{
int tmp=q1.front();
cout<<tmp<<endl;
q1.pop();
}
//_____________priority_queue_________________________________
priority_queue<int> pq; //默认最大值优先级队列,从大到小
//priority_queue<int,vector<int>,less<int> >pq;
//priority_queue<int,vector<int>,greater<int>>pq; //最小值优先级队列
pq.push(1);
pq.push(2);
pq.push(3);
pq.push(4);
cout<<"队列长度:"<<pq.size()<<endl;
cout<<"队头:"<<pq.top()<<endl;
//__________list___________________________________________
list<int> li;
li.push_back(1);
li.push_back(2);
li.push_back(3);
li.push_back(4);
//cout<<"list长度:"<<li.size()<<endl;
//插入
/*int n=3;
list<int>::iterator it=li.begin();
for(int i=0; i<n; i++)
{
it++;
}
li.insert(it,0); //在第n个位置前插入
for (list<int>::iterator it = li.begin(); it != li.end();it++) //遍历
{
cout << *it << endl;
}
//删除
li.erase(li.begin()); //删除链首
//li.erase(li.begin(),li.end());
li.remove(4); //删除所有值为4的数
for (list<int>::iterator it = li.begin(); it != li.end();it++) //遍历
{
cout << *it << endl;
}
//___________________set______________________________________-
set<int> s1; //默认从小到大
set<int,less<int> > s2; //从大到小
//插入
for(int i=0;i<5;i++)
{
s1.insert(rand());
}
s1.insert(100);
s1.insert(100); //只插入一次
for(set<int>::iterator it=s1.begin();it!=s1.end();it++)
{
cout<<*it<<endl;
}
//删除集合
while(!s1.empty())
{
set<int>::iterator it=s1.begin();
cout<<*it<<endl;
s1.erase(s1.begin());
}
Student stu1(1,"alice");
Student stu2(3,"bob");
Student stu3(2,"petter");
set<Student,sort_func> s1;
s1.insert(stu1); //可插入结构体
s1.insert(stu2);
s1.insert(stu3);
for(set<Student,sort_func>::iterator it=s1.begin(); it!=s1.end();it++)
{
cout<<it->age<<"+"<<it->name<<endl;
}
set<int> s1;
for(int i=0;i<5;i++)
{
s1.insert(i+1);
}
cout<<"值为3的个数:"<<s1.count(3)<<endl;
for(set<int>::iterator it=s1.begin();it!=s1.end();it++)
{
cout<<*it<<endl;
}
set<int>::iterator it1=s1.find(3);//返回3的迭代器位置
set<int>::iterator it2=s1.lower_bound(2); //返回2的第一个可安插位置,也就是“元素值>=2”的第一个元素位置,为2(序列为1,2,3,4,5)
set<int>::iterator it3=s1.upper_bound(4);//返回4的最后一个可安插位置,也就是“元素值>4”的第一个元素位置,为5
cout<<*it1<<*it2<<*it3<<endl;v 输出为3,2,5
map<int,string > map1;
map1.insert(pair<int,string>(1,"hello1"));
map1.insert(pair<int,string>(2,"hello2"));
map1.insert(pair<int,string>(3,"hello3"));
map1[4]="hello4";
map1[5]="hello5";
for(map<int,string>::iterator it=map1.begin(); it!=map1.end();it++)
{
cout<<it->first<<"+"<<it->second<<endl;
}
//删除
while(!map1.empty())
{
map<int,string>::iterator it=map1.begin();
map1.erase(it);
}
//查找
map<int,string>::iterator it=map1.find(3);
cout<<it->first<<"+"<<it->second<<endl;*/
//______________
system("pause");
return 0;
}