C++进阶-3-5-set/multiset容器
1 #include<iostream>
2 #include<set>
3 using namespace std;
4
5 // set/multiset容器
6
7 void printSet(set<int>& s) {
8
9 for (set<int>::iterator it = s.begin(); it != s.end(); it++)
10 {
11 cout << *it << " ";
12 }
13 cout << endl;
14 }
15
16 // 1.构造和赋值
17 void test01() {
18
19 set<int> s1;
20
21 // 插入数据,只有insert
22 s1.insert(10);
23 s1.insert(40);
24 s1.insert(30);
25 s1.insert(20);
26 s1.insert(30);
27
28 // 元素插入,自动排序,且不运行有重复数据
29 printSet(s1);
30
31 // 拷贝构造
32 set<int>s2(s1);
33 printSet(s2);
34
35 // 赋值
36 set<int>s3;
37 s3 = s2;
38 printSet(s3);
39
40 }
41
42 // 2.大小和交换
43 void test02() {
44
45 set<int> s1;
46
47 s1.insert(10);
48 s1.insert(30);
49 s1.insert(20);
50 s1.insert(40);
51
52 printSet(s1);
53
54 // 判断是否为空
55 if (s1.empty()) {
56 cout << "s1为空" << endl;
57 }
58 else {
59 cout << "s1不为空" << endl;
60 cout << "s1的大小为:" << s1.size() << endl;
61 }
62
63 // 交换
64 set<int> s2;
65
66 s2.insert(100);
67 s2.insert(300);
68 s2.insert(200);
69 s2.insert(400);
70
71 cout << "交换前:" << endl;
72 printSet(s1);
73 printSet(s2);
74
75 cout << "交换后:" << endl;
76 s1.swap(s2);
77 printSet(s1);
78 printSet(s2);
79
80 }
81
82 // 3.插入和删除
83 void test03() {
84
85 set<int> s1;
86
87 // 插入
88 s1.insert(10);
89 s1.insert(30);
90 s1.insert(20);
91 s1.insert(40);
92
93 // 遍历
94 printSet(s1);
95
96 // 删除
97 s1.erase(s1.begin());
98 printSet(s1);
99
100 // 删除重载版本
101 s1.erase(30);
102 printSet(s1);
103
104 // 清空
105 //s1.erase(s1.begin(), s1.end())
106 s1.clear();
107 printSet(s1);
108
109 }
110
111 // 4.查找和统计
112 void test04() {
113
114 set<int> s1;
115
116 s1.insert(10);
117 s1.insert(30);
118 s1.insert(20);
119 s1.insert(40);
120
121 printSet(s1);
122
123 // 查找
124 // 存在,返回该元素的迭代器,不存在,返回set.end();
125
126 set<int>::iterator pos = s1.find(30);
127 if (pos != s1.end()) {
128 cout << "找到元素" << endl;
129 }
130 else
131 {
132 cout << "未找到元素" << endl;
133 }
134
135 // 统计
136 int num = s1.count(40); // 统计40的个数
137 cout << "num = " << num << endl;
138 // 对于set而言,统计结果,要么是0, 要么是1
139
140 }
141
142 // 5. set和multiset区别
143 void test05() {
144
145 set<int> s;
146
147 // set插入数据的同时还会返回插入的结果,表示插入是否成功
148 pair<set<int>::iterator, bool>set = s.insert(10);
149
150 if (set.second) {
151 cout << "第一次插入成功!" << endl;
152 }
153 else
154 {
155 cout << "第一次插入失败!" << endl;
156 }
157
158 set = s.insert(10);
159
160 if (set.second) {
161 cout << "第二次插入成功!" << endl;
162 }
163 else
164 {
165 cout << "第二次插入失败!" << endl;
166 }
167
168
169 // multiset允许插入重复值
170 multiset<int>ms;
171 ms.insert(10);
172 ms.insert(10);
173 ms.insert(10);
174
175 for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++) {
176 cout << *it << " ";
177 }
178 cout << endl;
179 }
180
181
182 // 6.pair对组创建
183 void test06() {
184
185 // pair对组,成对出现的数据,利用对组可以返回两个数据
186
187 // 第一种方式
188 pair<string, int>p(string("Tom"), 20);
189 cout << "姓名:" << p.first << " 年龄:" << p.second << endl;
190
191 // 第二种数据
192 pair<string, int>p2 = make_pair("Jerry", 30);
193 cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
194
195 }
196
197 // 7.set容器排序
198
199 class MyCompare {
200 public:
201 // 仿函数,重写(),类似于函数
202 bool operator()(int v1, int v2) {
203 return v1 > v2;
204 }
205 };
206
207 void test07() {
208
209 // set排序是由小到大的升序
210 // 利用仿函数,可以改变排序规则
211
212 set<int> s1;
213
214 s1.insert(10);
215 s1.insert(30);
216 s1.insert(20);
217 s1.insert(40);
218
219 printSet(s1);
220
221 // 改为从大到小,在插入之前操作
222
223 set<int, MyCompare> s2;
224
225 s2.insert(10);
226 s2.insert(30);
227 s2.insert(20);
228 s2.insert(40);
229
230 for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
231 {
232 cout << *it << " ";
233 }
234 cout << endl;
235 }
236
237
238 int main() {
239
240 // 1.构造和赋值
241 //test01();
242
243 // 2.大小和交换
244 //test02();
245
246 // 3.插入和删除
247 //test03();
248
249 // 4.查找和统计
250 //test04();
251
252 // 5. set和multiset区别
253 //test05();
254
255 // 6.pair对组创建
256 //test06();
257
258 // 7.set容器排序--内置数据类型
259 //test07();
260
261 system("pause");
262
263 return 0;
264 }
265
266 // 总结
267 //
268 // set/multiset容器
269 //
270 // 简介:所有元素都会在插入时自动排序
271 //
272 // 本质:set/multiset属于关联式容器,底层结构是二叉树实现
273 //
274 // set/multiset区别:
275 // 1.set不允许容器中有重复的元素
276 // 2.multiset允许容器中有重复的元素
277 // 3.set插入数据的同时还会返回插入的结果,表示插入是否成功
278 // 4.multiset不会检测数据,因此可以插入重复数据
279 //
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
我不知道为什么,但是当我设置这个设置时它无法编译设置:static_cache_control,[:public,:max_age=>300]这是我得到的syntaxerror,unexpectedtASSOC,expecting']'(SyntaxError)set:static_cache_control,[:public,:max_age=>300]^我只想将“过期”header设置为css、javaascript和图像文件。谢谢。 最佳答案 我猜您使用的是Ruby1.8.7。Sinatra文档中显示的语法似乎是在Ruby1.
通常,数组被实现为内存块,集合被实现为HashMap,有序集合被实现为跳跃列表。在Ruby中也是如此吗?我正在尝试从性能和内存占用方面评估Ruby中不同容器的使用情况 最佳答案 数组是Ruby核心库的一部分。每个Ruby实现都有自己的数组实现。Ruby语言规范只规定了Ruby数组的行为,并没有规定任何特定的实现策略。它甚至没有指定任何会强制或至少建议特定实现策略的性能约束。然而,大多数Rubyist对数组的性能特征有一些期望,这会迫使不符合它们的实现变得默默无闻,因为实际上没有人会使用它:插入、前置或追加以及删除元素的最坏情况步骤复
我正在关注Hartl的railstutorial.org并已到达11.4.4:Imageuploadinproduction.我做了什么:注册亚马逊网络服务在AmazonIdentityandAccessManagement中,我创建了一个用户。用户创建成功。在AmazonS3中,我创建了一个新存储桶。设置新存储桶的权限:权限:本教程指示“授予上一步创建的用户读写权限”。但是,在存储桶的“权限”下,未提及新用户名。我只能在每个人、经过身份验证的用户、日志传送、我和亚马逊似乎根据我的名字+数字创建的用户名之间进行选择。我已经通过选择经过身份验证的用户并选中了上传/删除和查看权限的框(而不
我有一个如下所示的数组:nodes=['server1','server1','server2']在厨师食谱中,我需要在传递给模板erb之前将其转换为集合。我该怎么做? 最佳答案 此模式适用于Set、Matrix、JSON等;这是尝试的第一件事。require'set'nodes=['server1','server1','server2']pnodes.to_set## 关于ruby-厨师和ruby:howtoconvertaarrayintoaset,我们在StackOverflow
我每次打开终端时都会收到这个错误:警告:PATH设置为RVMruby但未设置GEM_HOME和/或GEM_PATH,请参阅:https://github.com/wayneeseguin/rvm/issues/3212这是在我最近安装zsh(oh-my-zsh)后开始发生的我不知道如何设置GEM_HOME和/或GEM_PATH的路径。 最佳答案 我也面临同样的问题,更改.zshrc中的以下行,exportPATH="/usr/local/heroku/bin:.........."到exportPATH="$PATH:/usr/
在我的schedule.rb文件中,我有以下几行:set:output,'/log/cron_log.log'every5.minutesdocommand'echo"hello"'end我按照这个问题Rails,usingwhenevergemindevelopment中的建议运行了whenever-w,并且我假设cronfile已编写并正在运行。(我也尝试重新启动Rails服务器。)当我运行$crontab-l时,我看到以下内容:0,5,10,15,20,25,30,35,40,45,50,55****/bin/bash-l-c'echo"hello">>/log/cron_log
我一直在为使用acts_as_list的模型实现一些不错的交互界面,这些界面可以对我的mRails应用程序中的列表进行排序。我有一个排序函数,在每次拖放之后使用sortable_elementscript.aculo.us函数调用并设置每条记录的位置。这是在拖放完成后处理排序的Controller操作示例:defsortparams[:documents].each_with_indexdo|id,index|Document.update_all(['position=?',index+1],['id=?',id])endend现在我正在尝试对嵌套集模型(acts_as_nested
假设我有一个变量directory_list,我在名为get_directory_list的ruby_block中定义和设置了它。我可以稍后在我的Recipe中使用directory_list吗,或者编译/收敛过程会阻止这种情况吗?例子:ruby_block"get_file_list"doblockdotransferred_files=Dir['/some/dir/*']endendtransferred_files.eachdo|file|file"#{file}"dogroup"woohoo"user"woohoo"endend 最佳答案
根据RubySet类的文档,“==如果两个集合相等,则返回true。每对元素的相等性根据Object#eql?定义。可以使用Date对象来演示其本质,其中包含不同Date对象但具有相同日期的集合比较相等:require'set'd1=Date.today#=>Thu,30Sep2010putsd1.object_id#=>2211539680d2=Date.today+1#=>Fri,01Oct2010putsd2.object_id#=>2211522320set1=Set.new([d1,d2])d11=Date.today#=>Thu,30Sep2010putsd11.objec