首页 > 其他分享 >STL的常用法

STL的常用法

时间:2022-12-26 18:44:35浏览次数:32  
标签:insert 常用 cout STL st int push include

一.队列和优先队列

队列是front,back可以访问队首队尾,优先队列用top访问优先级最高

#include<iostream>
#include<queue>
using namespace std;
struct st{
    int x;
    int y;
    friend bool operator<(const st T1,const st T2)
    {
        return T1.x>T2.x;
    }
};
int main()
{
    priority_queue<int,vector<int>,greater<int> > qu;
    qu.push(2);qu.push(3);qu.push(5);
    while(!qu.empty())
    {
        cout<<qu.top();//235
        qu.pop();
    }
    cout<<endl;
    priority_queue<st>qq;
    st t1,t2;
    t1.x=2;t2.x=3;
    qq.push(t2);qq.push(t1);
    while(!qq.empty())
    {
          cout<<qq.top().x;//23
          qq.pop();
    }
    return 0;
}

二.栈

栈和队列一样没有clear的清空函数,需要循环pop。栈先进后出,访问栈顶的元素用top。其他操作与队列类似。

.size()可以知道元素个数

#include<iostream>
#include<stack>
using namespace std;
int main()
{
    stack<int> s;
    s.push(2);s.push(3);s.push(4);
    cout<<s.size()<<endl;//3
    while(!s.empty())
    {
        cout<<s.top();//432
        s.pop();
    }
    return 0;
}

三.向量(动态数组)

vector可以用下标访问

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector<int> v;
v.push_back(2);v.push_back(6);v.push_back(8);
cout<<v.size()<<endl;//3
cout<<v.front();cout<<v.back()<<endl;//28
reverse(v.begin(),v.end());
//v.end()是尾地址加一类似于sort
for(int i=0;i<3;++i)
cout<<v[i];//862
cout<<endl;
v.pop_back();
cout<<v.front()<<endl;//8
v.pop_back();
cout<<v.empty()<<endl;//0
v.clear();
cout<<v.empty();//1
return 0;
}

四.集合

set是有序,互异的集合,可以在logn时间进行查找,插入,删除等操作。只能用迭代器访问

#include<iostream>
#include<set>
#include<algorithm>
using namespace std;
int main()
{
    set<int> s;
    s.insert(2);s.insert(7);s.insert(6);
    s.erase(2);
    cout<<s.count(2)<<endl;//0
    for(set<int>::iterator it=s.begin();it!=s.end();it++)
        cout<<*it<<" ";//6 7
    cout<<endl;
    set<int,greater<int> >st;
    st.insert(3);st.insert(8);st.insert(9);
    for(set<int,greater<int> >::iterator it=st.begin();it!=st.end();it++)
        cout<<*it<<" ";//9 8 3
    st.clear();
    cout<<endl<<st.empty();//1
    return 0;
}

五.string

string可以用“=”“+”等运算符,可以用“>”">=""<""<=""==""!="

#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
    int a[4]={1,2,3,4};
    do
    {
      cout<<a[0]<<a[1]<<a[2]<<a[3]<<endl;
    }while(next_permutation(a,a+4));//找全排列
    return 0;
}

 

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    string str("This is the world");
    string str2;
    str2 = str.substr(6, 3);
    cout << str2 << endl;
    str2 = str.substr(6);
    cout << str2 << endl;
    //str2.insert(2,"io");
    //str2.insert(2,str2);
    str2.insert(2, 3, 'i');//第三参不放字符串
    cout << str2 << endl;
    cout << str.find("is") << endl;//找第一次出现的位置
    cout << str.find("is", 4) << endl;//从4到结尾第一次出现的位置
    string str1("agbdcfe");
    sort(str1.begin(), str1.end());
    cout << str1 << endl;//abcdefg
    next_permutation(str1.begin(), str1.end());
    cout << str1 << endl;//abcdegf下一个全排列
    prev_permutation(str1.begin(), str1.end());
    cout << str1 << endl;//abcdefg上一个全排列
    return 0;
}

六.映射(map)

map是键值对容器,不能修改key,可以修改value,map会根据key自动排序

#include<iostream>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
    map<int,string> m;
    m[1]="zhangsan";
    m[2]="lisi";
    m.insert({2,"wangwu"});//若键值已经存在,就不再插入
    m.insert({3,"wangwu"});
    for(map<int,string>::iterator it=m.begin();it!=m.end();it++)
        cout<<it->first<<" "<<it->second<<endl;
    m.erase(2);//erase()里放键值
    cout<<m.count(2)<<endl;
    return 0;
}

 

标签:insert,常用,cout,STL,st,int,push,include
From: https://www.cnblogs.com/ruoye123456/p/17006478.html

相关文章

  • Robotframework 之常用断言关键字简介
    From: https://www.cnblogs.com/yanjiahong/p/15006244.html 任何自动化测试框架或实例中断言是必不可少的,Robotframework同样如此,那下面就介绍下其常用断言关键字。1......
  • robotframework常用断言关键字
    From: https://www.cnblogs.com/yanjiahong/p/15006257.html 定义两个函数${int} createlist 123${string} setvariable ilovepython${null} cre......
  • 盘点Python 中字符串的常用操作
    摘要:盘点Python中字符串的几个常用操作,对新手极度的友好。本文分享自华为云社区《​​盘点Python中字符串的常用操作,对新手极度友好​​》,作者:TT-千叶。在Python中字......
  • 盘点Python 中字符串的常用操作
    摘要:盘点Python中字符串的几个常用操作,对新手极度的友好。本文分享自华为云社区《盘点Python中字符串的常用操作,对新手极度友好》,作者:TT-千叶。在Python中字符串......
  • 分布式系统中的常用技术
    1、布隆过滤器Bloom过滤器是一种节省空间的概率数据结构,用于测试元素是否为某集合的成员。它用于我们只需要检查元素是否属于对象的场景。 在BigTable(和Cassandra)中,任......
  • Robot Framework - 常用断言讲解
    From: https://www.cnblogs.com/yanjiahong/p/15006607.html RobotFramework带有丰富的系统关键,使用时无需导入,直接使用,为写自动化用例带来了极大的方便;不能停留在知道......
  • Java基础之常用类(String类)
    String类定义String类代表字符串。Java程序中的所有字符串字面值(如"abc")都作为此类的实例实现。我们可以将字符串看作是String,但是严格意义上来说,String还是......
  • STL中map用法详解
    Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处......
  • javaScript 列表常用语法基础大全
    javascript数组常用方法1.push()=>语法,数组.push(数据)=>作用:向数组的末尾追加数据=>返回值:添加数据以后,返回新的数组2.pop()=>语法,数组.pop(数据)=>作用:删除......
  • STL string 常用函数
    string类的构造函数:string(constchar*s);//用c字符串s初始化string(intn,charc);//用n个字符c初始化此外,string类还支持默认构造函数和复制构造函数,如string......