zoukankan      html  css  js  c++  java
  • STL基础总结

                        STL

    1. vector 不定长数组

      声明:

             vector<int> a           vector<double>b      vector<node> c

      a.size()读取它的大小             a.push_back(x) 向尾部添加x

      a.resize(x)改变大小,改为x                a.pop_back() 删除最后一个元素

      a.cllear() 清空                   a.empty() 测试是否为空

       排序  sort(a.begin(),a.end(),cmp);

    1. set 集合(元素不会重复 并且已从小到大排好序)

      set<int> s;

      s.insert(x) 向集合s插入x

      遍历:迭代器

      for(set<int>::iterator it = s.begin();it!=s.end();++it)

      cout<<*it<<endl;

      m.find() 函数

      找到返回元素地址值

      找不到返回.end()值

    1. map 从key到value的映射 关联数组

      map<int,int>m   key为m.first  value为m.second

      例:用map<string,int>month_name来表示“月份名字 到 月份编号”的映射

      用month_name[“July”]=7来赋值

      map插入方法:

      <1>. map<int,string> m;

      pair<map<int,string>::iterator,bool> pair1 ,pair2,pair3;

      //1.方法

      pair1 = m.insert(pair<int,string>(1,"teacher01"));

      pair2 = m.insert(pair<int,string>(2,"teacher02"));

      pair3 = m.insert(pair<int,string>(2,"teacher02"));

      if (pair2.second)

      {

                 cout<<"2 teacher02插入成功"<<endl;

                 cout<<pair2.first->first<<":"<<pair2.second<<endl;

      }

      else

      {

               cout<<"2 teacher02插入失败"<<endl;

               cout<<pair2.first->first<<":"<<pair2.second<<endl;

      }

      //<2>.方法

      m.insert(make_pair(3,"teacher03"));

      m.insert(make_pair(4,"teacher04"));

      //<3>.方法

      m.insert(map<int,string>::value_type(5,"teacher05"));

      m.insert(map<int,string>::value_type(6,"teacher06"));

      //<4>. 如果key相等 会修改对应的value

      m[7] = "teacher07";

      m[8] = "teacher08";

      m[0] = "teacher09";

      m[0] = "teacher00";

      //遍历

      <1>

      for (map<int,string>::iterator it = m.begin();it!=m.end();it++)

      {

            cout<<(*it).first<<endl;

               cout<<(*it).second<<endl;

      }

      <2>

      while (!m.empty())

      {

               map<int,string>::iterator it = m.begin();

               cout<<it->first<<" "<<it->second<<endl;

               m.erase(it);

      }

      (此处参考自https://blog.csdn.net/bbs375/article/details/52709367)

      m.find() 函数

      找到返回元素地址值

      map<char,int> mymap

      cout<<mymap.find('b')->second

      找不到返回mymap.end()值

    1. 栈 队列 优先队列

      栈

      stack<int> s

      push()和pop()实现元素的入栈和出栈操作

      top()取栈顶元素(但不删除)

      队列

             queue<int> q  

             用push()和pop()进行元素的入队(队尾)和出队(队首)操作

             front()取队首元素(但不删除)

  • 相关阅读:
    几个 vim 的块操作命令
    图灵社区 : 阅读 : 谁说Vim不是IDE?(三)
    google.sg
    Vim 配置详解_wuyang
    Vim 配置详解_wuyang
    不忘本~结构
    刚刚做了一个菜单导航变亮的效果,共享一下吧!
    不忘本~静态构造函数
    数据结构~时间复杂度和空间复杂度
    数据结构~在页面上渲染树型结构
  • 原文地址:https://www.cnblogs.com/hao-tian/p/9516064.html
Copyright © 2011-2022 走看看