Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basic usage of map function in C language?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What is the basic usage of map function in C language? many novices are not very clear about this. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can get something.

Map

There are N students' name name and student number ID, asking for a student's name to find his ID.

The simple way to do this is to define string name [N] and int ID [N] to store information, then look for the student in name [], find it, and output his ID. But the disadvantage is that it needs to find all the name [N], the time complexity is O (N), and the efficiency is low.

The search can be realized quickly by using the map container in STL, and the complexity is O (log 2N).

Map is an association container that implements the mapping from key to value. The reason why map is efficient is that it uses balanced binary search trees for storage and access.

General arrays are also implemented by mapping.

Int a defines the mapping from int to int, for example, a [4] = 36 is to map 4 to 36, respectively.

Double b defines the mapping from int to int. For example, if b [5] = 0.009, it maps 5 to 0.009 respectively.

Specific operation of map

(1) definition: for example, map stands for mapping from a string to a number, and there are multiple mappings. (note that string-to-integer mapping must be string rather than char)

(2) assignment: for example, a ["TOM"] = 15. Here "TOM" is used as a normal array subscript.

(3) search: you can use a ["TOM"] directly to indicate that it is not necessary to search all values.

Note that map is a function of C++ and must use the C++ header file. Example POJ 2153

# include#include#include//map function header file using namespace std;map xramp / definition maps from string to int char s [10010] [50]; char b [50], c [50] = "Li Ming"; int main () {int m * n *

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report