set和map的底层模板是红黑树,可以有不同的键值和实值,关于增删改查,迭代器的使用都在代码里面,亲手尝试更方便记忆

#include <iostream>
#include <map>
#include <algorithm>

//#include <functional>//不知道用来干什么

using namespace std;
void fun(pair<int,char>pr){
cout << pr.first << "-" << pr.second << " ";
}
int main()
{

map<int,char>imp;
imp.insert(pair<int,char>(3,'a'));
imp.insert(pair<int,char>(2,'b'));
imp.insert(pair<int,char>(1,'c'));
imp.insert(pair<int,char>(4,'f'));
imp.insert(pair<int,char>(5,'g'));
imp[10]='i';

//imp.insert(pair<int,char>(2,'a'));
map<int,char,less<int> >imp1(imp);
map<int,char>imp2(imp1.begin(),imp1.end());
map<int,char>::iterator ite=imp2.begin();
imp.clear();
ite++;
imp.insert(ite,imp2.end());
imp.swap(imp1);
//cout << imp.size() << " " << imp.count(2) << " " << imp.empty() << endl;
ite=imp.find(2);
ite=imp.upper_bound(1);//>,返回迭代器
ite=imp.lower_bound(3);//<=,返回迭代器
//cout << ite->second << endl;
//键值不可修改,实值可以修改

//删
//imp.erase(2);
map<int,char>::iterator ite1=imp.find(3);
map<int,char>::iterator ite2=imp.find(4);
//imp.erase(ite1,ite2);//ite1到ite2的前一个元素
//imp.erase(ite);
//imp.clear();

//cout << imp.max_size() << endl;

//改
//imp[3]='w';
//cout <<imp[3] << endl;

//查
//ite=imp.find(2);//若找不到,指向end()
//cout << imp[2] << " " << ite->second << endl;
//cout << imp[11] << endl;//找不到就不输出
//for_each(imp.begin(),imp.end(),fun);
cout << endl;

map<int,char,greater<int> >rimp(imp.begin(),imp.end());
for_each(rimp.begin(),rimp.end(),fun);
cout << endl;
pair<map<int,char>::iterator,map<int,char>::iterator> pre;
pre=imp.equal_range(2);
cout << pre.first->first << endl;
cout << pre.second->first << endl;
/*
//key_comp()
map <int, int, less<int> > m1;
map <int, int, less<int> >::key_compare kc1 = m1.key_comp( ) ;
bool result1 = kc1( 2, 3 ) ;
if( result1 == true )
{
cout << "kc1( 2,3 ) returns value of true, "
<< "where kc1 is the function object of m1."
<< endl;
}
else
{
cout << "kc1( 2,3 ) returns value of false "
<< "where kc1 is the function object of m1."
<< endl;
}

map <int, int, greater<int> > m2;
map <int, int, greater<int> >::key_compare kc2 = m2.key_comp( );
bool result2 = kc2( 2, 3 ) ;
if( result2 == true )
{
cout << "kc2( 2,3 ) returns value of true, "
<< "where kc2 is the function object of m2."
<< endl;
}
else
{
cout << "kc2( 2,3 ) returns value of false, "
<< "where kc2 is the function object of m2."
<< endl;
}
*/

/*
equal_range()//在algorithm头文件,map头文件
typedef map <int, int, less<int> > IntMap;
IntMap m1;
map <int, int> :: const_iterator m1_RcIter;
typedef pair <int, int> Int_Pair;

m1.insert ( Int_Pair ( 1, 10 ) );
m1.insert ( Int_Pair ( 2, 20 ) );
m1.insert ( Int_Pair ( 3, 30 ) );

pair <IntMap::const_iterator, IntMap::const_iterator> p1, p2;
p1 = m1.equal_range( 2 );

cout << "The lower bound of the element with "
<< "a key of 2 in the map m1 is: "
<< p1.first -> second << "." << endl;

cout << "The upper bound of the element with "
<< "a key of 2 in the map m1 is: "
<< p1.second -> second << "." << endl;

// Compare the upper_bound called directly
m1_RcIter = m1.upper_bound( 2 );

cout << "A direct call of upper_bound( 2 ) gives "
<< m1_RcIter -> second << "," << endl
<< "matching the 2nd element of the pair"
<< " returned by equal_range( 2 )." << endl;

p2 = m1.equal_range( 4 );

// If no match is found for the key,
// both elements of the pair return end( )
if ( ( p2.first == m1.end( ) ) && ( p2.second == m1.end( ) ) )
cout << "The map m1 doesn't have an element "
<< "with a key less than 40." << endl;
else
cout << "The element of map m1 with a key >= 40 is: "
<< p2.first -> first << "." << endl;
}
*/
return 0;
}

