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