C++在线运行

版本:

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

                        
以下是用户最新保存的代码
单链表综合应用 发布于:2024-04-25 22:49 【链栈】例4.2 算术表达式求值 发布于:2024-04-25 15:23 【顺序栈】例4.2 算术表达式求值 发布于:2024-04-25 15:20 【链栈】例4.1 数制转换 发布于:2024-04-25 13:35 链栈基本操作 发布于:2024-04-25 13:28 【顺序栈】两个栈共享一个向量空间 发布于:2024-04-25 13:22 【顺序栈】思考:给定一个数据元素由栈底到栈顶按从小到大排列的有序栈S1。现要求伴随着一系列入栈和出栈操作,对一个任意数据元素x 进行入栈操作,使得x入栈后S1仍然为有序栈。操作过程可借助额外的栈S2完成。 发布于:2024-04-25 13:16 顺序栈操作 发布于:2024-04-25 13:12 拷贝构造函数 发布于:2024-04-15 11:21 解析析构函数 发布于:2024-04-15 11:13 C++int与double除法 发布于:2024-04-12 16:46 移动语意、移动构造、移动赋值示例 发布于:2024-04-12 12:25 二分查找左右边界 发布于:2024-04-11 15:17 #include<iostream> using namespace std; class Base { public: virtual void fn() { cout <<"In Base Class\n";} }; class SubClass : public Base { public: virtual void fn(){ cout <<"In Sub Class\n"; } }; int main() { Base bc; Base *p; SubClass sc; p=&bc; p->fn(); p=&sc; p->fn(); } 发布于:2024-04-11 10:31 汉诺塔算法模拟 发布于:2024-04-10 10:22 static使用 发布于:2024-04-09 17:53 cpp测试0403 发布于:2024-04-03 11:20 泛型代码测试 发布于:2024-03-29 10:06 实验二单向链表 发布于:2024-03-29 09:30 三只小猪谁最重 发布于:2024-03-23 21:06 如何理解虚函数 发布于:2024-03-20 15:22 shared_ptr 智能指针理解 发布于:2024-03-20 11:22 c++数据结构 发布于:2024-03-19 22:10 bfs走迷宫 发布于:2024-04-12 14:22 拷贝构造函数调用顺序 发布于: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 [更多]
显示目录

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