草庐IT

C++进阶-3-6-map/multimap容器

LYH-win 2023-03-28 原文

C++进阶-3-6-map/multimap容器

  1 #include<iostream>
  2 #include<map>
  3 using namespace std;
  4 
  5 // map / multimap容器
  6 
  7 void printMap(map<int, int>& m) {
  8     for (map<int, int>::iterator it = m.begin(); it != m.end(); it++) {
  9         cout << "key = " << (*it).first << " value = " << it->second << endl;
 10     }
 11     cout << endl;
 12 }
 13 
 14 // 1.构造和赋值
 15 void test01() {
 16 
 17     // 创建map容器
 18     map<int, int> m;
 19 
 20     m.insert(pair<int, int>(1, 10));
 21     m.insert(pair<int, int>(2, 20));
 22     m.insert(pair<int, int>(3, 30));
 23     m.insert(pair<int, int>(4, 40));
 24 
 25     printMap(m);
 26 
 27     // 拷贝构造
 28     map<int, int>m2(m);
 29     printMap(m2);
 30 
 31     // 赋值
 32     map<int, int>m3;
 33     m3 = m2;
 34     printMap(m3);
 35 
 36 }
 37 
 38 // 2.大小和交换
 39 void test02() {
 40 
 41     map<int, int> m;
 42 
 43     m.insert(pair<int, int>(1, 10));
 44     m.insert(pair<int, int>(2, 20));
 45     m.insert(pair<int, int>(3, 30));
 46     m.insert(pair<int, int>(4, 40));
 47 
 48     //printMap(m);
 49 
 50     // 大小
 51     if (m.empty()) {
 52         cout << "m 为空" << endl;
 53     }
 54     else
 55     {
 56         cout << "m 不为空" << endl;
 57         cout << "m 的大小为:" << m.size() << endl;
 58     }
 59 
 60     // 交换
 61     map<int, int> m2;
 62 
 63     m2.insert(pair<int, int>(5, 50));
 64     m2.insert(pair<int, int>(6, 60));
 65     m2.insert(pair<int, int>(7, 70));
 66     m2.insert(pair<int, int>(8, 80));
 67 
 68     cout << "交换前:" << endl;
 69     printMap(m);
 70     printMap(m2);
 71 
 72     cout << "交换后:" << endl;
 73     m.swap(m2);
 74     printMap(m);
 75     printMap(m2);
 76 }
 77 
 78 // 3.插入和删除
 79 void test03() {
 80 
 81     map<int, int> m;
 82 
 83     // 插入
 84     // 第一种
 85     m.insert(pair<int, int>(1, 10));
 86     printMap(m);
 87 
 88     // 第二种
 89     m.insert(make_pair(2, 20));
 90     printMap(m);
 91 
 92     // 第三种
 93     m.insert(map<int, int>::value_type(3, 30));
 94     printMap(m);
 95 
 96     // 第四种
 97     m[4] = 40;  // 不建议插入使用
 98     printMap(m);
 99 
100 
101     // 删除
102     m.erase(m.begin());
103     printMap(m);
104 
105     m.erase(3);  // 按照key删除
106     printMap(m);
107 
108     // 清空
109     //m.erase(m.begin(), m.end());
110     m.clear();
111     printMap(m);
112 }
113 
114 // 4.查找和统计
115 void test04() {
116 
117     map<int, int> m;
118 
119     m.insert(pair<int, int>(1, 10));
120     m.insert(pair<int, int>(2, 20));
121     m.insert(pair<int, int>(3, 30));
122     m.insert(pair<int, int>(4, 40));
123 
124     printMap(m);
125 
126     // 查找,find返回的是迭代器
127     map<int, int>::iterator pos = m.find(3);
128 
129     if (pos != m.end()) {
130         cout << "查到了元素,key = " << (*pos).first << " value = " << pos->second << endl;
131     }
132     else
133     {
134         cout << "未找到元素" << endl;
135     }
136 
137     // 统计, map中无重复的key,所以,统计值为0或1
138     // multimap的count统计可能大于1
139     int num = m.count(1);
140     cout << "num = " << num << endl;
141 
142 }
143 
144 // 5. 排序
145 
146 class MyCompare {
147 public:
148     bool operator()(int v1, int v2) {
149         // 降序
150         return v1 > v2;
151     }
152 
153 };
154 
155 void printMap1(map<int, int, MyCompare>& m) {
156     for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++) {
157         cout << "key = " << (*it).first << " value = " << it->second << endl;
158     }
159     cout << endl;
160 }
161 
162 void test05() {
163 
164     map<int, int> m;
165 
166     m.insert(pair<int, int>(1, 10));
167     m.insert(pair<int, int>(2, 20));
168     m.insert(pair<int, int>(3, 30));
169     m.insert(pair<int, int>(4, 40));
170 
171     // 排序 默认:从小到大,升序
172     printMap(m);
173 
174     // 降序
175     map<int, int, MyCompare> m2;
176 
177     m2.insert(pair<int, int>(1, 10));
178     m2.insert(pair<int, int>(2, 20));
179     m2.insert(pair<int, int>(3, 30));
180     m2.insert(pair<int, int>(4, 40));
181     
182     printMap1(m2);
183 
184 }
185 
186 int main() {
187 
188     // 1.构造和赋值
189     //test01();
190 
191     // 2.大小和交换
192     //test02();
193 
194     // 3.插入和删除
195     //test03();
196 
197     // 4.查找和统计
198     //test04();
199 
200     // 5. 排序,默认,从小到大升序
201     test05();
202 
203     system("pause");
204 
205     return 0;
206 }
207 
208 // 总结
209 // 
210 // map / multimap容器
211 // 
212 // 简介:
213 //    map中所有元素都是pair
214 //    pair中第一个元素为key,起索引作用,第二个元素为value
215 //    所有元素都回根据元素的键值自动排序
216 // 
217 // 本质:属于关联式容器,地层结构用二叉树实现
218 // 
219 // 优点:可以根据key值快速找到value值
220 // 
221 // map / multimap区别:
222 //    map不允许容器中有重复的key
223 //    multi允许容器中有重复的key
224 //  

 

