• stl粗略用法


    【set】

    存进去就直接去重排序 ,需要头文件<set>引用

     1、头文件——<set>

     2、定义——set<int>Q;

     3、输入(插入)——insert(x);

     4、删除制定元素——erase(x);

     5、清空——clear();

     6、判空——empty();

     7、大小——size();

        int a[105];
        set<int> s;
        REP(i,1,5) scanf("%d",&a[i]),s.insert(a[i]);
        set<int>::iterator it;
        for(it = s.begin(); it != s.end(); it++){  
            cout << *it << " ";  
        }
        
    View Code

    【algorithm】

    1、sort:排序神器,后面可加cmp函数

    2、unique:去重

        int a[105],n=5;
        REP(i,1,n) scanf("%d",&a[i]);
        sort(a+1,a+1+n);
        n=unique(a+1,a+1+n)-a-1;
        REP(i,1,n) printf("%d ",a[i]);
    View Code

     3、lower_bound & upper_bound:

    对于upper_bound来说,返回的是被查序列中第一个大于查找值的指针,也就是返回指向被查值>查找值的最小指针,lower_bound则是返回的是被查序列中第一个大于等于查找值的指针,也就是返回指向被查值>=查找值的最小指针

    4、max & min

    5、abs(fabs)

    6、swap

    7、reverse

    7、next_permutation:给出全排列的下一个序列

    8、fill:数组类型对应范围中的任意值

     fill(a,a+5,233) //将a[0]~a[4]均赋值为233 

    【vector】

    1、通过迭代器访问

    vector<int> vi;
    for(i=1;i<=5;i++){
        vi.push(i);
    }
    vector<int>::iterator it=vi.begin();
    for(i=0;i<5;i++)
        printf("%d ",*(it+i) );
        
    for(vector<int>::iterator it=vi.begin();it!=vi.end();it++)
        printf("%d ",*it);
    View Code

    只有string和vector能*(it+i)访问

    2、插入:

    push_back:在队尾插入

    pop_back:在队头插入

    时间O(1)

    insert:在任意出插入

     vi.insert ( vi.begin() + 2 /*位置*/ , i/*插入值*/ ) 

    时间O(n)

    3、删除:

    erase():道理同insert

     vi.erase(vi.begin()+1,vi.begin()+4); 

    erase(first,last)即删除[first,last)内所有元素

    4、clear():清空所有元素,时间O(n)

    【string】

    1、迭代器访问:

    不需要像其他stl需要参数,可以直接

     string::iterator it; 

    2、operator+=:

    string s1="abc",s2="xyz",s3;
    s3=s1+s2;
    s1+=s2;
    

    compare operator :比较规则字典序

    3、length() & size()

    4、insert() & erase() 相同

    5、substr:

    string str="Thank you for your smile."
    cout << str.substr ( 0/*起始位置*/ , 5/*长度*/ ) << endl ;
    cout<<str.substr(14,4)<<endl;//空格有算
    //输出Thack  your

    6、find:

    当s2是s1的子串时,返回s1第一次出现的位置,如果不是则返回npos(其本身值为-1或者4294967295)

    时间复杂度为o(nm)

    7、replace:

    str.replace(it1,it2,str2)把str的迭代器[it1,it2)范围的子串替换成str2

    时间复杂度O(str.length())

    【map】

    1、erase:

    单个删除:时间复杂度O(1),key值删除时间复杂度为O(logN),N为map中映射个数

    删除区间:左开右闭,时间复杂度O(last-first)

    map<char,int> mp;
    mp['a']=1;
    mp['b']=2;
    mp['c']=3;
    map<char,int>::iterator it=mp.find('b');
    mp.erase(it);    //删除b 2
    for(map<char,int>::iterator it=mp.begin();it!=mp.end();it++)
        printf("%c %d
    ",it->first,it->second);
    View Code

    2、find:

    时间复杂度为O(logN),N为map中映射个数

    【queue】

     1、front队首 & back队尾

    时间复杂度o(1)

    2、 push入队

      pop队首出队

      时间复杂度o(1)

    3、priority_queue

    【stack】

     1、 push入栈

         pop出栈

      时间复杂度o(1)

    2、top:访问栈顶元素,时间复杂度o(1)

    【pair】

     自带内部有两个元素的结构体

    两个参数分别对应first和second的数据类型

    【bitset】

    用处:

    b.any() b中是否存在置为1的二进制位?
    b.none() b中不存在置为1的二进制位吗?
    b.count() b中置为1的二进制位的个数
    b.size() b中二进制位的个数
    b[pos] 访问b中在pos处的二进制位
    b.test(pos) b中在pos处的二进制位是否为1?
    b.set() 把b中所有二进制位都置为1
    b.set(pos) 把b中在pos处的二进制位置为1
    b.reset() 把b中所有二进制位都置为0
    b.reset(pos) 把b中在pos处的二进制位置为0
    b.flip() 把b中所有二进制位逐位取反
    b.flip(pos) 把b中在pos处的二进制位取反
    b.to_ulong() 用b中同样的二进制位返回一个unsigned long值
    os << b 把b中的位集输出到os流

    bitset中的一个元素一般只占1 bit,相当于一个char元素所占空间的八分之一

  • 相关阅读:
    1028. Hanoi Tower Sequence
    sicily 1063. Who's the Boss
    ubuntu 12.04 x86_64:java.lang.UnsatisfiedLinkError: Could not load SWT library. Reasons
    ubuntu12.04 desktop默认无ssh支持
    取消putty右键粘贴功能
    gcc编译参数之m32 m64
    Invalid command 'RailsBaseURI'
    Ubuntu 12.4 server 安装 redmine
    查看Samba用户的方法
    【转】windows浏览共享切换用户登录的方法
  • 原文地址:https://www.cnblogs.com/EvfX/p/8657062.html
Copyright © 2020-2023  润新知