STL 成员函数

来源:互联网 发布:sql许可模式 编辑:程序博客网 时间:2024/06/01 19:20
stack:先进后出 FILO
{
   构造析构:
   stack <Elem> c     创建一个空的stack
   stack <Elem> c2(c)    复制stack
   数据增减:
   c.top()       返回栈顶数据
   c.push(x)     在栈顶增加elem数据
   c.pop()       弹出栈顶数据
   其他操作:
   c.empty()     判断栈是否为空
   c.size()      返回栈中数据的个数
}






queue:先进先出 FIFO
{
   构造析构:
   queue<Elem> c     创建一个空的queue  
   queue<Elem> c1(c) 复制queue
   数据访问与增减:
   c.front()         返回队列头部数据
   c.back()         返回队列尾部数据
   c.push(elem)    在队尾部增加elem数据
   c.pop()          弹出头部数据
   其他操作:
   c.empty()        判断队列是否为空
   c.size()        返回队列中数据的个数
}






vector:动态数组  vector<int> a;(等于声明了一个数组a[],大小没有指定,可以动态的向里面添加删除。
{
    c.clear()         移除容器中所有数据。
    c.empty()         判断容器是否为空。
    c.erase(pos)        删除pos位置的数据
    c.erase(beg,end) 删除[beg,end)区间的数据
    c.front()         传回第一个数据。
    c.insert(pos,elem)  在pos位置插入一个elem拷贝
    c.pop_back()     删除最后一个数据。
    c.push_back(elem) 在尾部加入一个数据
    c.resize(num)     重新设置该容器的大小
    c.size()         回容器中实际数据的个数。
    c.begin()         返回指向容器第一个元素的迭代器
    c.end()          返回指向容器最后一个元素的迭代器
}






set: 一个集合,默认为升序排列
set的各成员函数列表如下:
begin()--返回指向第一个元素的迭代器
clear()--清除所有元素
count()--返回某个值元素的个数
empty()--如果集合为空,返回true
end()--返回指向最后一个元素的迭代器
equal_range()--返回集合中与给定值相等的上下限的两个迭代器
erase()--删除集合中的元素
find()--返回一个指向被查找到元素的迭代器
get_allocator()--返回集合的分配器
insert()--在集合中插入元素
lower_bound()--返回指向大于(或等于)某值的第一个元素的迭代器
key_comp()--返回一个用于元素间值比较的函数
max_size()--返回集合能容纳的元素的最大限值
rbegin()--返回指向集合中最后一个元素的反向迭代器
rend()--返回指向集合中第一个元素的反向迭代器
size()--集合中元素的数目
swap()--交换两个集合变量
upper_bound()--返回大于某个值元素的迭代器
value_comp()--返回一个用于比较元素间的值的函数








map:
map的功能
自动建立Key - value的对应。key 和 value可以是任意你需要的类型。
根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。
快速插入Key - Value 记录。
快速删除记录
根据Key 修改value记录。
遍历所有记录。
成员变量和成员函数
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不是一个容器中的元素交换,而是两个容器交换;
  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;
}
6.map的sort问题:
  Map中的元素是自动按key升序排序,所以不能对map用sort函数:
7,   map的基本操作函数:
      C++ Maps是一种关联式容器,包含“关键字/值”对
      begin()         返回指向map头部的迭代器
      clear()        删除所有元素
      count()         返回指定元素出现的次数
      empty()        如果map为空则返回true
      end()           返回指向map末尾的迭代器
      erase()         删除一个元素
      find()           查找一个元素
      insert()        插入元素
      key_comp() 返回比较元素key的函数
      lower_bound()    返回键值>=给定元素的第一个位置
      max_size()    返回可以容纳的最大元素个数
      rbegin()        返回一个指向map尾部的逆向迭代器
      rend()           返回一个指向map头部的逆向迭代器
      size()            返回map中元素的个数
      swap()           交换两个map
      upper_bound()     返回键值>给定元素的第一个位置
      value_comp()       返回比较元素value的函数






priority_queue:优先队列
empty() 如果优先队列为空,则返回真
pop() 删除第一个元素
push() 加入一个元素
size() 返回优先队列中拥有的元素的个数
top() 返回优先队列中有最高优先级的元素

原创粉丝点击