有关C++进阶-3-6-map/multimap容器的更多相关文章

  1. ruby - 在 ruby​​ 中使用 .try 函数和 .map 函数 - 2

    我需要从json记录中获取一些值并像下面这样提取curr_json_doc['title']['genre'].map{|s|s['name']}.join(',')但对于某些记录,curr_json_doc['title']['genre']可以为空。所以我想对map和join()使用try函数。我试过如下curr_json_doc['title']['genre'].try(:map,{|s|s['name']}).try(:join,(','))但是没用。 最佳答案 你没有正确传递block。block被传递给参数括号外的方法

  2. ruby - 不能将 `each` 的所有或大多数情况替换为 `map` 吗? - 2

    Enumerable#each和Enumerable#map的区别在于返回的是接收者还是映射后的结果。回到接收者是微不足道的,你通常不需要在each之后继续一个方法链,比如each{...}.another_method(我可能没见过这样的案例。即使你想回到接收者那里,你也可以通过tap来实现)。所以我认为所有或者大部分使用Enumerable#each的情况都可以用Enumerable#map代替。我错了吗?如果我是对的,each的目的是什么?map是否比each慢?编辑:我知道当您对返回值不感兴趣时​​使用each是一种常见的做法。我对这种做法是否存在不感兴趣,但感兴趣的是,除了从

  3. ruby - `map` 比 `each` 快吗? - 2

    map遍历数组是否比each更快?两者有速度差异吗?mapresult=arr.map{|a|a+2}每个result=[]arr.eachdo|a|result.push(a+2)end 最佳答案 我认为是的。我试过这个测试require"benchmark"n=10000arr=Array.new(10000,1)Benchmark.bmdo|x|#Mapx.reportdon.timesdoresult=arr.map{|a|a+2}endend#Eachx.reportdon.timesdoresult=[]arr.each

  4. ruby - 用于 Ruby 哈希的 map_values()? - 2

    我想念Ruby中的Hash方法来仅转换/映射散列值。h={1=>[9,2,3,4],2=>[6],3=>[5,7,1]}h.map_values{|v|v.size}#=>{1=>4,2=>1,3=>3}你如何在Ruby中归档它?更新:我正在寻找map_values()的实现。#moreexamplesh.map_values{|v|v.reduce(0,:+)}#=>{1=>18,2=>6,3=>13}h.map_values(&:min)#=>{1=>2,2=>6,3=>1} 最佳答案 Ruby2.4引入了方法Hash#tran

  5. ruby - 了解 Ruby Enumerable#map(具有更复杂的 block ) - 2

    假设我有一个函数defodd_or_evennifn%2==0return:evenelsereturn:oddendend我有一个简单的可枚举数组simple=[1,2,3,4,5]然后我用我的函数在map中运行它,使用一个do-endblock:simple.mapdo|n|odd_or_even(n)end#=>[:odd,:even,:odd,:even,:odd]如果不首先定义函数,我怎么能做到这一点?例如,#doesnotworksimple.mapdo|n|ifn%2==0return:evenelsereturn:oddendend#Desiredresult:#=>[

  6. ruby - 将 each_with_index 与 map 一起使用 - 2

    我想获取一个数组并将其作为订单列表。目前我正在尝试以这种方式进行:r=["a","b","c"]r.each_with_index{|w,index|puts"#{index+1}.#{w}"}.map.to_a#1.a#2.b#3.c#=>["a","b","c"]输出应该是["1.a","2.b","3.c"]。如何让正确的输出成为r数组的新值? 最佳答案 a.to_enum.with_index(1).map{|element,index|"#{index}.#{element}"}或a.map.with_index(1){|

  7. javascript - jQuery 的 jquery-1.10.2.min.map 正在触发 404(未找到) - 2

    我看到有关未找到文件min.map的错误消息:GETjQuery'sjquery-1.10.2.min.mapistriggeringa404(NotFound)截图这是从哪里来的? 最佳答案 如果ChromeDevTools报告.map文件的404(可能是jquery-1.10.2.min.map、jquery.min.map或jquery-2.0.3.min.map,但任何事情都可能发生)首先要知道的是,这仅在使用DevTools时才会请求。您的用户不会遇到此404。现在您可以修复此问题或禁用sourcemap功能。修复:获取文

  8. ruby - `map` 是否使用 `each`? - 2

    在Ruby中,Enumerable模块混合到集合类中,并依赖于提供each方法的类,该方法产生集合中的每个项目。好吧,如果我想在我自己的类中使用Enumerable,我将只实现each[1]:classColorsincludeEnumerabledefeachyield"red"yield"green"yield"blue"endend>c=Colors.new>c.map{|i|i.reverse}#=>["der","neerg","eulb"]这按预期工作。但是,如果我用Enumerable重写现有类上的each方法,它不会破坏map等函数。为什么不呢?classArrayde

  9. Ruby:如何编写像 map 这样的 bang 方法? - 2

    我想编写一些新的Array方法来改变调用对象,如下所示:a=[1,2,3,4]a.map!{|e|e+1}a=[2,3,4,5]...但我对如何执行此操作一无所知。我想我需要一个新的大脑。所以,我想要这样的东西:classArraydefstuff!#changethecallingobjectinsomewayendendmap!只是一个例子,我想写一个全新的,而不使用任何预先存在的!方法。谢谢! 最佳答案 编辑-更新答案以反射(reflect)对您问题的更改。classArraydefstuff!self[0]="a"enden

  10. ruby - 在 ruby​​ 中优雅地实现 'map (+1) list' - 2

    title中的短代码是在Haskell中,它做了类似的事情list.map{|x|x+1}ruby。虽然我知道那种方式,但我想知道的是,是否有更优雅的方式来像在Haskell中一样在ruby​​中实现同样的事情。我真的很喜欢ruby​​中的to_proc快捷方式,就像这样:[1,2,3,4].map(&:to_s)[1,2,3,4].inject(&:+)但这只接受Proc和方法之间完全匹配的参数数。我正在尝试寻找一种方法,允许将一个或多个参数额外传递到Proc,而不像第一个演示那样使用无用的临时block/变量。我想这样做:[1,2,3,4].map(&:+(1))ruby是否有类似

随机推荐