STL整理

Posted 神犇(shenben)

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STL整理相关的知识,希望对你有一定的参考价值。

pair

#include<cstdio>
#include<utility>
#include<map>
#include<iostream>
using namespace std;
/*struct pair{
    int first;
    int second;
}x;*///<==>
pair<int,int>x(1,2);
typedef pair<string, string> author;
author pro("May", "Lily");
author joye;
int main(){
    printf("%d\n%d\n",x.first,x.second); 
    
    cout<<pro.first<<endl<<joye.first<<endl;
    
    int a = 8;
    string m = "James";
    pair<int, string> newone;
    newone = make_pair(a, m);//make_pair(),返回一个pair类型
    cout<<newone.first<<endl<<newone.second<<endl;
    
    if (pro.first == "May" && pro.second == "Lily")
    cout<<"yes"<<endl;

    return 0;
}

 

map

 

1. map最基本的构造函数;
   map<string , int >mapstring;         map<int ,string >mapint;
   map<sring, char>mapstring;         map< char ,string>mapchar;
   map<char ,int>mapchar;            map<int ,char >mapint;

2. map添加数据;

   map<int ,string> maplive;  
   1.maplive.insert(pair<int,string>(102,"aclive"));
   2.maplive.insert(map<int,string>::value_type(321,"hai"));
   3, maplive[112]="April";//map中最简单最常用的插入添加!
3,map中元素的查找:

   find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。        

   map<int ,string >::iterator l_it;; 
   l_it=maplive.find(112);
   if(l_it==maplive.end())
                cout<<"we do not find 112"<<endl;
   else cout<<"wo find 112"<<endl;
4,map中元素的删除:
   如果删除112;
   map<int ,string >::iterator l_it;;
   l_it=maplive.find(112);
   if(l_it==maplive.end())
        cout<<"we do not find 112"<<endl;
   else  maplive.erase(l_it);  //delete 112;
5,map中 swap的用法:
  Map中的swap不是一个容器中的元素交换,而是两个容器交换;

