STL容器----vector\array\map\set

template<class T,size_t N>

class My_Array

{

      T data[N];

};

int main()

{

      std::array<int,1000>  ia;

}

 //vector的emplace_back  =>在容器末尾就地构造元素

int main()

{

    std::vector<Int> iva;

   iva.reserve(10);

   Int a(10);

 iva.emplace_back(a);     //拷贝进入iva

   iva.emplace_back(Int(20)); //先构造无名对象(将亡值) 再拷贝进入iva

   iva.emplace_back(30);   //直接构造iva内的Int对象

}

//在push_back元素时为了保证效率一般是先开够空间,再加入元素

vector<Int> iva;

iva.reserve(1000000);  //先申请够空间

for(int i=0;i<1000000;++i)

{

    iva.push_back(i);

}  

 pair

template<class _Ty1,class _Ty2>

struct my_pair

{

      using first_type=_Ty1;

      using second_type=_Ty2;

      first_type first;

      second_type second;

public:

      my_pair(const _Ty1& _fs=_Ty1{},const _Ty2& _se=_Ty2{})

   :first(_fs),second(_se)  {}

  

};

int main()

{

    my_pair<int,int> iip(12,11);

}

比较pair的大小

template<class _Ty1,class _Ty2>

inline bool operator==(const pair<_Ty1,_Ty2>& _x,const pair<_Ty1,_Ty2>& _y)

{

    return _x.first==_y.first&& _x.second==_y.second;

}

template<class _Ty1,class _Ty2>

inline bool operator<(const pair<_Ty1,_Ty2>& _x,const pair<_Ty1,_Ty2>& _y)

{

    return  _x.first<_y.first||

  (!(_y.first>_x.first)&& _x.second<_y.second);

#include<map>

using namespace std;

int main()

{

     std::map<long long,void *> ismap;

     //                key             _Val

    //pair<KetType,ValType>   键值对

struct my_plus

{

    int operator()(int a,int b) const

{

     return a+b;

 }

}; 

int main()

{

     my_plus add;

     int x=add(12,23);

     // x=add.operator()(12,23) ;

}

int main()

  // _KeyT   _ValT

   std::map<int,std::string> ismap;

    ismap[12]="yhping";

    ismap[22]="hwj";

    ismap[18]="lmy"

    ismap[12]="mxy"   //会覆盖 不会重复

    

int age;

while(cin>>age,age!=-1)

{

    try

       {

            string str=ismap.at(age);  //string str=ismap[age];   使用at在age超出范围时会抛出异常

              cout<<str<<endl;

         }

       catch(std::out_of_range& e)

      {

          cout<<e.what() <<endl;

}

}

union RandIP

{

struct {

     unsigned short a;

     unsigned short b;

};

unsigned int ip;

   

}; 

int main()

{

   RandIP x;

   unsigned int ip;

  std::map<unsigned int,int>iimap;

  for(int i=0;i<100000;++i)

{

    x.a=rand();

    x.b=rand();

    ip=x.ip;

    iimap[ip]+=1;

}

for(auto& x: iimap)   //pair<unsigned int,int>; first second

  {

       cout<<x.first<<"=>"x.second<<endl;

}

return 0;

-------insert插入

int main()

{

    std::map<std::string,int> simap;

       //   _name      age

     //std::pair<std::string,int>

    simap["liminyang"]=20;

     simap["houwenjing"]=19;

    simap.insert(std::map<std::string,int>::value_type("yangheping",69));

     simap.insert(std::pair<std::string,int>("lixiaodan",12)); 

  //输出的结果是红黑树中序遍历后的结果

   return 0;

std::map<int,int> iimap;

int ar[]={12,23,34,45,56,67};

for(int i=0;i<sizeof(ar)/sizeof(ar[0]);++i)

{

   iimap[ar[i]]=i;

}

auto it=iimap.equal_range(34);   //双迭代器

//std::pair<std::map<int,int>::iterator,std::map<int,int>::iterator> it =iimap.equal_range(34);

//map容器的equal_range()返回不小于键值34和大于34的第一个值的迭代器

cout<<(*it.first).first<<endl//key 34

cout<<(*it.first).second<<endl//index 2

红黑树-----二叉平衡树

规则:1.节点分红黑  2.根为黑 3.NIL为黑 4.不能有相连的红节点 5.根到所有NIL的黑高度相同

推论:1.红节点的父和双子必然为黑,黑节点的则不一定 2.符合规则5,当且仅当任意子树符合规                则5

插入:新元素是:红色叶子节点   可能会违反规则4,但不会影响其他规则 

插入算法:修复规则4的违规情况(双红)

1.被插入的节点是根节点。 直接把此节点涂为黑色。

2.被插入的节点的父节点是红色。

(1)当前结点的祖父结点的另一个结点(叔叔节点)也是红色。

[1]将"父节点"设为黑色。

[2]将"叔叔节点"设为黑色。

[3]将"祖父节点"设为"红色"。

[4]将"祖父节点"设为"当前节点(红色节点)",即之后继续对"当前节点"进行操作。

(2)叔叔节点是黑色,且当前节点是其父节点的右孩子。

[1]将"父节点"作为"新的当前节点"。

[2]以新的当前节点为支点进行左旋。

(3)叔叔节点是黑色,且当前节点是其父节点的左孩子

[1]将父节点设为黑色。

[2]将祖父节点设为红色。

[3]以祖父节点为支点进行右旋。

删除操作:

auto it=iimap.equal_range(50);

//范围以两个迭代器定义

//一个指向首个不小于key的元素,另一个指向首个大于key的元素

//首个迭代器可以换用lower_bound()获得

//而第二个迭代器可换用upper_bound()获得

auto p=iimap.lower_bound(50);   // key 56

//返回指向首个不小于给定键的元素的迭代器

auto x=iimap.upper_bound(50);   // key 56

//返回首个大于给定键的元素的迭代器

//多重map在关键码相同的时候,相同键值在右边插入

set----只有关键码 是一个集合

using namespace std;

int main()

{

    int ar[]={12,23,34,45,56,67,78,89,90,100};

    std::set<int> iset;

   for(int i=0;i<sizeof(ar)/sizeof(ar[0]);++i)

   {

     iset.insert(ar[i]);

    }

for(auto& x:iset)   //set中没有重复的关键码,如果插入时是重复的就舍弃掉

{

   cout<<x<<endl;

}

 int val;

 while(cin>>val,val!= )

{

    cout<<iset.count(val)<<endl;  //如果有输出1,如果没有输出0

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值