stl_map复习的更多相关文章

  1. iOS总结_UI层自我复习总结

    UI层复习笔记 在main文件中,UIApplicationMain函数一共做了三件事 根据第三个参数创建了一个应用程序对象 默认写nil,即创建的是UIApplication类型的对象,此对象看成是 ...

  2. vuex复习方案

    这次复习vuex,发现官方vuex2.0的文档写得太简略了,有些看不懂了.然后看了看1.0的文档,感觉很不错.那以后需要复习的话,还是先看1.0的文档吧.

  3. 我的操作系统复习——I/O控制和系统调用

    上篇博客介绍了存储器管理的相关知识——我的操作系统复习——存储器管理,本篇讲设备管理中的I/O控制方式和操作系统中的系统调用. 一.I/O控制方式 I/O就是输入输出,I/O设备指的是输入输出设备和存 ...

  4. 复习(1)【Maven】

    终于开始复习旧知识了,有输入必然要有输出.输入和输出之间的内化过程尤为重要,在复习的同时,真正把学到的东西积淀下来,加深理解. Maven项目概念与配置 Maven是一个项目管理和综合工具.Maven ...

  5. 《CSS权威指南》基础复习+查漏补缺

    前几天被朋友问到几个CSS问题,讲道理么,接触CSS是从大一开始的,也算有3年半了,总是觉得自己对css算是熟悉的了.然而还是被几个问题弄的"一脸懵逼"... 然后又是刚入职新公司 ...

  6. JS复习--更新结束

    js复习-01---03 一 JS简介 1,文档对象模型 2,浏览器对象模型 二 在HTML中使用JS 1,在html中使用<script></script>标签 2,引入外部 ...

  7. jQuery 复习

    jQuery 复习 基础知识 1, window.onload $(function(){});   $(document).ready(function(){}); 只执行函数体重的最后一个方法,事 ...

  8. jQuery5~7章笔记 和 1~3章的复习笔记

    JQery-05 对表单和表格的操作及其的应用 JQery-06 jQuery和ajax的应用 JQery-07 jQuery插件的使用和写法 JQery-01-03 复习 之前手写的笔记.实在懒得再 ...

  9. HTML和CSS的复习总结

    HTML(Hypertext Markup Language)超文本标记语言:其核心就是各种标记!<html> HTML页面中的所有内容,都在该标签之内:它主要含<head>和 ...

随机推荐

  1. Oracle忘记密码怎么办?

    1.打开cmd,输入sqlplus /nolog,回车:输入“conn / as sysdba”;输入“alter user sys identified by 新密码”,注意:新密码最好以字母开头, ...

  2. Socket通信实现步骤

    public class Server { public static void main(String[] args) { try { ServerSocket serverSocket = new ...

  3. 模拟服务容器Ioc

    服务容器是一个用于管理类依赖和执行依赖注入的强大工具. 一个类要被容器所能够提取,必须要先注册至这个容器.既然称这个容器叫做服务容器,那么我们需要某个服务,就得先注册.绑定这个服务到容器,那么提供服务 ...

  4. Qt编译Curl

    1.下载Curl,下载地址:https://curl.haxx.se/download.html,windows下载.zip压缩包,解压到E盘. 2.在”开始菜单“—>”所有程序“->”Q ...

  5. springboot,vue,shiro整合 关于登录认证功能

    首先是session问题 传统session认证 http协议是一种无状态协议,即浏览器发送请求到服务器,服务器是不知道这个请求是哪个用户发来的.为了让服务器知道请求是哪个用户发来的,需要让用户提供用 ...

  6. ImageMagick 将PDF转图片命令

    将 pdf 转一张图片 PS C:\Users\Microestc\desktop> magick convert -density -quality .pdf -append .jpeg ro ...

  7. leetcode刷题-- 3.二分查找

    二分查找 正常实现 题解 public int binarySearch(int[] nums, int key) { int l = 0, h = nums.length - 1; while (l ...

  8. CentOS6.9安装MySQL(编译安装、二进制安装)

    目录 CentOS6.9安装MySQL Linux安装MySQL的4种方式: 1. 二进制方式 特点:不需要安装,解压即可使用,不能定制功能 2. 编译安装 特点:可定制,安装慢 5.5之前: ./c ...

  9. mysql判断数据库或表是否存在

    (1) 判断数据库存在, 则删除:       drop database if exists db_name;(2) 判断数据表存在, 则删除: drop table if exists table ...

  10. A. You Are Given Two Binary Strings…

    A. You Are Given Two Binary Strings… You are given two binary strings x and y, which are binary repr ...