STL编程总结--2022-3-25

STL

文档访问网站: https://docs.microsoft.com/zh-cn/cpp/standard-library/map-class?view=msvc-170

MFC设计文档网站 : https://docs.microsoft.com/zh-cn/cpp/mfc/mfc-desktop-applications?view=msvc-170
在这里插入图片描述总结:
在这里插入图片描述在这里插入图片描述

stack

取栈顶用top()
在这里插入图片描述

queue

取队首元素 是front()
取最后的元素是 back()

// queue_back.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>
int main( )
{
   using namespace std;
   queue <int> q1;

   q1.push( 10 );
   q1.push( 11 );
   
   int& i = q1.back( );
   const int& ii = q1.front( );

   cout << "The integer at the back of queue q1 is " << i
        << "." << endl;
   cout << "The integer at the front of queue q1 is " << ii
        << "." << endl;
}

priority_queue

头文件:#include

涉及到比较运算符的重载
对于自己定义的结构,使用时,需要重载比较运算符
在这里插入图片描述

// pqueue_empty.cpp
// compile with: /EHsc
#include <queue>
#include <iostream>

int main( )
{
   using namespace std;

   // Declares priority_queues with default deque base container
   priority_queue <int> q1, s2;

   q1.push( 1 );

   if ( q1.empty( ) )
      cout << "The priority_queue q1 is empty." << endl;
   else
      cout << "The priority_queue q1 is not empty." << endl;

   if ( s2.empty( ) )
      cout << "The priority_queue s2 is empty." << endl;
   else
      cout << "The priority_queue s2 is not empty." << endl;
}

vector

vector v(5)
v.at(4) =100; //在位置4放入100
v.end() //最后的位置
v.begin() //开始的位置

list 链表、双线性列表

在这里插入图片描述

deque容器类,双端队列

Set 容器 (有序的)可以从大到小排,也可以从小到大排

从小到大排序(10,20,30,40)
在这里插入图片描述从大到小排序(20,10)
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

pair模板

在这里插入图片描述
在这里插入图片描述

Map

用于存储和检索集合中的数据,此集合中的每个元素均为包含数据值和排序键的元素对。 键的值是唯一的,用于自动排序数据。

可以直接更改映射中的元素值。 键值为常量,无法更改。 必须先删除与旧元素关联的键值,才能为新元素插入新键值。
在这里插入图片描述

template <class Key,class Type,class Traits = less<Key>,class Allocator=allocator<pair <const Key, Type>>>
class map;

大小可变的关联容器,基于关联键值高效检索元素值。
可逆,因为它提供双向迭代器来访问其元素。
有序,因为它的元素根据指定的比较函数按键值排序。
唯一。 因为它的每个元素必须具有唯一键。
关联容器对,因为它的元素数据值与其键值不同。
类模板,因为它提供的功能是泛型的,独立于元素或键类型。 用于元素和键的数据类型作为类模板以及比较函数和分配器中的参数指定。

// map_at.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
typedef std::map<char, int> Mymap;
int main()
{
    Mymap c1;
    c1.insert(Mymap::value_type('a', 1));
    c1.insert(Mymap::value_type('b', 2));
    c1.insert(Mymap::value_type('c', 3));
// find and show elements
    std::cout << "c1.at('a') == " << c1.at('a') << std::endl;
    std::cout << "c1.at('b') == " << c1.at('b') << std::endl;
    std::cout << "c1.at('c') == " << c1.at('c') << std::endl;
    return (0);
 }
begin与cbegin、end() 与 cend()

返回一个迭代器,此迭代器用于发现 map 中的第一个元素。

const_iterator begin() const;

iterator begin();

例子:
clear()
清除映射的所有元素。

// map_begin.cpp
// compile with: /EHsc
#include <map>
#include <iostream>

int main( )
{
   using namespace std;
   map <int, int> m1;
   map <int, int> :: iterator m1_Iter;
   map <int, int> :: const_iterator m1_cIter;
   typedef pair <int, int> Int_Pair;
   m1.insert ( Int_Pair ( 0, 0 ) );
   m1.insert ( Int_Pair ( 1, 1 ) );
   m1.insert ( Int_Pair ( 2, 4 ) );
   m1_cIter = m1.begin ( );
   cout << "The first element of m1 is " << m1_cIter -> first << endl;
   m1_Iter = m1.begin ( );
   m1.erase ( m1_Iter );
   // The following 2 lines would err because the iterator is const
   // m1_cIter = m1.begin ( ); 
   // m1.erase ( m1_cIter );     常量不能修改的。
   m1_cIter = m1.begin( );
   cout << "The first element of m1 is now " << m1_cIter -> first << endl;
	 m1.clear();
   i = m1.size();
   cout << "The size of the map after clearing is "<< i << "." << endl;
}
迭代器的使用:类型 const_iterator 不能用于修改元素的值。

由const_iterator映射定义的 value_typepair<constKey, Type>指向作为 的对象的元素,这些元素的类型为 ,其第一个成员是元素的键,其第二个成员是元素所持有映射的基准。

   map <int, int> m1;
   map <int, int> :: iterator m1_Iter;
   map <int, int> :: const_iterator m1_cIter;
   m1_cIter = m1.begin ( );

以下是对const的区分

auto i1 = Container.begin();
// i1 is Container<T>::iterator
auto i2 = Container.cbegin();
// i2 is Container<T>::const_iterator
map与pair一起使用
map <int, int> m1;
   typedef pair <int, int> Int_Pair;
   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 2, 20 ) );
 const int &Ref1 = ( m1.begin( ) -> first );  //赋值给另一个值。这个是键
 int &Ref2 = ( m1.begin( ) -> second );       //这个是值

qsort()

在这里插入图片描述

作业:zoj 1204

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值