Maps-STL

来源:互联网 发布:软件测试周末班 编辑:程序博客网 时间:2024/06/05 11:48

Maps-STL

Maps are a part of the C++ STL.Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order.The mainly used member functions of maps are:

· Map Template:

· std::map <key_type, data_type>

· Declaration:

· map<string,int>m; //Creates a map m where key_type is of type string and data_type is of type int.

· Size:

· int length=m.size(); //Gives the size of the map.

· Insert:

· m.insert(make_pair("hello",9)); //Here the pair is inserted into the map where the key is "hello" and the value associated with it is 9.

· Erasing an element:

· m.erase(val); //Erases the pair from the map where the key_type is val.

· Finding an element:

· map<string,int>::iterator itr=m.find(val); //Gives the iterator to the element val if it is found otherwise returns m.end() .

· Ex: map<string,int>::iterator itr=m.find("Maps"); //If Maps is not present as the key value then itr==m.end().

· Accessing the value stored in the key:

· To get the value stored of the key "MAPS" we can do m["MAPS"] or we can get the iterator using the find function and then by itr->second we can access the value.

To know more about maps click Here.

You are appointed as the assistant to a teacher in a school and she is correcting the answer sheets of the students.Each student can have multiple answer sheets.So the teacher has queries:

:Add the marks to the student whose name is .

: Erase the marks of the students whose name is .

: Print the marks of the students whose name is . (If didn't get any marks print .)

Input Format

The first line of the input contains where is the number of queries. The next lines contain query each.The first integer, of each query is the type of the query.If query is of type , it consists of one string and an integer and where is the name of the student and is the marks of the student.If query is of type or ,it consists of a single string where is the name of the student.

Constraints

Output Format

For queries of type print the marks of the given student.

Sample Input

7

1 Jesse 20

1 Jess 12

1 Jess 18

3 Jess

3 Jesse

2 Jess

3 Jess

Sample Output

30

20

0

百度翻译:

地图是C++ STL的一部分。地图关联容器,存储的元素相结合的一个关键值和映射值组成,下面的一个特定的顺序,主要用于地图的成员函数:

 

地图模板:

 

std:map < key_typedata_type >

 

宣言:

 

<字符串>地图,int m//创建一个地图Mkey_type是字符串类型和data_typeint类型。

 

大小:

 

int length = M size()//给地图的大小。

 

Insert

 

m.insertmake_pair(“您好”,9));//这里对插入到地图的地方,关键是“你好”和与它相关联的值是9

 

擦除元素:

 

m.eraseVal);//删除对从地图上key_type是瓦尔。

 

寻找元素:

 

<字符串>int:迭代器ITR = m.findVal);//给迭代器的元素值如果发现否则返回M()。

 

例:地图<字符串,int >::迭代器ITR = m.find(“地图”);//如果地图是不存在的核心价值,然后ITR = = M()。

 

访问密钥中存储的值:

 

要存储的值的关键“我们可以地图”做“地图”][我们可以使用查找功能,然后通过ITR ->二我们可以访问的价值得到迭代器。

 

要了解更多地图,请点击这里。

 

你被任命为学校老师的助手,她正在批改学生的答卷,每个学生可以有多张答卷,所以老师有疑问:

 

把分数加在名字叫那个的学生身上。

 

擦掉学生姓名的痕迹。

 

打印学生姓名的标记。(如果没有得到任何标记打印。)

 

输入格式

 

输入的第一行包含查询的数量在哪里。下一行包含查询每个。第一个整数,每个查询的查询类型。如果查询类型,它由一个字符串和一个整数,是学生的名字和学生的成绩。如果查询是类型,它由一个字符串哪里是学生的名字。

 

约束

 

输出格式

 

对于类型的查询打印给定学生的标记。

 

样本输入

 

 

1杰西20

 

1杰西12

 

1杰西18

 

3杰西

 

3杰西

 

2杰西

 

3杰西

 

示例输出

 

三十

 

二十

 

 

#include <cmath>

#include <cstdio>

#include <vector>

#include <iostream>

#include <set>

#include <map>

#include <algorithm>

using namespace std;

int main()

{

int n,type;

    cin>>n;

    map<string,int>clas;

    string name;

    int i,mark;

for(i=0;i<n;i++)

{

cin>>type>>name;

if(type==1)

        {

cin>>mark;

clas[name]+=mark;

}

else if(type==2)

clas.erase(name);

else

cout<<clas[name]<<endl;

}

return 0;

}

原创粉丝点击