std map 热血格斗场

map,  用实力值当做关键字

#include <iostream>
#include <memory.h>
#include <map>
#include <stdio.h>
#define IN (1<<28)
using namespace std;
int N;
map <int,int> record;
int main()
{
    cin >> N;
    record[1000000000] = 1;
    for( int i = 1; i <= N; i++ )
    {
        int id,value;
        scanf("%d %d", &id, &value);
        record[value] = id;
        map<int,int>::iterator p = record.find(value);
        map<int,int>::iterator j;
        map<int,int>::iterator k;
        if( p != record.end() )
        {
             if( p == record.begin() )
             {
                 j = p;
                 j++;
                 cout << p->second << ' ' << j->second << endl;
             }
             else
             {
                 j = p;
                 j++;
                 k = p;
                 k--;
                 int up = j->first - p->first;
                 int low = p->first - k->first;
                 if( low <= up )
                 {
                     cout << p->second << ' ' << k->second << endl;
                 }
                 else
                 {
                     cout << p->second << ' ' << j->second << endl;
                 }
             }
        }
        else
            break;
    }
    return 0;
}

### C++ 中 `std::map` 的使用方法和实例 #### 基本概念 `std::map` 是 C++ 标准模板库(STL)中的一个关联容器,用于存储键值对(key-value pairs)。其中的元素按照键值自动排序,默认情况下按键值升序排列。每个键在 `std::map` 中是唯一的。 以下是关于 `std::map` 的一些重要特性[^3]: - 键值唯一:不允许重复的键。 - 自动排序:所有元素会根据其键值进行排序。 - 高效查找:通过二叉树实现,提供了基于键的快速检索功能。 --- #### 创建和初始化 `std::map` 可以通过多种方式创建并初始化 `std::map`: ```cpp #include <iostream> #include <map> int main() { // 方法一:直接声明空 map std::map<int, std::string> myMap; // 方法二:使用 initializer list 初始化 std::map<std::string, int> anotherMap = {{"apple", 1}, {"banana", 2}, {"cherry", 3}}; // 输出另一个 map 的内容 for (const auto& pair : anotherMap) { std::cout << pair.first << ": " << pair.second << '\n'; } return 0; } ``` 上述代码展示了如何定义一个空的 `std::map` 和使用初始列表来填充它的例子[^3]。 --- #### 插入元素到 `std::map` 可以使用以下几种方法向 `std::map` 添加新元素: ```cpp #include <iostream> #include <map> int main() { std::map<std::string, int> fruitCount; // 方法一:使用下标操作符 [] fruitCount["apple"] = 10; // 方法二:调用 insert 函数 fruitCount.insert(std::make_pair("orange", 20)); // 方法三:使用 emplace fruitCount.emplace("grape", 15); for (const auto& pair : fruitCount) { std::cout << pair.first << ": " << pair.second << '\n'; } return 0; } ``` 这里分别演示了三种不同的插入技术及其效果[^3]。 --- #### 访问和修改 `std::map` 中的数据 访问 `std::map` 中已有的条目非常简单,可以直接利用键名或者迭代器完成这一过程: ```cpp #include <iostream> #include <map> int main() { std::map<std::string, double> prices = {{"coffee", 2.99}, {"tea", 1.99}}; // 使用 [] 获取或设置值 std::cout << "Price of coffee: " << prices["coffee"] << '\n'; // 如果不存在,则会新增一条记录,并赋予默认值(这里是double类型的0) prices["water"]; std::cout << "Price of water after default insertion: " << prices["water"] << '\n'; // 修改现有项的价格 prices["tea"] += 0.50; std::cout << "Updated price of tea: " << prices["tea"] << '\n'; return 0; } ``` 此片段说明了如何读取以及更新特定项目的数值。 --- #### 删除 `std::map` 中的内容 删除指定键对应的项目可借助 erase 成员函数达成: ```cpp #include <iostream> #include <map> int main(){ std::map<char,int> charCounts{{'a',1},{'b',2}}; // 移除某个具体 key 及其对应 value charCounts.erase('a'); if(charCounts.empty()){ std::cout<<"The map is now empty.\n"; } else{ for(auto const &pair:charCounts){ std::cout<<pair.first<<":"<<pair.second<<"\n"; } } return 0;} ``` 这段程序解释了移除单个元素的过程[^3]。 --- #### 迭代遍历整个 `std::map` 为了处理所有的键值对,通常采用循环结构逐一访问它们: ```cpp #include <iostream> #include <map> int main(){ std::map<std::string,std::string>mymap={{"John","Doe"},{"Jane","Smith"}}; for(auto it=mymap.cbegin();it!=mymap.cend();++it){ std::cout<<(*it).first<<" "<<(*it).second<<"\n"; } return 0;} ``` 该示例呈现了一种典型的遍历模式[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值