CSP202006-2 稀疏向量

map使用练习

#include<iostream>
#include<map>
using namespace std;
map<int, int>rec1;
map<int, int>rec2;
int main(void)
{
    int n, a, b;
    scanf("%d %d %d", &n, &a, &b);
    int index, value;
    for (int i = 0; i < a; i++)
    {
        scanf("%d %d", &index, &value);
        rec1[index] = value;
    }
    for (int i = 0; i < b; i++)
    {
        scanf("%d %d", &index, &value);
        rec2[index] = value;
    }

    long long sum = 0;
    for (auto i = rec1.begin(); i != rec1.end(); i++)
    {
        index = i->first;
        value = i->second;
        auto t = rec2.find(index);
        sum = sum + value * (t->second);
    }
    cout << sum << endl;
    return 0;
}

map的基本操作函数:

C++ maps是一种关联式容器,包含“关键字/值”对

begin() 返回指向map头部的迭代器

clear() 删除所有元素

count() 返回指定元素出现的次数, (帮助评论区理解: 因为key值不会重复,所以只能是1 or 0)

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的函数

auto

在C++11标准的语法中,auto被定义为自动推断变量的类型。
 
输入输出格式:
 
注意范围,sum用long long类型存储,另外最后输出sum时用printf通过60%,用cout则100%。
 
 
世界一直在变化,结果由我们来决定
 

参考:

https://blog.csdn.net/huzimu_/article/details/119792101

原文地址:https://www.cnblogs.com/qingdaodaozhu/p/15211664.html