Day 8: Dictionaries and Maps

港控/mmm° 2022-03-20 05:21 175阅读 0赞

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3podWlxaXV6aHVveXVlNTgz_size_16_color_FFFFFF_t_70

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3podWlxaXV6aHVveXVlNTgz_size_16_color_FFFFFF_t_70 1

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3podWlxaXV6aHVveXVlNTgz_size_16_color_FFFFFF_t_70 2

C++,方法一:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string>
  4. #include<vector>
  5. #include<algorithm>
  6. #include<map>
  7. #include<math.h>
  8. #include<iostream>
  9. using namespace std;
  10. int main()
  11. {
  12. int n;
  13. string name;
  14. long num;
  15. cin >> n;
  16. cin.ignore();
  17. map <string, long> pBook;
  18. /*以array的方式,向map中插入*/
  19. for (int i = 0; i < n; i++)
  20. {
  21. cin >> name;
  22. cin >> num;
  23. pBook[name] = num;
  24. }
  25. /*对输入信息进行查找*/
  26. while (cin >> name)
  27. {
  28. /*使用find()函数直接查找*/
  29. if (pBook.find(name) != pBook.end())
  30. {
  31. cout << name << "=" << pBook.find(name)->second << endl;
  32. }
  33. else
  34. {
  35. cout << "Not found" << endl;
  36. }
  37. }
  38. system("pause");
  39. return 0;
  40. }

C++,方法二:

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string>
  4. #include<vector>
  5. #include<algorithm>
  6. #include<map>
  7. #include<math.h>
  8. #include<iostream>
  9. using namespace std;
  10. int main()
  11. {
  12. map<string, int> phoneBook;
  13. map<string, int>::iterator iter;
  14. int n;
  15. cin >> n;
  16. for (int i = 0; i < n; i++) {
  17. string name;
  18. int phoneNumber;
  19. cin >> name;
  20. cin >> phoneNumber;
  21. /* 以pair的方式,向map中插入 */
  22. phoneBook.insert(pair<string, int>(name, phoneNumber));
  23. }
  24. string objectName;
  25. while (cin >> objectName) {
  26. /* 使用迭代器进行查找 */
  27. iter = phoneBook.find(objectName);
  28. if (iter != phoneBook.end())
  29. cout << iter->first<< "=" << iter->second << endl;
  30. else
  31. cout << "Not found" << endl;
  32. }
  33. system("pause");
  34. return 0;
  35. }

python3:

  1. n = int(input())
  2. phoneBook = {}
  3. for num in range(0, n):
  4. #输入的是元组(sum,1111222)
  5. tup = tuple(input().split(' '))
  6. #将元组存入字典中 {sum:1111222}
  7. phoneBook[tup[0]] = tup[1]
  8. for num in range(0, n):
  9. name = input()
  10. if name in phoneBook:
  11. print("%s=%s" %(name,phoneBook[name]))
  12. else:
  13. print('Not found')

总结:

C++:

map数据结构:

1.插入元素:

  1. // 定义一个map对象
  2. map<int, string> mapStudent;
  3. // 第一种 用insert函數插入pair
  4. mapStudent.insert(pair<int, string>(000, "student_zero"));
  5. // 第二种 用insert函数插入value_type数据
  6. mapStudent.insert(map<int, string>::value_type(001, "student_one"));
  7. // 第三种 用"array"方式插入
  8. mapStudent[123] = "student_first";
  9. mapStudent[456] = "student_second";

2.查找元素:

  1. //使用迭代器
  2. map<int,string>::iterator iter;
  3. // find 返回迭代器指向当前查找元素的位置否则返回map::end()位置
  4. iter = mapStudent.find("123");
  5. if(iter != mapStudent.end())
  6. cout<<"Find, the value is"<<iter->second<<endl;
  7. else
  8. cout<<"Do not Find"<<endl;

3.刪除与清空元素

  1. //迭代器刪除
  2. iter = mapStudent.find("123");
  3. mapStudent.erase(iter);
  4. //用关键字刪除
  5. int n = mapStudent.erase("123"); //如果刪除了會返回1,否則返回0
  6. //用迭代器范围刪除 : 把整个map清空
  7. mapStudent.erase(mapStudent.begin(), mapStudent.end());
  8. //等同于mapStudent.clear()

4.map的大小:

  1. int nSize = mapStudent.size();

5.map的基本操作函数

  1. C++ maps是一种关联式容器,包含“关键字/值”对
  2. begin() 返回指向map头部的迭代器
  3. clear() 删除所有元素
  4. count() 返回指定元素出现的次数
  5. empty() 如果map为空则返回true
  6. end() 返回指向map末尾的迭代器
  7. equal_range() 返回特殊条目的迭代器对
  8. erase() 删除一个元素
  9. find() 查找一个元素
  10. get_allocator() 返回map的配置器
  11. insert() 插入元素
  12. key_comp() 返回比较元素key的函数
  13. lower_bound() 返回键值>=给定元素的第一个位置
  14. max_size() 返回可以容纳的最大元素个数
  15. rbegin() 返回一个指向map尾部的逆向迭代器
  16. rend() 返回一个指向map头部的逆向迭代器
  17. size() 返回map中元素的个数
  18. swap() 交换两个map
  19. upper_bound() 返回键值>给定元素的第一个位置
  20. value_comp() 返回比较元素value的函数

参考:

https://blog.csdn.net/sevenjoin/article/details/81943864

python3:

将输入的数据作为一对,使用元组 tuple,元组 (sum,1111222) ,使用split(‘ ‘)去除输入的空格。

即:

  1. tup = tuple(input().split(' '))

将元组存入字典中,得到字典 {sum:1111222}。

即:

  1. phoneBook[tup[0]] = tup[1]

查找字典中是否包含 name:

  1. for num in range(0, n):
  2. name = input()
  3. if name in phoneBook:
  4. print("%s=%s" %(name,phoneBook[name]))
  5. else:
  6. print('Not found')

所有代码如下:

  1. n = int(input())
  2. phoneBook = {}
  3. for num in range(0, n):
  4. #输入的是元组(sum,1111222)
  5. tup = tuple(input().split(' '))
  6. #将元组存入字典中 {sum:1111222}
  7. phoneBook[tup[0]] = tup[1]
  8. for num in range(0, n):
  9. name = input()
  10. if name in phoneBook:
  11. print("%s=%s" %(name,phoneBook[name]))
  12. else:
  13. print('Not found')

发表评论

表情:
评论列表 (有 0 条评论,175人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Day 8

    今日阴雨绵绵 空气舒适,天雷滚滚 适合发呆 偶尔学习。 再次感受到屁股经验决定脑袋。 从某种角度上说 命运是固定的 决定的。你的立场 你的经验 一出生不就定型了么。所以没有

    相关 Taro and Map

    我太累了,懒得加注释了 页面上一共三个按钮,实现了三个功能: 【1.打开微信手机地图选位置】 ![watermark_type_ZmFuZ3poZW5naG