map的访问

映射表(map)

  在每个条目被插入时将之按键进行排序。取迭代器指向值时将返回value_type结构,它有两个数据成员:first,second。访问first获得键的数据,访问second获得值的数据。

  除了迭代器访问外,映射表还提供通过它们的键值随机访问的接口(可以用数组进行访问)。

 1 #pragma warning (disable:4786)
 2 #include<iostream>
 3 #include<map>
 4 #include<string>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 template<typename F,typename S>
 9 class value_equals
10 {
11 private:
12     S second;
13 public:
14     value_equals(const S& s) :second(s){}
15     bool operator()(pair<const F, S> elem){ return elem.second == second; }
16 };
17 
18 typedef map<int, string> isMap;
19 typedef isMap::value_type isValType;
20 typedef isMap::iterator isMapItor;
21 
22 void main()
23 {
24     isMap c;
25 
26     c.insert(isValType(100, "OneHundred"));
27     c.insert(isValType(3, "Three"));
28     c.insert(isValType(150, "OneHundredFifty"));
29     c.insert(isValType(99, "NinetyNine"));
30 
31     for (isMapItor itor = c.begin(); itor != c.end(); ++itor)
32     {
33         cout << "Key = " << itor->first << ",Value = " << itor->second << endl;
34     }
35 
36     //你可以通过关联数组方式访问映射表
37     cout << "Key 3 displays value " << c[3].c_str() << endl;
38     //或者以关联数组方式插入键/值对
39     c[123] = "OneHundredTwentyThree";
40     //基于键找到并删除一个特定值
41     isMapItor pos = c.find(123);
42     if (pos != c.end())
43         c.erase(pos);
44 
45     //基于值找到并删除元素
46     pos = find_if(c.begin(), c.end(), value_equals<int, string>("NinetyNine"));
47     if (pos != c.end())
48         c.erase(pos);
49     //如果你需要在遍历链表时删除元素
50     for (isMapItor itor = c.begin(); itor != c.end();)
51     {
52         if (itor->second == "Three")
53             c.erase(itor++);
54         else
55             ++itor;
56     }
57 }

 

  注意 !!

 for (isMapItor itor = c.begin(); itor != c.end();)
     {
       if (itor->second == "Three")
           c.erase(itor++);
       else
            ++itor;
     }

  如果你需要迭代遍历并手工在映射表中删除对象,需要注意。STL设计者没有像其他容器那样提供一个erase()函数来删除特定元素并返回下一个有效位置。所以,我们不得不在删除后重新排序,以使我们的迭代器不会失效。在这个例子中,与在for循环语句中步进迭代器不同,我们在循环体内以条件语句的方式完成迭代器工作。注意,当一个元素需要删除,我们在将迭代器作为参数传递给erase()后使用后置递增运算符步进此迭代器。但如果此元素不需要删除,我们使用标准前置递增运算符。因为操作顺序的不同,这个方法允许在不使用临时迭代器进行重新排序的情况下进行安全的迭代操作。 

转载于:https://www.cnblogs.com/ll-10/p/5461678.html

### C++ 中 map 数据结构访问方法和示例 在 C++ 中,`std::map` 是一种关联容器,用于存储键值对(key-value pairs)。`std::map` 的键是唯一的,并且按键排序。可以通过多种方式访问 `std::map` 中的数据,包括使用下标运算符、迭代器以及成员函数如 `find()` 和 `at()`。 以下是对这些访问方法的详细介绍和示例代码: #### 1. 使用下标运算符 (`operator[]`) 下标运算符可以用来访问或插入元素。如果指定的键不存在于 `map` 中,`operator[]` 会创建一个默认构造的值并将其该键关联[^1]。 ```cpp #include <iostream> #include <map> int main() { std::map<char, int> myMap; myMap['a'] = 10; // 插入或修改键 'a' 的值为 10 myMap['b'] = 20; // 访问键 'a' 的值 std::cout << "Value of 'a': " << myMap['a'] << std::endl; return 0; } ``` #### 2. 使用 `at()` 成员函数 `at()` 成员函数用于访问指定键的值。 `operator[]` 不同,`at()` 在找不到指定键时会抛出 `std::out_of_range` 异常[^1]。 ```cpp #include <iostream> #include <map> int main() { std::map<char, int> myMap = { {'a', 10}, {'b', 20} }; try { std::cout << "Value of 'a': " << myMap.at('a') << std::endl; std::cout << "Value of 'c': " << myMap.at('c') << std::endl; // 抛出异常 } catch (const std::out_of_range& e) { std::cerr << "Error: " << e.what() << std::endl; } return 0; } ``` #### 3. 使用迭代器遍历 `map` 可以通过迭代器遍历整个 `map`。每个迭代器指向一个键值对,可以通过 `->first` 访问键,通过 `->second` 访问值。 ```cpp #include <iostream> #include <map> int main() { std::map<char, int> myMap = { {'a', 10}, {'b', 20}, {'c', 30} }; for (auto it = myMap.begin(); it != myMap.end(); ++it) { std::cout << it->first << " => " << it->second << std::endl; } return 0; } ``` #### 4. 使用 `find()` 成员函数 `find()` 成员函数用于查找指定键的迭代器。如果键存在,则返回指向该键的迭代器;否则返回 `end()` 迭代器。 ```cpp #include <iostream> #include <map> int main() { std::map<char, int> myMap = { {'a', 10}, {'b', 20}, {'c', 30} }; auto it = myMap.find('b'); if (it != myMap.end()) { std::cout << "Key 'b' found with value: " << it->second << std::endl; } else { std::cout << "Key 'b' not found" << std::endl; } return 0; } ``` ### 总结 C++ 中的 `std::map` 提供了多种访问数据的方式,包括下标运算符、`at()` 成员函数、迭代器以及 `find()` 成员函数。每种方式都有其适用场景,开发者可以根据具体需求选择合适的访问方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值