C++在线运行

版本:

所属目录
点击了解高性能代码运行API
运行结果
教程手册
代码仓库
极速运行
终端运行
图形+终端

                        
以下是用户最新保存的代码
三只小猪谁最重 发布于:2024-03-23 21:06 code2 c++ 发布于:2024-03-20 18:21 如何理解虚函数 发布于:2024-03-20 15:22 shared_ptr 智能指针理解 发布于:2024-03-20 11:22 c++数据结构 发布于:2024-03-19 22:10 bfs走迷宫 发布于:2024-03-19 20:41 拷贝构造函数调用顺序 发布于:2024-03-19 16:23 构造函数初始化列表 发布于:2024-03-19 16:03 最富裕小家庭真题代码 发布于:2024-03-18 02:52 这是一个分析数据大小的程序 发布于:2024-03-16 16:27 虚函数、运行时的多态 发布于:2024-03-16 11:00 c++ class试水 发布于:2024-03-15 16:04 常见排序算法 发布于:2024-03-13 14:44 顶顶顶大苏打 发布于:2024-03-09 21:46 自学测试使用 发布于:2024-03-08 10:03 E. 【lqc++152308ST】面积最大差值 发布于:2024-03-07 22:16 生成Delaunay图算法 发布于:2024-03-07 14:59 C语言学习资料 发布于:2024-03-06 23:31 输入一个值 进行计算 发布于:2024-03-08 11:13 sizeof的代码 发布于:2024-03-04 13:52 缩进控制流语句体判断数字的位置 发布于:2024-03-01 17:31 选择排序之随机数组 发布于:2024-02-29 22:28 变分重构有限体积法生成动网格 发布于:2024-02-29 14:37 四面体动网格壳变换 发布于:2024-02-29 14:29 排序算法 - 插入排序 - 希尔排序 发布于:2024-02-29 13:51 动网格生成代码 发布于:2024-02-29 11:16 枚举类型示例 发布于:2024-02-28 18:55 圆的周长面积计算 发布于:2024-02-28 16:59 三个数字挑选出最大的一个 发布于:2024-02-26 14:45 很好用的在线编程网站 发布于:2024-02-23 20:43 C++图的存储——链表 发布于:2024-02-22 17:47 --- c++学习复习 发布于:2024-03-01 17:32 C++ 之 字符判断——快捷函数 发布于:2024-02-21 17:37 NMS例程 发布于:2024-02-21 14:43 力扣小测试 发布于:2024-02-22 17:05 鸡兔同笼免做代码 发布于:2024-02-19 14:13 函数 曼哈顿距离 发布于:2024-02-17 17:16 闰年统计题目 发布于:2024-02-15 18:58 浮点数的四舍五入问题 发布于:2024-02-11 20:44 容器案例--员工分组 发布于:2024-02-07 23:00 吹泡泡的陈胜之路 发布于:2024-02-07 17:46 redblack tree exam 发布于:2024-02-18 11:49 失效迭代器(Invalidating Iterators) 发布于:2024-02-04 11:42 搜索不到你啊 发布于:2024-02-02 22:23 十滴水算法(别人的) 发布于:2024-02-01 17:16 测试宏展开 发布于:2024-02-01 11:30 - 安徽大学龙河校区安徽大学 发布于:2024-01-31 11:39 C++i++与++i的区别 发布于:2024-01-30 10:17 详见洛谷P1443 发布于:2024-01-27 18:29 EDE-rand% 发布于:2024-01-27 17:13 [更多]
显示目录

map用法



学习嵌入式的绝佳套件,esp8266开源小电视成品,比自己去买开发板+屏幕还要便宜,省去了焊接不当搞坏的风险。 蜂鸣版+触控升级仅36元,更强的硬件、价格全网最低。

点击购买 固件广场

map用法

map是C++中的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,本文为大家总结了map的一些基本简单的操作!

1、map最基本的构造函数;

mapmapstring; mapmapint;
mapmapstring; map< char ,string>mapchar;
mapmapchar; mapmapint;

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;
  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函数:

示例:

 #include <map>`` #include <iostream>
 using namespace std;
 int main( )
 {
   map <int, int> m1;
   map <int, int>::iterator m1_Iter;
   m1.insert ( pair <int, int>  ( 1, 20 ) );
   m1.insert ( pair <int, int>  ( 4, 40 ) );
   m1.insert ( pair <int, int>  ( 3, 60 ) );
   m1.insert ( pair <int, int>  ( 2, 50 ) );
   m1.insert ( pair <int, int>  ( 6, 40 ) );
   m1.insert ( pair <int, int>  ( 7, 30 ) );
   cout << "The original map m1 is:"<<endl;
   for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )
      cout <<  m1_Iter->first<<" "<<m1_Iter->second<<endl;
}

7、map的基本操作函数:

C++ Maps是一种关联式容器,包含“关键字/值”对
begin() 返回指向map头部的迭代器
clear() 删除所有元素
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的函数
由JSRUN为你提供的C++在线运行、在线编译工具
        JSRUN提供的C++ 在线运行,C++ 在线运行工具,基于linux操作系统环境提供线上编译和线上运行,具有运行快速,运行结果与常用开发、生产环境保持一致的特点。
yout