#include<map>
#include<iostream>
using namespace std;
/*
  map<int,int> 下表可正可负,不会爆内存(二叉树储存原理) 
*/
int main(){
      map <int, int> m1, m2, m3;
      map <int, int>::iterator m1_Iter;

      m1.insert ( pair <int, int>  ( 1, 10 ) );
      m1.insert ( pair <int, int>  ( 2, 20 ) );
      m1.insert ( pair <int, int>  ( 3, 30 ) );
      m2.insert ( pair <int, int>  ( 10, 100 ) );
      m2.insert ( pair <int, int>  ( 20, 200 ) );
      m3.insert ( pair <int, int>  ( 30, 300 ) );

   cout<<"The original map m1 is:";
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout<<" "<<m1_Iter->second;
      cout<<"."<<endl;

   // This is the member function version of swap
   //m2 is said to be the argument map; m1 the target map
   m1.swap( m2 );

   cout<<"After swapping with m2, map m1 is:";
   for( m1_Iter = m1.begin( ); m1_Iter != m1.end( );m1_Iter++ )
      cout<<" "<<m1_Iter -> second;
      cout<<"."<<endl;
   cout<<"After swapping with m2, map m2 is:";
   for ( m1_Iter = m2.begin( ); m1_Iter != m2.end( ); m1_Iter++ )
      cout<<" "<<m1_Iter -> second;
      cout<<"."<<endl;
   // This is the specialized template version of swap
   swap( m1, m3 );

   cout<<"After swapping with m3, map m1 is:";
   for( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout<<" "<<m1_Iter -> second;
      cout<<"."<<endl;
    return 0;
}

 

6.map的sort问题:
  Map中的元素是自动按key升序排序,所以不能对map用sort函数:
  For example:

#include<map>
#include<cstdio>
#include<iostream>
using namespace std;
int main(){
    map<int,int>test;
    map<int,int>::iterator test_Iter;
    test.insert(pair<int,int>(1,21)); 
    test.insert(pair<int,int>(1,19));
    test.insert(pair<int,int>(2,21));
    test.insert(pair<int,int>(9,51));
    test.insert(pair<int,int>(5,51));
    test.insert(pair<int,int>(4,81));
    test.insert(pair<int,int>(3,21));
    for(test_Iter=test.begin();test_Iter!=test.end();test_Iter++)
       printf("%d %d\n",test_Iter->first,test_Iter->second);
    //自动按关键字排序 
    return 0;
}

 

7,   map的基本操作函数:
      C++ Maps是一种关联式容器,包含“关键字/值”对
      begin()          返回指向map头部的迭代器
      clear()         删除所有元素
      count()          返回指定元素出现的次数
      empty()          如果map为空则返回true
      end()            返回指向map末尾的迭代器
      equal_range()    返回特殊条目的迭代器对
      erase()          删除一个元素
      find()           查找一个元素
      get_allocator()  返回map的配置器
      insert()         插入元素
      key_comp()       返回比较元素key的函数
      lower_bound()    返回键值>=给定元素的第一个位置
      max_size()       返回可以容纳的最大元素个数
      rbegin()         返回一个指向map尾部的逆向迭代器
      rend()           返回一个指向map头部的逆向迭代器
      size()           返回map中元素的个数
      swap()            交换两个map
      upper_bound()     返回键值>给定元素的第一个位置
      value_comp()      返回比较元素value的函数

 

set

set介绍

C++ 集合(set)是一种包含已排序对象的关联容器,能够自动对进入集合的元素排序,并且不允许重复(另一类集合(multiset)提供对重复元素的支持)。集合中的元素可以是常见类型,也可以是自定义的接口提类型。set容器有2个主要特征:

(1)不能直接改变元素值,因为那样会打乱原本正确的顺序,要改变元素值必须先删除旧元素,再插入新元素。

(2)不提供直接存取元素的任何操作函数,只能通过迭代器(iterator)进行间接存取,可以将迭代器类比成C里的指针。

C++标准模板库提供通用的set模板,需要添加头文件#include<set>

 

常用操作

1.插入:insert()

2.中序遍历:

 

set<int> s;

......

set<int>::iterator it; //定义迭代器 
    //中序遍历集合中的所有元素  
    for(it=s.begin();it!=s.end();it++) 
		......


3.删除:

 

set<int> s;

s.erase(2);        //删除键值为2的元素
s.clear();           //清除所有元素

4.元素检索:

#include<set>
#include<iostream>
#include<cstdio>
using namespace std;
set<int>s;
int main(){
    set<int>::iterator it;
    s.insert(2);
    it=s.find(2);
    printf("%s",it!=s.end()?"yes":"no");//yes 找到 
}

 

 

一个不包含重复元素的 collection。更确切地讲,set 不包含满足 e1.equals(e2) 的元素对 e1 和 e2,并且最多包含一个 null 元素。正如其名称所暗示的,此接口模仿了数学上的 set 抽象。
  在所有构造方法以及 add、equals 和 hashCode 方法的协定上,Set 接口还加入了其他规定,这些规定超出了从 Collection 接口所继承的内容。出于方便考虑,它还包括了其他继承方法的声明(这些声明的规范已经专门针对 Set 接口进行了修改,但是没有包含任何其他的规定)。对这些构造方法的其他规定是(不要奇怪),所有构造方法必须创建一个不包含重复元素的 set(正如上面所定义的)。注:如果将可变对象用作 set 元素,那么必须极其小心。如果对象是 set 中某个元素,以一种影响 equals 比较的方式改变对象的值,那么 set 的行为就是不确定的。此项禁止的一个特殊情况是不允许某个 set 包含其自身作为元素。
常用的几个方法
  boolean add(E e)
  如果 set 中尚未存在指定的元素,则添加此元素(可选操作)。
  boolean addAll(Collectionc)
  如果 set 中没有指定 collection 中的所有元素,则将其添加到此 set 中(可选操作)。
  void clear()
  移除此 set 中的所有元素(可选操作)。
  boolean contains(Object o)
  如果 set 包含指定的元素,则返回 true。
  boolean containsAll(Collectionc)
  如果此 set 包含指定 collection 的所有元素,则返回 true。
  boolean equals(Object o)
  比较指定对象与此 set 的相等性。
  int hashCode()
  返回 set 的哈希码值。
  boolean isEmpty()
  如果 set 不包含元素,则返回 true。
  Iterator iterator()
  返回在此 set 中的元素上进行迭代的迭代器。
  boolean remove(Object o)
  如果 set 中存在指定的元素,则将其移除(可选操作)。
  boolean removeAll(Collectionc)
  移除 set 中那些包含在指定 collection 中的元素(可选操作)。
  boolean retainAll(Collectionc)
  仅保留 set 中那些包含在指定 collection 中的元素(可选操作)。
  int size()
  返回 set 中的元素数(其容量)。
  Object[] toArray()
  返回一个包含 set 中所有元素的数组。
  T[]
  toArray(T[] a)

  返回一个包含此 set 中所有元素的数组;返回数组的运行时类型是指定数组的类型。

以上是关于STL整理的主要内容,如果未能解决你的问题,请参考以下文章

STL——pair

STL pair

[STL] pair

STL之pair类型

STL中pair容器的用法

STL之pair类型具体分析