-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.xml
473 lines (228 loc) · 355 KB
/
search.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
<?xml version="1.0" encoding="utf-8"?>
<search>
<entry>
<title>卷积、池化计算</title>
<link href="/2024/08/31/juan-ji-chi-hua-ji-suan/"/>
<url>/2024/08/31/juan-ji-chi-hua-ji-suan/</url>
<content type="html"><![CDATA[<p>要设计卷积神经网络的结构,必须<strong>匹配层与层之间的输入与输出的尺寸</strong>,这就需要较好的计算输出尺寸</p><h2 id="1-卷积"><a href="#1-卷积" class="headerlink" title="1. 卷积"></a>1. 卷积</h2><p>卷积层的作用是<strong>提取输入图片中的信息,这些信息被称为图像特征</strong>,这些特征是由图像中的每个像素通过组合或者独立的方式所体现,比如图片的纹理特征,颜色特征。</p><p>卷积层有很多卷积核,通过做越来越多的卷积,提取到的图像特征会越来越抽象。</p><p>卷积后,图像尺寸计算公式为:**(图像大小 - 卷积核大小 + 2<em>填充值) / 步长 + 1</em>*</p><p>即:</p><p><img src="/2024/08/31/juan-ji-chi-hua-ji-suan/%E5%8D%B7%E7%A7%AF%E5%85%AC%E5%BC%8F.png"></p><pre class="line-numbers language-txt" data-language="txt"><code class="language-txt">其中:N:输出矩阵的大小,N x NW:输入图片的大小,W x WK:卷积核kernel_size大小,K x KP:填充值padding大小S:步长stride大小<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>以resnet50为例,输入为[1, 3, 224, 224],其中1为 batchsize,3为通道数,224为height和width。</p><p>经过第一层卷积后,其大小为[1,64,112,112]</p><pre class="line-numbers language-python" data-language="python"><code class="language-python">in_channels<span class="token operator">=</span> <span class="token number">3</span> <span class="token operator">//</span>输入通道,RGB图像作为输入时channels为<span class="token number">3</span>out_channels<span class="token operator">=</span> <span class="token number">64</span> <span class="token operator">//</span>输出通道nn<span class="token punctuation">.</span>Conv2d<span class="token punctuation">(</span>in_channels<span class="token punctuation">,</span> out_channels<span class="token punctuation">,</span> kernel_size<span class="token operator">=</span><span class="token number">7</span><span class="token punctuation">,</span> stride<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">,</span> bias<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><p>N = (W − K + 2P ) / S + 1 = (224 - 7 + 2 x 3) / 2 + 1 = 112</p><p><strong>对矩阵和公式的理解:</strong></p><p>1为batchsize,不改变。<br>对于通道数,会生成与设定的输出通道个数相同个数的卷积核,对图片进行卷积,即<strong>卷积核的个数等于输出特征图的通道数</strong>。<br>得到最终输出大小为[1,64,112,112]<br>(W − K + 2P )相当于计算除了第一次卷积后剩下的可用来卷积的大小<br>(W − K + 2P ) / S为按照S大小的步长在刚刚得到的大小上可以向后移动多少次,即:还可以做几次卷积<br>因为不包括第一次卷积,所以再加上一个1,<br>即N = (W − K + 2P )/S+1<br>输出大小 = (图片宽或高 - 卷积核大小 + padding大小)/ 步长 + 1<br>对于宽和高不同的图片可分别用上述公式计算,得到最终的输出大小</p><p>卷积动态图解参考:<br><a href="https://cs231n.github.io/assets/conv-demo/index.html">https://cs231n.github.io/assets/conv-demo/index.html</a></p><h2 id="2-池化"><a href="#2-池化" class="headerlink" title="2. 池化"></a>2. 池化</h2><p>池化层的作用是对卷积层中提取的特征进行挑选,分为<strong>最大池化</strong>和<strong>平均池化</strong>,就是对所有相应位置的数求<strong>最大</strong>或<strong>平均值</strong>,一般用来将高和宽降维。而1*1卷积中的降维降的是通道数的维度。</p><p>最大池化:Max Pooling,取窗口内的最大值作为输出</p><p><img src="/2024/08/31/juan-ji-chi-hua-ji-suan/%E6%9C%80%E5%A4%A7%E6%B1%A0%E5%8C%96.png"></p><p>平均池化:Avg Pooling,去窗口内所有值的平均值作为输出</p><p><img src="/2024/08/31/juan-ji-chi-hua-ji-suan/%E5%B9%B3%E5%9D%87%E6%B1%A0%E5%8C%96.png"></p><p>池化后,图像尺寸的计算公式为:**(图像大小 - 池化核大小 + 2<em>填充值) / 步长 + 1</em>*</p><p>即:</p><p><img src="/2024/08/31/juan-ji-chi-hua-ji-suan/%E6%B1%A0%E5%8C%96%E5%85%AC%E5%BC%8F.png"></p><pre class="line-numbers language-none"><code class="language-none">其中:N:输出矩阵的大小,N x NW:输入图片的大小,W x WK:池化核kernel_size大小,K x KP:填充值padding大小。池化一般不使用填充padding。S:步长stride大小<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>池化会对特征进行降维,但是<strong>不会改变通道数</strong></p><p>示例:</p><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> torch<span class="token keyword">import</span> torch<span class="token punctuation">.</span>nn <span class="token keyword">as</span> nn<span class="token comment"># 四维分别对应:(batch_size,channel,H,W)</span>im <span class="token operator">=</span> torch<span class="token punctuation">.</span>randn<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">64</span><span class="token punctuation">,</span> <span class="token number">112</span><span class="token punctuation">,</span> <span class="token number">112</span><span class="token punctuation">)</span><span class="token comment"># 卷积核为3*3,步长为2,上下左右各填充1行</span>c <span class="token operator">=</span> nn<span class="token punctuation">.</span>MaxPool2d<span class="token punctuation">(</span>kernel_size<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">,</span> stride<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>output <span class="token operator">=</span> c<span class="token punctuation">(</span>im<span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"原张量尺寸:"</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>im<span class="token punctuation">.</span>shape<span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">"经最大池化处理后的张量尺寸:"</span><span class="token punctuation">)</span><span class="token keyword">print</span><span class="token punctuation">(</span>output<span class="token punctuation">.</span>shape<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>原张量尺寸为:[1, 64, 112, 112]</p><p>池化后张量尺寸为:[1, 64, 56, 56]</p>]]></content>
<categories>
<category> 深度学习 </category>
</categories>
<tags>
<tag> 深度学习基础 </tag>
<tag> 卷积 </tag>
<tag> 池化 </tag>
</tags>
</entry>
<entry>
<title>深度学习模型训练全过程</title>
<link href="/2024/08/29/shen-du-xue-xi-mo-xing-xun-lian-quan-guo-cheng/"/>
<url>/2024/08/29/shen-du-xue-xi-mo-xing-xun-lian-quan-guo-cheng/</url>
<content type="html"><![CDATA[<p>本文记录学习深度学习模型训练的流程,包括:数据准备、定义并调用模型、训练模型、评估模型、保存模型、测试模型等</p><h2 id="1-环境准备"><a href="#1-环境准备" class="headerlink" title="1. 环境准备"></a>1. 环境准备</h2><p>首先引入一些深度学习模型所需要的库和包</p><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">import</span> numpy <span class="token keyword">as</span> np<span class="token keyword">import</span> torch<span class="token keyword">import</span> torch<span class="token punctuation">.</span>nn <span class="token keyword">as</span> nn<span class="token keyword">import</span> torch<span class="token punctuation">.</span>optim <span class="token keyword">as</span> optim<span class="token keyword">import</span> torchvision<span class="token keyword">import</span> torchvision<span class="token punctuation">.</span>transforms <span class="token keyword">as</span> transforms<span class="token keyword">from</span> torch<span class="token punctuation">.</span>utils<span class="token punctuation">.</span>data <span class="token keyword">import</span> DataLoader<span class="token keyword">from</span> torch<span class="token punctuation">.</span>utils<span class="token punctuation">.</span>tensorboard <span class="token keyword">import</span> SummaryWriter<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="2-数据准备"><a href="#2-数据准备" class="headerlink" title="2. 数据准备"></a>2. 数据准备</h2><p>定义数据变换;</p><p>下载并加载训练集和测试集;</p><p>创建数据加载器</p><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token keyword">def</span> <span class="token function">get_training_dataloader</span><span class="token punctuation">(</span>mean<span class="token punctuation">,</span> std<span class="token punctuation">,</span> batch_size<span class="token operator">=</span><span class="token number">16</span><span class="token punctuation">,</span> num_workers<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> shuffle<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token triple-quoted-string string">""" return training dataloader Args: mean: mean of cifar100 training dataset std: std of cifar100 training dataset path: path to cifar100 training python dataset batch_size: dataloader batchsize num_workers: dataloader num_works shuffle: whether to shuffle Returns: train_data_loader:torch dataloader object """</span><span class="token comment">#对数据进行随机变换生成新的训练样本,提高泛化能力,减少过拟合;将不同格式的数据转换为模型可以处理的统一格式;归一化</span> transform_train <span class="token operator">=</span> transforms<span class="token punctuation">.</span>Compose<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token comment">#定义数据变换,一个数据预处理流水线</span> <span class="token comment">#transforms.ToPILImage(),</span> transforms<span class="token punctuation">.</span>RandomCrop<span class="token punctuation">(</span><span class="token number">32</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">#随机裁剪图像到 32x32 大小,裁剪前在图像周围填充 4 个像素</span> transforms<span class="token punctuation">.</span>RandomHorizontalFlip<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">#以一定的概率随机水平翻转图像</span> transforms<span class="token punctuation">.</span>RandomRotation<span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">#随机旋转图像,旋转角度在 -15 度到 15 度之间</span> transforms<span class="token punctuation">.</span>ToTensor<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">#将图像从 PIL 图像或 numpy 数组转换为 PyTorch 张量</span> transforms<span class="token punctuation">.</span>Normalize<span class="token punctuation">(</span>mean<span class="token punctuation">,</span> std<span class="token punctuation">)</span> <span class="token comment">#使用给定的均值和标准差对图像进行归一化处理</span> <span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">#cifar100_training = CIFAR100Train(path, transform=transform_train)</span> cifar100_training <span class="token operator">=</span> torchvision<span class="token punctuation">.</span>datasets<span class="token punctuation">.</span>CIFAR100<span class="token punctuation">(</span>root<span class="token operator">=</span><span class="token string">'./data'</span><span class="token punctuation">,</span> train<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> download<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> transform<span class="token operator">=</span>transform_train<span class="token punctuation">)</span> cifar100_training_loader <span class="token operator">=</span> DataLoader<span class="token punctuation">(</span> cifar100_training<span class="token punctuation">,</span> shuffle<span class="token operator">=</span>shuffle<span class="token punctuation">,</span> num_workers<span class="token operator">=</span>num_workers<span class="token punctuation">,</span> batch_size<span class="token operator">=</span>batch_size<span class="token punctuation">)</span> <span class="token keyword">return</span> cifar100_training_loader<span class="token keyword">def</span> <span class="token function">get_test_dataloader</span><span class="token punctuation">(</span>mean<span class="token punctuation">,</span> std<span class="token punctuation">,</span> batch_size<span class="token operator">=</span><span class="token number">16</span><span class="token punctuation">,</span> num_workers<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> shuffle<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token triple-quoted-string string">""" return training dataloader Args: mean: mean of cifar100 test dataset std: std of cifar100 test dataset path: path to cifar100 test python dataset batch_size: dataloader batchsize num_workers: dataloader num_works shuffle: whether to shuffle Returns: cifar100_test_loader:torch dataloader object """</span> transform_test <span class="token operator">=</span> transforms<span class="token punctuation">.</span>Compose<span class="token punctuation">(</span><span class="token punctuation">[</span> <span class="token comment">#定义数据变换</span> transforms<span class="token punctuation">.</span>ToTensor<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> transforms<span class="token punctuation">.</span>Normalize<span class="token punctuation">(</span>mean<span class="token punctuation">,</span> std<span class="token punctuation">)</span> <span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">#cifar100_test = CIFAR100Test(path, transform=transform_test)</span> cifar100_test <span class="token operator">=</span> torchvision<span class="token punctuation">.</span>datasets<span class="token punctuation">.</span>CIFAR100<span class="token punctuation">(</span>root<span class="token operator">=</span><span class="token string">'./data'</span><span class="token punctuation">,</span> train<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">,</span> download<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">,</span> transform<span class="token operator">=</span>transform_test<span class="token punctuation">)</span> cifar100_test_loader <span class="token operator">=</span> DataLoader<span class="token punctuation">(</span> cifar100_test<span class="token punctuation">,</span> shuffle<span class="token operator">=</span>shuffle<span class="token punctuation">,</span> num_workers<span class="token operator">=</span>num_workers<span class="token punctuation">,</span> batch_size<span class="token operator">=</span>batch_size<span class="token punctuation">)</span> <span class="token keyword">return</span> cifar100_test_loader<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="3-模型定义-x2F-调用模型"><a href="#3-模型定义-x2F-调用模型" class="headerlink" title="3. 模型定义/调用模型"></a>3. 模型定义/调用模型</h2><p>自定义一个模型或调用一个模型;</p><p>使用之前需要先实例化模型(以vgg为例)</p><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token comment"># 定义一个模型</span><span class="token keyword">class</span> <span class="token class-name">VGG</span><span class="token punctuation">(</span>nn<span class="token punctuation">.</span>Module<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> features<span class="token punctuation">,</span> num_class<span class="token operator">=</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token builtin">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span> self<span class="token punctuation">.</span>features <span class="token operator">=</span> features self<span class="token punctuation">.</span>classifier <span class="token operator">=</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span> nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span><span class="token number">512</span><span class="token punctuation">,</span> <span class="token number">4096</span><span class="token punctuation">)</span><span class="token punctuation">,</span> nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span>inplace<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">,</span> nn<span class="token punctuation">.</span>Dropout<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span><span class="token number">4096</span><span class="token punctuation">,</span> <span class="token number">4096</span><span class="token punctuation">)</span><span class="token punctuation">,</span> nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span>inplace<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">,</span> nn<span class="token punctuation">.</span>Dropout<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> nn<span class="token punctuation">.</span>Linear<span class="token punctuation">(</span><span class="token number">4096</span><span class="token punctuation">,</span> num_class<span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token keyword">def</span> <span class="token function">forward</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> x<span class="token punctuation">)</span><span class="token punctuation">:</span> output <span class="token operator">=</span> self<span class="token punctuation">.</span>features<span class="token punctuation">(</span>x<span class="token punctuation">)</span> output <span class="token operator">=</span> output<span class="token punctuation">.</span>view<span class="token punctuation">(</span>output<span class="token punctuation">.</span>size<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> output <span class="token operator">=</span> self<span class="token punctuation">.</span>classifier<span class="token punctuation">(</span>output<span class="token punctuation">)</span> <span class="token keyword">return</span> output<span class="token keyword">def</span> <span class="token function">make_layers</span><span class="token punctuation">(</span>cfg<span class="token punctuation">,</span> batch_norm<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">)</span><span class="token punctuation">:</span> layers <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> input_channel <span class="token operator">=</span> <span class="token number">3</span> <span class="token keyword">for</span> l <span class="token keyword">in</span> cfg<span class="token punctuation">:</span> <span class="token keyword">if</span> l <span class="token operator">==</span> <span class="token string">'M'</span><span class="token punctuation">:</span> layers <span class="token operator">+=</span> <span class="token punctuation">[</span>nn<span class="token punctuation">.</span>MaxPool2d<span class="token punctuation">(</span>kernel_size<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">,</span> stride<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">]</span> <span class="token keyword">continue</span> layers <span class="token operator">+=</span> <span class="token punctuation">[</span>nn<span class="token punctuation">.</span>Conv2d<span class="token punctuation">(</span>input_channel<span class="token punctuation">,</span> l<span class="token punctuation">,</span> kernel_size<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">,</span> padding<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">]</span> <span class="token keyword">if</span> batch_norm<span class="token punctuation">:</span> layers <span class="token operator">+=</span> <span class="token punctuation">[</span>nn<span class="token punctuation">.</span>BatchNorm2d<span class="token punctuation">(</span>l<span class="token punctuation">)</span><span class="token punctuation">]</span> layers <span class="token operator">+=</span> <span class="token punctuation">[</span>nn<span class="token punctuation">.</span>ReLU<span class="token punctuation">(</span>inplace<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">]</span> input_channel <span class="token operator">=</span> l <span class="token keyword">return</span> nn<span class="token punctuation">.</span>Sequential<span class="token punctuation">(</span><span class="token operator">*</span>layers<span class="token punctuation">)</span><span class="token keyword">def</span> <span class="token function">vgg11_bn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">return</span> VGG<span class="token punctuation">(</span>make_layers<span class="token punctuation">(</span>cfg<span class="token punctuation">[</span><span class="token string">'A'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> batch_norm<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token comment"># 调用模型并实例化</span><span class="token keyword">from</span> models<span class="token punctuation">.</span>vgg <span class="token keyword">import</span> vgg11_bnnet <span class="token operator">=</span> vgg11<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment"># 实例化模型</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><h2 id="4-定义损失函数和优化器"><a href="#4-定义损失函数和优化器" class="headerlink" title="4. 定义损失函数和优化器"></a>4. 定义损失函数和优化器</h2><p>需要定义一个<strong>损失函数</strong>来衡量模型的预测和真实值之间的差异,同时还需要一个<strong>优化器</strong>来更新模型的参数</p><pre class="line-numbers language-python" data-language="python"><code class="language-python">loss_function <span class="token operator">=</span> nn<span class="token punctuation">.</span>CrossEntropyLoss<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">#交叉熵损失函数。衡量模型预测与真实值之间的差异</span>optimizer <span class="token operator">=</span> optim<span class="token punctuation">.</span>Adam<span class="token punctuation">(</span>net<span class="token punctuation">.</span>parameters<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> lr<span class="token operator">=</span><span class="token number">0.001</span><span class="token punctuation">)</span> <span class="token comment">#使用自适应矩估计(Adam)优化器</span><span class="token comment"># 或</span>optimizer <span class="token operator">=</span> optim<span class="token punctuation">.</span>SGD<span class="token punctuation">(</span>net<span class="token punctuation">.</span>parameters<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> lr<span class="token operator">=</span>args<span class="token punctuation">.</span>lr<span class="token punctuation">,</span> momentum<span class="token operator">=</span><span class="token number">0.9</span><span class="token punctuation">,</span> weight_decay<span class="token operator">=</span><span class="token number">5e-4</span><span class="token punctuation">)</span> <span class="token comment">#使用随机梯度下降(SGD)优化器</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><p><strong>优化器优劣比较:</strong></p><p><strong>SGD(随机梯度下降)</strong><br><strong>基本思想:</strong> SGD在每一步更新中仅使用一个(或一小批)样本来计算梯度,而不是使用整个数据集。这种方法可以显著减少计算量,使得训练大规模数据集变得可行。<br><strong>学习率:</strong> SGD通常需要手动调整学习率,并且可能会使用如学习率衰减这样的技巧来帮助模型收敛。学习率的选择对SGD的性能影响很大。<br><strong>收敛速度:</strong> SGD的收敛速度通常比较慢,尤其是在接近最小值的平坦区域。<br><strong>泛化能力:</strong> 研究表明,由于SGD的噪声更大,它可能有助于模型找到泛化性能更好的解。</p><p><strong>优点:</strong>对于大数据集,可以更快地进行优化;可以有效地跳出局部最优解。</p><p><strong>缺点:</strong>1. 更新方向不稳定,可能导致优化过程震荡;2. 可能需要较长时间才能收敛;3. 对所有的参数使用相同的学习率。这可能导致优化过程在某些方向上过快,而在其他方向上过慢。</p><p><strong>Momentum(动量优化)</strong></p><p>动量优化在SGD的基础上引入了动量项,使得优化过程可以积累之前的梯度,从而更快地收敛</p><p><strong>优点:</strong>可以加速SGD在相关方向的收敛速度,抑制震荡。</p><p><strong>缺点:</strong>1. 需要人工设置动量参数;2. 可能会导致优化过程在某些方向上过快,从而跳过最优解。</p><p><strong>Adam(自适应矩估计)</strong><br><strong>基本思想:</strong> Adam是一种自适应学习率的优化算法,它结合了动量(Momentum)和RMSprop的优点。Adam会为不同的参数计算不同的自适应学习率。<br><strong>学习率:</strong> Adam自动调整学习率,通常不需要像SGD那样手动微调学习率,这使得Adam在很多情况下都能较快地收敛。<br><strong>收敛速度:</strong> 由于自适应学习率的特性,Adam在初期训练阶段通常比SGD收敛得更快。<br><strong>泛化能力:</strong> 尽管Adam在许多任务中都显示出了较快的收敛速度,但一些研究表明,对于某些问题,Adam可能导致过拟合,泛化能力不如SGD。<br><strong>优点:</strong>既可以加速优化,又可以自动调整学习率,对于深度学习模型的优化效果较好。</p><p><strong>缺点:</strong>1. 需要人工设置初始学习率和两个衰减系数;2. Adam优化器的收敛性并不是单调的,这可能会导致在训练过程中出现波动,从而影响模型的性能和稳定性;3. Adam优化器在训练初期阶段对梯度的估计存在偏差,但通过偏差纠正可以降低其影响,然而这种偏差纠正可能会带来额外的计算负担。</p><h2 id="5-模型训练"><a href="#5-模型训练" class="headerlink" title="5. 模型训练"></a>5. 模型训练</h2><p>模型训练包括:前向传播、计算损失、反向传播、参数更新。</p><pre class="line-numbers language-python" data-language="python"><code class="language-python">net<span class="token punctuation">.</span>train<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">#设置神经网络为训练模式</span><span class="token keyword">for</span> batch_index<span class="token punctuation">,</span> <span class="token punctuation">(</span>images<span class="token punctuation">,</span> labels<span class="token punctuation">)</span> <span class="token keyword">in</span> <span class="token builtin">enumerate</span><span class="token punctuation">(</span>cifar100_training_loader<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token comment">#遍历训练数据加载器</span> optimizer<span class="token punctuation">.</span>zero_grad<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">#将优化器中所有参数的梯度清零,以便进行新一轮的梯度计算</span> outputs <span class="token operator">=</span> net<span class="token punctuation">(</span>images<span class="token punctuation">)</span> <span class="token comment">#将输入图像传递给神经网络进行前向传播</span> loss <span class="token operator">=</span> loss_function<span class="token punctuation">(</span>outputs<span class="token punctuation">,</span> labels<span class="token punctuation">)</span> <span class="token comment">#计算输出和标签之间的损失</span> loss<span class="token punctuation">.</span>backward<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">#进行反向传播,计算损失对网络参数的梯度</span> optimizer<span class="token punctuation">.</span>step<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">#根据计算的梯度更新网络参数</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="6-模型评估"><a href="#6-模型评估" class="headerlink" title="6. 模型评估"></a>6. 模型评估</h2><p>在测试集上评估模型的性能,看它在未见过的数据集上的性能表现如何</p><pre class="line-numbers language-python" data-language="python"><code class="language-python">net<span class="token punctuation">.</span><span class="token builtin">eval</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">#将神经网络设置为评估模式,禁用 dropout 和 batch normalization 等训练相关的操作</span>test_loss <span class="token operator">=</span> <span class="token number">0.0</span> <span class="token comment"># cost function error</span>correct <span class="token operator">=</span> <span class="token number">0.0</span><span class="token keyword">for</span> <span class="token punctuation">(</span>images<span class="token punctuation">,</span> labels<span class="token punctuation">)</span> <span class="token keyword">in</span> cifar100_test_loader<span class="token punctuation">:</span> <span class="token keyword">if</span> args<span class="token punctuation">.</span>gpu<span class="token punctuation">:</span> images <span class="token operator">=</span> images<span class="token punctuation">.</span>cuda<span class="token punctuation">(</span><span class="token punctuation">)</span> labels <span class="token operator">=</span> labels<span class="token punctuation">.</span>cuda<span class="token punctuation">(</span><span class="token punctuation">)</span> outputs <span class="token operator">=</span> net<span class="token punctuation">(</span>images<span class="token punctuation">)</span> loss <span class="token operator">=</span> loss_function<span class="token punctuation">(</span>outputs<span class="token punctuation">,</span> labels<span class="token punctuation">)</span> test_loss <span class="token operator">+=</span> loss<span class="token punctuation">.</span>item<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">#累加损失值</span> _<span class="token punctuation">,</span> preds <span class="token operator">=</span> outputs<span class="token punctuation">.</span><span class="token builtin">max</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token comment">#获取输出中每行最大值的索引,即预测的类别</span> correct <span class="token operator">+=</span> preds<span class="token punctuation">.</span>eq<span class="token punctuation">(</span>labels<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token builtin">sum</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">#将预测正确的样本数累加</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="7-保存模型"><a href="#7-保存模型" class="headerlink" title="7. 保存模型"></a>7. 保存模型</h2><p>将训练好的模型保存下来,方便后续使用或部署</p><pre class="line-numbers language-python" data-language="python"><code class="language-python"><span class="token comment">#start to save best performance model after learning rate decay to 0.01</span><span class="token keyword">if</span> epoch <span class="token operator">></span> settings<span class="token punctuation">.</span>MILESTONES<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token keyword">and</span> best_acc <span class="token operator"><</span> acc<span class="token punctuation">:</span> weights_path <span class="token operator">=</span> checkpoint_path<span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>net<span class="token operator">=</span>args<span class="token punctuation">.</span>net<span class="token punctuation">,</span> epoch<span class="token operator">=</span>epoch<span class="token punctuation">,</span> <span class="token builtin">type</span><span class="token operator">=</span><span class="token string">'best'</span><span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'saving weights file to {}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>weights_path<span class="token punctuation">)</span><span class="token punctuation">)</span> torch<span class="token punctuation">.</span>save<span class="token punctuation">(</span>net<span class="token punctuation">.</span>state_dict<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> weights_path<span class="token punctuation">)</span> best_acc <span class="token operator">=</span> acc <span class="token keyword">continue</span><span class="token comment">#定期保存模型。当前epoch是SAVE_EPOCH的倍数</span><span class="token keyword">if</span> <span class="token keyword">not</span> epoch <span class="token operator">%</span> settings<span class="token punctuation">.</span>SAVE_EPOCH<span class="token punctuation">:</span> weights_path <span class="token operator">=</span> checkpoint_path<span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>net<span class="token operator">=</span>args<span class="token punctuation">.</span>net<span class="token punctuation">,</span> epoch<span class="token operator">=</span>epoch<span class="token punctuation">,</span> <span class="token builtin">type</span><span class="token operator">=</span><span class="token string">'regular'</span><span class="token punctuation">)</span> <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'saving weights file to {}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>weights_path<span class="token punctuation">)</span><span class="token punctuation">)</span> torch<span class="token punctuation">.</span>save<span class="token punctuation">(</span>net<span class="token punctuation">.</span>state_dict<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> weights_path<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="8-加载模型"><a href="#8-加载模型" class="headerlink" title="8. 加载模型"></a>8. 加载模型</h2><p>在需要时,可以随时加载已保存的模型权重继续使用</p><pre class="line-numbers language-python" data-language="python"><code class="language-python">net <span class="token operator">=</span> vgg11<span class="token punctuation">(</span><span class="token punctuation">)</span>net<span class="token punctuation">.</span>load_state_dict<span class="token punctuation">(</span>torch<span class="token punctuation">.</span>load<span class="token punctuation">(</span><span class="token string">'model.pth'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>net<span class="token punctuation">.</span><span class="token builtin">eval</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>]]></content>
<categories>
<category> 深度学习 </category>
</categories>
<tags>
<tag> 深度学习基础 </tag>
<tag> 模型训练 </tag>
</tags>
</entry>
<entry>
<title>Notion常用快捷键</title>
<link href="/2024/06/02/notion-chang-yong-kuai-jie-jian/"/>
<url>/2024/06/02/notion-chang-yong-kuai-jie-jian/</url>
<content type="html"><![CDATA[<h2 id="常用Notion快捷键-amp-命令"><a href="#常用Notion快捷键-amp-命令" class="headerlink" title="常用Notion快捷键&命令"></a><strong>常用Notion快捷键&命令</strong></h2><p>以下是Notion中最常用的一些快捷键和命令,建议熟练掌握:</p><table><thead><tr><th>Windows快捷键</th><th>功能</th><th>Mac快捷键</th></tr></thead><tbody><tr><td>ctrl + N</td><td>新建页面</td><td>⌘ + N</td></tr><tr><td>ctrl + shift + U</td><td>跳转到导航条的第一个页面</td><td>⌘ + shift + U</td></tr><tr><td>shift + enter</td><td>表格插入新行(非编辑模式下才有效)</td><td>shift + enter</td></tr><tr><td>ctrl + /</td><td>改变内容块(Block)的Type类型</td><td>⌘ + /</td></tr><tr><td>/into[type]</td><td>例子:在block的右侧输入/intopage,可将内容块的类型改为Page</td><td>/into[type]</td></tr><tr><td>ctrl + \</td><td>收起或展开侧边栏</td><td>⌘ + \</td></tr><tr><td>ctrl + alt + t</td><td>折叠或展开笔记内容</td><td>⌘ + alt + t</td></tr><tr><td>ctrl + d</td><td>复制一个内容块Block</td><td>⌘ + d</td></tr><tr><td>ctrl + L</td><td>复制当前页面的链接</td><td>⌘ + L</td></tr><tr><td>ctrl + 鼠标左键ctrl + shift + N</td><td>在新窗口中打开笔记页</td><td>⌘ + 鼠标左键⌘ + shift + N</td></tr><tr><td>ctrl + R</td><td>页面卡住了?试一下刷新当前页面</td><td>⌘ + R</td></tr><tr><td>ctrl + alt + 9</td><td>把notionblock转换成page页面</td><td>⌘ + alt + 9</td></tr><tr><td>ctrl + shift + r</td><td>对Page页面链接重新命名</td><td>⌘ + shift + r</td></tr><tr><td>ctrl + P</td><td>查看最近浏览过的页面</td><td>⌘ + p</td></tr><tr><td>ctrl + [</td><td>回到上一个浏览的页面</td><td>⌘ + [</td></tr><tr><td>ctrl + ]</td><td>前进一个页面</td><td>⌘ + ]</td></tr><tr><td>ctrl + shift + L</td><td>切换黑色背景</td><td>⌘ + shift + L</td></tr><tr><td>windows键 + .windows键 + ;</td><td>插入emoji表情</td><td>control + ⌘+ space</td></tr></tbody></table><h2 id="文字编辑"><a href="#文字编辑" class="headerlink" title="文字编辑"></a><strong>文字编辑</strong></h2><table><thead><tr><th>Windows快捷键</th><th>功能</th><th>Mac快捷键</th></tr></thead><tbody><tr><td>ctrl + B</td><td>选中文字加粗</td><td>⌘ + B</td></tr><tr><td>ctrl + I</td><td>选中文字变斜体</td><td>⌘ + I</td></tr><tr><td>ctrl + U</td><td>选中文字加下划线</td><td>⌘ + U</td></tr><tr><td>ctrl + shift + S</td><td>选中文字加删除线</td><td>⌘ + shift + S</td></tr><tr><td>ctrl + K</td><td>选中文字添加链接</td><td>⌘ + K</td></tr><tr><td>ctrl + V</td><td>选中文字粘贴超链接</td><td>⌘ + V</td></tr><tr><td>ctrl + E</td><td>选中文字变成代码格式</td><td>⌘ + E</td></tr><tr><td>tab</td><td>为选中内容/block添加缩进</td><td>tab</td></tr><tr><td>shift + tab</td><td>为选中内容/block取消缩进</td><td>shift + tab</td></tr><tr><td>/turn</td><td>在block的开头或结尾插入。改变block的类型。</td><td>/turn</td></tr><tr><td>/color/color background</td><td>在block的开头或结尾插入。改变block的字体或背景颜色。</td><td>/color/color background</td></tr><tr><td>/default</td><td>在block的开头或结尾插入。移除字体或背景颜色。</td><td>/default</td></tr></tbody></table><h2 id="新建内容"><a href="#新建内容" class="headerlink" title="新建内容"></a><strong>新建内容</strong></h2><table><thead><tr><th>Windows快捷键</th><th>功能</th><th>Mac快捷键</th></tr></thead><tbody><tr><td>enter</td><td>在block内插入新行</td><td>enter</td></tr><tr><td>shift + enter</td><td>插入一个新的文字block</td><td>shift + enter</td></tr><tr><td>ctrl + shift + M</td><td>新建一个评论</td><td>⌘ + shift + M</td></tr><tr><td>—</td><td>在新行输入—可创建分隔符</td><td>—</td></tr><tr><td>ctrl + shift + 0</td><td>新建文字内容</td><td>⌘ + option + 0</td></tr><tr><td>ctrl + shift + 1</td><td>新建H1标题</td><td>⌘ + option + 1</td></tr><tr><td>ctrl + shift + 2</td><td>新建H2标题</td><td>⌘ + option + 2</td></tr><tr><td>ctrl + shift + 3</td><td>新建H3标题</td><td>⌘ + option + 3</td></tr><tr><td>ctrl + shift + 4</td><td>新建勾选框</td><td>⌘ + option + 4</td></tr><tr><td>ctrl + shift + 5</td><td>新建无序列表</td><td>⌘ + option + 5</td></tr><tr><td>ctrl + shift + 6</td><td>新建有序列表</td><td>⌘ + option + 6</td></tr><tr><td>ctrl + shift + 7</td><td>新建折叠列表(Toggle List)</td><td>⌘ + option + 7</td></tr><tr><td>ctrl + shift + 8</td><td>新建代码块</td><td>⌘ + option + 8</td></tr><tr><td>ctrl + shift + 9</td><td>新建页面,或将当前行内容转换为页面</td><td>⌘ + option + 9</td></tr><tr><td>ctrl + +</td><td>放大</td><td>⌘ + +</td></tr><tr><td>ctrl + –</td><td>缩小</td><td>⌘ + –</td></tr><tr><td>ctrl + shift + U</td><td>返回导航条上一级页面</td><td>⌘ + shift + U</td></tr><tr><td>alt</td><td>按住并拖放,克隆页面中的任意内容</td><td>option</td></tr></tbody></table><h2 id="移动或编辑内容块(Block)"><a href="#移动或编辑内容块(Block)" class="headerlink" title="移动或编辑内容块(Block)"></a><strong>移动或编辑内容块(Block)</strong></h2><table><thead><tr><th>Windows快捷键</th><th>功能</th><th>Mac快捷键</th></tr></thead><tbody><tr><td>esc</td><td>选中当前正在编辑的Block再按一次取消选中</td><td>esc</td></tr><tr><td>ctrl + a</td><td>全选当前Block的内容</td><td>⌘ + a</td></tr><tr><td>space</td><td>打开当前选中的图片</td><td>space</td></tr><tr><td>方向键</td><td>选择不同的block</td><td>方向键</td></tr><tr><td>shift + ↑/↓</td><td>选中光标前方或后方内容</td><td>shift + ↑/↓</td></tr><tr><td>alt + shift + click</td><td>同时选中多个block</td><td>⌘ + shift + click</td></tr><tr><td>shift + click</td><td>选中另一个block,并将它与当前block之间的block全部选中</td><td>shift + click</td></tr><tr><td>Backspace/Delete</td><td>删除选中的block</td><td>Backspace/Delete</td></tr><tr><td>ctrl + D</td><td>复制当前或选中的block</td><td>⌘ + D</td></tr><tr><td>ctrl + /</td><td>修改block样式,输入color、turn into可以修改block的颜色和类型</td><td>⌘ + /</td></tr><tr><td>ctrl + shift + 方向键</td><td>移动block的位置</td><td>⌘ + shift + 方向键</td></tr><tr><td>ctrl + alt + T</td><td>折叠或展开折叠块(Toggle List)</td><td>⌘ + option + T</td></tr><tr><td>ctrl + shift + H</td><td>高亮选中文字</td><td>⌘ + shift + H</td></tr><tr><td>ctrl + enter</td><td>多功能热键:打开一个页面链接勾选/取消勾选展开/折叠当前折叠块全屏显示图片</td><td>⌘ + enter</td></tr></tbody></table><h2 id="“-”命令:提示和提醒"><a href="#“-”命令:提示和提醒" class="headerlink" title="“@”命令:提示和提醒"></a><strong>“@”命令:提示和提醒</strong></h2><table><thead><tr><th>命令</th><th>功能</th></tr></thead><tbody><tr><td>@[member’s name]</td><td>输入@和团队成员的名字,可以提醒或通知该成员</td></tr><tr><td>@[page’s name]</td><td>输入@和页面标题,可以插入该页面的链接</td></tr><tr><td>@[date]</td><td>输入@和任意格式的日期,或’today’/’yesterday’/’tomorrow’等,在输入截止日时很有用</td></tr><tr><td>@remind[date]</td><td>输入@remind和任意格式时间,你可以在该指定时间收到notion的通知</td></tr></tbody></table><p><strong>注:如果你只想打出@这个符号,按esc可以取消@菜单</strong></p><h2 id="“-“命令:页面链接"><a href="#“-“命令:页面链接" class="headerlink" title="“[[“命令:页面链接"></a><strong>“[[“命令:页面链接</strong></h2><table><thead><tr><th>命令</th><th>功能</th></tr></thead><tbody><tr><td>[[</td><td>输入[[和另一个页面的标题,可以插入该页面的链接</td></tr><tr><td>[[</td><td>输入[[和你想要的任意名字,配合方向键点击+ Add new sub-page 可以在当前页面新建一个sub-page</td></tr><tr><td>[[</td><td>输入[[和你想要的任意名字,配合方向键点击↗︎ Add new page in…可以在其他页面或database新建一个sub-page</td></tr></tbody></table><h2 id="“-”命令:新建页面"><a href="#“-”命令:新建页面" class="headerlink" title="“+”命令:新建页面"></a><strong>“+”命令:新建页面</strong></h2><table><thead><tr><th>命令</th><th>功能</th></tr></thead><tbody><tr><td>+</td><td>输入+和任意名称,配合方向键点击+ Add new sub-page 可以在当前页面新建一个sub-page</td></tr><tr><td>+</td><td>输入+和任意名称,配合方向键点击↗︎ Add new page in…可以在其他页面或database新建一个sub-page</td></tr><tr><td>+</td><td>输入+和另一个页面的标题,可以插入该页面的链接</td></tr></tbody></table><p><strong>注意:[[命令侧重于插入页面链接功能,+命令侧重新建页面功能。</strong></p><h2 id="“-x2F-“命令"><a href="#“-x2F-“命令" class="headerlink" title="“/“命令"></a><strong>“/“命令</strong></h2><h3 id="基础”-x2F-“命令"><a href="#基础”-x2F-“命令" class="headerlink" title="基础”/“命令"></a><strong>基础”/“命令</strong></h3><table><thead><tr><th>命令</th><th>功能</th></tr></thead><tbody><tr><td>/</td><td>输入/命令会弹出Block菜单,配合方向键可新建任意类型的内容块</td></tr><tr><td>/text 或 /plain</td><td>新建文字内容块text block</td></tr><tr><td>/page</td><td>新建页面page(回车后自动打开该页面)</td></tr><tr><td>/bullet</td><td>新建无序列表</td></tr><tr><td>/num</td><td>新建有序列表</td></tr><tr><td>/todo</td><td>新建待办事项和复选框</td></tr><tr><td>/toggle</td><td>新建折叠内容块(可折叠/展开)</td></tr><tr><td>/div</td><td>新建分隔符</td></tr><tr><td>/quote</td><td>新建引用内容块</td></tr><tr><td>/h1 或 /#</td><td>新建h1标题</td></tr><tr><td>/h2 或 /##</td><td>新建h2标题</td></tr><tr><td>/h3 或 /###</td><td>新建h3标题</td></tr><tr><td>/link</td><td>新建页面链接page link</td></tr><tr><td>esc</td><td>如果只想输入/,可以按esc退出菜单</td></tr></tbody></table><h3 id="行内”-x2F-“命令"><a href="#行内”-x2F-“命令" class="headerlink" title="行内”/“命令"></a><strong>行内”/“命令</strong></h3><table><thead><tr><th>命令</th><th>功能</th></tr></thead><tbody><tr><td>/mention</td><td>提示团队中的某位成员,或者提示某个页面</td></tr><tr><td>/date 或 /reminder</td><td>添加时间戳或时间提示</td></tr><tr><td>/equation</td><td>添加TeX格式的公式</td></tr><tr><td>/emoji</td><td>添加emoji表情</td></tr></tbody></table><h3 id="数据视图”-x2F-“命令"><a href="#数据视图”-x2F-“命令" class="headerlink" title="数据视图”/“命令"></a><strong>数据视图”/“命令</strong></h3><table><thead><tr><th>命令</th><th>功能</th></tr></thead><tbody><tr><td>/table-inline</td><td>新建行内普通表格</td></tr><tr><td>/database-inline</td><td>新建行内Database数据库表格</td></tr><tr><td>/board-inline</td><td>新建行内卡片(board)</td></tr><tr><td>/calendar-inline</td><td>新建行内日历</td></tr><tr><td>/list-inline</td><td>新建行内页面列表(list)</td></tr><tr><td>/gallery-inline</td><td>新建行内画廊数据表(gallery)</td></tr><tr><td>/timeline-inline</td><td>新建时间线数据表</td></tr><tr><td>/linked</td><td>在当前页面引用database数据表</td></tr></tbody></table><p><strong>注:以上命令中的-inline都可以替换为-full,替换后新建的对象都以Page的形式打开。如: /table-full, /board-full</strong></p><h3 id="媒体”-x2F-“命令"><a href="#媒体”-x2F-“命令" class="headerlink" title="媒体”/“命令"></a><strong>媒体”/“命令</strong></h3><table><thead><tr><th>命令</th><th>功能</th></tr></thead><tbody><tr><td>/image</td><td>插入图片</td></tr><tr><td>/pdf</td><td>插入pdf</td></tr><tr><td>/book</td><td>插入网页链接并保存为书签</td></tr><tr><td>/video</td><td>插入视频</td></tr><tr><td>/audio</td><td>插入音频</td></tr><tr><td>/code</td><td>插入代码块</td></tr><tr><td>/file</td><td>插入文档</td></tr><tr><td>/embed</td><td>插入各种外部文件(500+种格式可选)</td></tr></tbody></table><h3 id="高级”-x2F-“命令"><a href="#高级”-x2F-“命令" class="headerlink" title="高级”/“命令"></a><strong>高级”/“命令</strong></h3><table><thead><tr><th>命令</th><th>功能</th></tr></thead><tbody><tr><td>/comment</td><td>在当前block插入评论</td></tr><tr><td>/duplicate</td><td>复制当前block</td></tr><tr><td>/moveto</td><td>将当前block移动到其他页面</td></tr><tr><td>/delete</td><td>删除当前块</td></tr><tr><td>/toc</td><td>插入目录</td></tr><tr><td>/button 或 /template</td><td>插入按钮</td></tr><tr><td>/bread</td><td>插入面包屑导航条</td></tr><tr><td>/math 或 /latex</td><td>插入数学公式</td></tr></tbody></table><h2 id="Markdown-语法"><a href="#Markdown-语法" class="headerlink" title="Markdown 语法"></a><strong>Markdown 语法</strong></h2><h3 id="使用Markdown编辑文本格式"><a href="#使用Markdown编辑文本格式" class="headerlink" title="使用Markdown编辑文本格式"></a><strong>使用Markdown编辑文本格式</strong></h3><table><thead><tr><th>命令</th><th>功能</th></tr></thead><tbody><tr><td>** 内容 **</td><td>加粗,只适用于Text内容块</td></tr><tr><td>*内容* 或 _内容_</td><td>斜体,只适用于Text内容块</td></tr><tr><td>`内容`</td><td>把内容转换为高亮代码格式,只适用于Text内容块</td></tr><tr><td>~内容~</td><td>给内容增加删除线,只适用于Text内容块</td></tr></tbody></table><h3 id="使用Markdown插入新内容块"><a href="#使用Markdown插入新内容块" class="headerlink" title="使用Markdown插入新内容块"></a><strong>使用Markdown插入新内容块</strong></h3><table><thead><tr><th>命令</th><th>功能</th></tr></thead><tbody><tr><td>– + space</td><td>插入无序列表</td></tr><tr><td>[] + space</td><td>插入复选框</td></tr><tr><td>1. 或 a. 或 i. + space</td><td>插入有序列表</td></tr><tr><td># + space</td><td>插入h1标题</td></tr><tr><td>## + space</td><td>插入h2标题</td></tr><tr><td>### + space</td><td>插入h3标题</td></tr><tr><td>> + space</td><td>插入折叠内容块(Toggle)</td></tr><tr><td>” + space</td><td>插入引用内容块(Quote)</td></tr></tbody></table>]]></content>
<categories>
<category> 工具 </category>
</categories>
<tags>
<tag> 快捷键 </tag>
<tag> 效率提升 </tag>
<tag> Notion </tag>
</tags>
</entry>
<entry>
<title>Ubuntu22.04安装OpenFOAM_v2212</title>
<link href="/2024/05/31/ubuntu22-04-an-zhuang-openfoam-v2212/"/>
<url>/2024/05/31/ubuntu22-04-an-zhuang-openfoam-v2212/</url>
<content type="html"><![CDATA[<h2 id="0、环境准备"><a href="#0、环境准备" class="headerlink" title="0、环境准备"></a>0、环境准备</h2><p>该并行应用程序需要使用 <code>openmpi</code>,因此需要事先安装<code>openmpi</code>。MPI 的安装方式可以查看之前的<a href="https://antoniozc666.github.io/2024/04/02/nemo-hai-yang-mo-xing-de-an-zhuang-da-jian-yu-yun-xing/">博客</a>。</p><p>需要实现在系统上安装以下依赖项:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> build-essential autoconf autotools-dev cmake <span class="token function">gawk</span> gnuplot<span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> flex libfl-dev libreadline-dev zlib1g-dev openmpi-bin libopenmpi-dev mpi-default-bin mpi-default-dev<span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> libgmp-dev libmpfr-dev libmpc-dev<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><p>要检查可用的系统版本,请使用 <code>apt-cache show</code> 命令。例如:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">apt-cache</span> show libopenmpi-dev<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>如果要使用系统组件,还需要安装以下组件:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">apt-get</span> <span class="token function">install</span> libfftw3-dev libscotch-dev libptscotch-dev libboost-system-dev libboost-thread-dev libcgal-dev<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>其他的软件要求如下:</p><p><img src="/2024/05/31/ubuntu22-04-an-zhuang-openfoam-v2212/openfoam%E7%B3%BB%E7%BB%9F%E8%A6%81%E6%B1%82.png"></p><h2 id="一、安装步骤"><a href="#一、安装步骤" class="headerlink" title="一、安装步骤"></a>一、安装步骤</h2><p>OpenFOAM有两种版本:一个是商业版本,版本编号为从1开始的数字(如:OpenFOAM v11);一个公益开源版本,版本编号为年份加月份(如:OpenFOAM v2212)。</p><h3 id="1、下载源码"><a href="#1、下载源码" class="headerlink" title="1、下载源码"></a>1、下载源码</h3><p>从<a href="https://dl.openfoam.com/source/v2212/">网站</a>上下载<code>OpenFOAM v2212</code>和<code>ThirdParty v2212</code>源代码,并将其上传到服务器。我存放的路径为:<code>/home/czhang/openfoam</code>。进入该存放路径,解压缩两个源代码压缩包。</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">tar</span> -zxvf OpenFOAM-v2212.tgz<span class="token function">tar</span> -zxvf ThirdParty-v2212.tgz<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><h3 id="2、设置环境变量"><a href="#2、设置环境变量" class="headerlink" title="2、设置环境变量"></a>2、设置环境变量</h3><p>在构建之前,要先满足系统要求,并获取正确的 OpenFOAM 环境。例如,对于 OpenFOAM-v2212 版本:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token builtin class-name">source</span> <span class="token operator"><</span>installation path<span class="token operator">></span>/OpenFOAM-v2212/etc/bashrc<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>在我这里,我安装在 <code>~/openfoam</code> 目录下,所以我这里是:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token builtin class-name">source</span> ~/openfoam/OpenFOAM-v2212/etc/bashrc<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p><strong>建议:</strong>将这句命令添加到用户根目录下的 <code>.bashrc/.zshrc</code>文件中</p><h3 id="3、编译OpenFOAM"><a href="#3、编译OpenFOAM" class="headerlink" title="3、编译OpenFOAM"></a>3、编译OpenFOAM</h3><p>进入OpenFOAM所在的目录进行操作:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token builtin class-name">cd</span> /home/czhang/openfoam/OpenFOAM-v2212<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>测试系统就绪情况:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">foamSystemCheck<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>新建一个shell命令窗口,切换到 OpenFOAM 主目录 ($WM_PROJECT_DIR)。如果此操作失败,则环境配置不正确。</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">foam<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>编译openfoam:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">./Allwmake -j -s -q -l<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>该命令使用所有内核 (-j),缩减输出 (-s, -silent)、队列 (-q, -queue) ,将编译信息一起输出到日志文件 (-l, -log) <code>log.linux64GccDPInt32Opt</code> 供以后检查。</p><h3 id="4、编译后检验"><a href="#4、编译后检验" class="headerlink" title="4、编译后检验"></a>4、编译后检验</h3><p>打开一个新的 shell 窗口并获取 OpenFOAM 环境。</p><p>通过运行以下命令来验证生成:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">foamInstallationTest<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>如果报错显示 <code>icoFoam</code>没有找到,则使用<code>Allwmake</code>去掉参数 <code>-q</code>重新编译。</p><p>之后测试任何给定的教程案例,例如:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">foamTestTutorial -full incompressible/simpleFoam/pitzDaily<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p><img src="/2024/05/31/ubuntu22-04-an-zhuang-openfoam-v2212/%E6%B5%8B%E8%AF%95%E6%95%99%E7%A8%8B%E6%A1%88%E4%BE%8B.png"></p><p>手动测试教程案例:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># Create the user "run" directory:</span><span class="token function">mkdir</span> -p <span class="token string">"<span class="token variable">$FOAM_RUN</span>"</span><span class="token comment"># Change to the user "run" directory:</span>run<span class="token comment"># Copy tutorial</span><span class="token function">cp</span> -r <span class="token string">"<span class="token variable">$FOAM_TUTORIALS</span>"</span>/incompressible/simpleFoam/pitzDaily ./<span class="token comment"># Run the tutorial</span><span class="token builtin class-name">cd</span> pitzDaily <span class="token operator">&&</span> blockMesh <span class="token operator">&&</span> simpleFoam<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><img src="/2024/05/31/ubuntu22-04-an-zhuang-openfoam-v2212/%E6%89%8B%E5%8A%A8%E6%B5%8B%E8%AF%95%E7%94%A8%E4%BE%8B%E9%83%A8%E5%88%86%E7%BB%93%E6%9E%9C%E5%B1%95%E7%A4%BA.png"></p><h2 id="二、安装第三方部分"><a href="#二、安装第三方部分" class="headerlink" title="二、安装第三方部分"></a>二、安装第三方部分</h2><h3 id="1、环境准备"><a href="#1、环境准备" class="headerlink" title="1、环境准备"></a>1、环境准备</h3><p>将第三方部分所需的依赖项引入系统的一种快速方法是使用 paraview 开发包:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">sudo</span> <span class="token function">apt</span> <span class="token function">install</span> paraview-dev<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>这是一个软件包,可以寻求系统管理员安装。</p><p>同时也要安装以下依赖:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">sudo</span> <span class="token function">apt</span> <span class="token function">install</span> cmake qtbase5-dev qttools5-dev qttools5-dev-tools libqt5opengl5-dev libqt5x11extras5-dev libxt-dev<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>可以用之前的 <code>apt-cache show</code>命令查看系统是否拥有。</p><h3 id="2、构建第三方部分"><a href="#2、构建第三方部分" class="headerlink" title="2、构建第三方部分"></a>2、构建第三方部分</h3><p>切换目录到第三方部分的目录:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token builtin class-name">cd</span> /home/czhang/openfoam/ThirdParty-v2212<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>运行命令:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">./Allwmake -j -s -q -l<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>期间可能会报错显示部分库找不到,如:gmp等,可以安装相应的依赖库:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> libmpfr-dev, libgmp-dev, libboost-all-dev<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h2 id="三、测试安装结果"><a href="#三、测试安装结果" class="headerlink" title="三、测试安装结果"></a>三、测试安装结果</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">foamVersion<span class="token comment"># icoFoam</span>blockMeshsimpleFoam<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><p>若输出结果均无报错则安装成功,在运行求解器(如icoFoam,simpleFoam等)之前需要先用blockMesh划分网格。</p><h2 id="四、案例测试"><a href="#四、案例测试" class="headerlink" title="四、案例测试"></a>四、案例测试</h2><p>运行命令 <code>run</code>跳转到测试运行目录,运行命令:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">cp</span> -r <span class="token string">"<span class="token variable">$FOAM_TUTORIALS</span>"</span>/incompressible/icoFoam/cavity ./<span class="token builtin class-name">cd</span> cavity<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>将tutorials中的cavity测试案例复制到run目录下,并进入cavity文件夹中。</p><p>在system文件夹中有一些运行控制文件,可通过修改这些文件中的值来调整运行参数</p><p><img src="/2024/05/31/ubuntu22-04-an-zhuang-openfoam-v2212/system%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84.png"></p><p>运行命令:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">blockMeshicoFoam<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>在运行求解器之前需要运行blockMesh划分网格。</p><p>如果要使用mpi进行并行运行,则需要先使用OpenFOAM的并行工具来分割计算域</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">decomposePar<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>该命令会自动创建像<code>processor0</code>这样的目录,分解了多少个计算域,就会产生多少个目录,之后要使用多少个线程运行。目录如图所示:</p><p><img src="/2024/05/31/ubuntu22-04-an-zhuang-openfoam-v2212/decomposePar%E7%BB%93%E6%9E%9C.png"></p><p>划分网格、分解计算域后,使用mpi并行运行分解器,要是用哪个分解器可在 <code>controlDict</code>中查看。</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">mpirun -np <span class="token number">9</span> icoFoam -parallel <span class="token operator">></span> icoFoamRun.log<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p><strong>Reference:</strong></p><p><a href="https://www.bilibili.com/list/watchlater?oid=207358298&bvid=BV1Vh411i7to&spm_id_from=333.1007.top_right_bar_window_view_later.content.click">OpenFOAM系列教程</a></p><p><a href="https://develop.openfoam.com/Development/openfoam/-/blob/master/doc/Build.md">OpenFOAM_v2212构建指南</a></p><p><a href="https://develop.openfoam.com/Development/ThirdParty-common/blob/develop/BUILD.md">第三方构建指南</a></p><p><a href="https://zhuanlan.zhihu.com/p/541266216">OpenFOAM安装教程1</a></p><p><a href="https://blog.csdn.net/qq_45682183/article/details/124945151">OpenFOAM安装教程2</a></p><p><a href="https://blog.csdn.net/AudrianaZhang/article/details/127043574">OpenFOAM安装教程3</a></p><p><a href="https://www.cfd-online.com/Forums/openfoam-installation/229950-icofoam-not-installed.html">icofoam未安装问题解决</a></p>]]></content>
<categories>
<category> 工作负载 </category>
</categories>
<tags>
<tag> 工作负载 </tag>
<tag> 环境搭建 </tag>
<tag> HPC </tag>
<tag> OpenFOAM </tag>
</tags>
</entry>
<entry>
<title>解决远程连接时VSCode与独立终端环境不一致问题</title>
<link href="/2024/05/30/jie-jue-yuan-cheng-lian-jie-shi-vscode-yu-du-li-zhong-duan-huan-jing-bu-yi-zhi-wen-ti/"/>
<url>/2024/05/30/jie-jue-yuan-cheng-lian-jie-shi-vscode-yu-du-li-zhong-duan-huan-jing-bu-yi-zhi-wen-ti/</url>
<content type="html"><![CDATA[<h2 id="一、问题描述"><a href="#一、问题描述" class="headerlink" title="一、问题描述"></a>一、问题描述</h2><p>本人平常使用vscode作为开发工具远程连接服务器。但是有时在 <code>.bashrc</code> 和 <code>.zshrc</code> 修改环境变量并<code>source</code>后,查看<code>PATH</code>变量发现还是有原来的变量设置,哪怕关掉终端另开新终端,或重启vscode也没有用。而使用Windows Terminal连接服务器则能显示更新后的环境变量。</p><p><img src="/2024/05/30/jie-jue-yuan-cheng-lian-jie-shi-vscode-yu-du-li-zhong-duan-huan-jing-bu-yi-zhi-wen-ti/vscode%E7%8E%AF%E5%A2%83.png"></p><p>![](独立终端环境 .png)</p><p>实际上这是由于<strong>vscode在远程服务器上有缓存</strong>,同时在关掉vscode后远程服务器上的 <strong><code>.vscode-server</code> 进程并没有停止</strong>成为了孤立进程。</p><h2 id="二、解决方法"><a href="#二、解决方法" class="headerlink" title="二、解决方法"></a>二、解决方法</h2><h3 id="1-查找vscode服务器进程"><a href="#1-查找vscode服务器进程" class="headerlink" title="1. 查找vscode服务器进程"></a>1. 查找vscode服务器进程</h3><p>使用 <code>ps</code> 命令列出进程并筛选与 VSCode 服务器相关的进程。通常,服务器进程的名称中将包含 <code>node</code> 或 <code>vscode-server</code> 。</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">ps</span> aux <span class="token operator">|</span> <span class="token function">grep</span> czhang/.vscode-server<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h3 id="2-终止vscode服务器进程"><a href="#2-终止vscode服务器进程" class="headerlink" title="2. 终止vscode服务器进程"></a>2. 终止vscode服务器进程</h3><p>使用该 <code>kill</code> 命令终止进程。使用从上一步获取的进程 ID (PID)。</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">kill</span> -9 <span class="token operator"><</span>PID<span class="token operator"><span class="token file-descriptor important">1</span>></span> <span class="token operator"><</span>PID<span class="token operator"><span class="token file-descriptor important">2</span>></span> <span class="token punctuation">..</span>.<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h3 id="3-删除vscode服务器目录"><a href="#3-删除vscode服务器目录" class="headerlink" title="3. 删除vscode服务器目录"></a>3. 删除vscode服务器目录</h3><p>VSCode 服务器目录通常位于主目录: <code>~/.vscode-server</code> 。使用 <code>rm</code> 命令删除 VSCode 服务器目录。这将强制 VSCode 在下次连接时重新安装服务器。</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">rm</span> -rf ~/.vscode-server<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h3 id="4-重启vscode,重新连接到服务器"><a href="#4-重启vscode,重新连接到服务器" class="headerlink" title="4. 重启vscode,重新连接到服务器"></a>4. 重启vscode,重新连接到服务器</h3>]]></content>
<categories>
<category> 工具 </category>
</categories>
<tags>
<tag> IDE </tag>
<tag> vscode </tag>
<tag> SSH </tag>
<tag> debug </tag>
</tags>
</entry>
<entry>
<title>Linux环境下使用TPC-H测试MySQL教程</title>
<link href="/2024/05/19/linux-huan-jing-xia-shi-yong-tpc-h-ce-shi-mysql-jiao-cheng/"/>
<url>/2024/05/19/linux-huan-jing-xia-shi-yong-tpc-h-ce-shi-mysql-jiao-cheng/</url>
<content type="html"><![CDATA[<h2 id="TPC-H介绍"><a href="#TPC-H介绍" class="headerlink" title="TPC-H介绍"></a>TPC-H介绍</h2><p>TPC-H 是 TPC(Transaction Processing Performance Council)组织发布的一套数据库测试规范,同样的还有很多其他的规范,例如Hammer DB、BenchMarkSQL、Sysbench、YCSB等等一样,都是提供了一套标准,然后供市面上的相关产品来执行这套标准,根据测试结果,从侧面展示产品的性能。</p><p>从 <a href="https://www.tpc.org/">TPC 组织的官网</a> 可以了解到,TCP 提供了一系列基准,据我了解,数据库基准测试大致可以分为两类:<a href="https://en.wikipedia.org/wiki/Online_transaction_processing">OLTP</a>、<a href="https://en.wikipedia.org/wiki/Online_analytical_processing">OLAP</a>。OLTP 类型中最流行的就是 TPC-C 测试基准,而 OLAP 中比较流行的则是TPC-H,而我们这次选择的就是TPC-H基准测试 。</p><p>根据<a href="https://tpc.org/TPC_Documents_Current_Versions/pdf/tpc-h_v3.0.0.pdf">TPC-H的基准规范</a>可以知道,TPC-H 将海量数据存储在8张表中,并运行 22 个查询(Q1~Q22)来分析这些数据。其主要评价指标是各个查询的响应时间,即从提交查询到结果返回所需的时间,以及多个会话的吞吐量(每秒查询数量)。基准参考文档中介绍还介绍了,测试表与测试数据的详细信息,一些扩展数据以及执行规则等等,这是一份详细的规则清单以及说明。可以手动的根据基准规范文档来创建数据表、生成数据、执行查询语句。也可以使用TPC-H工具包来生成相应的数据表与数据,然后依次测试这22个查询。</p><p>接下来我会使用MySQL-8.0.36来测试TPC-H的相关基准。MySQL的安装请见之前的文章 <a href="https://antoniozc666.github.io/2024/04/17/linux-pu-tong-yong-hu-wu-root-quan-xian-an-zhuang-mysql-8-0-36/">Linux普通用户无root权限安装MySQL 8.0.36</a></p><h2 id="TPC-H安装包的获取"><a href="#TPC-H安装包的获取" class="headerlink" title="TPC-H安装包的获取"></a>TPC-H安装包的获取</h2><ol><li><p>首先进入官网:<a href="https://tpc.org/">TPC-Homepage</a></p></li><li><p>进入工具包下载页面</p><p><img src="/2024/05/19/linux-huan-jing-xia-shi-yong-tpc-h-ce-shi-mysql-jiao-cheng/TPC-H%E5%B7%A5%E5%85%B7%E5%8C%85%E8%8E%B7%E5%8F%961.png" alt="TPC-H工具包下载页面"></p></li><li><p>进入 TPC-H 下载页面</p><p><img src="/2024/05/19/linux-huan-jing-xia-shi-yong-tpc-h-ce-shi-mysql-jiao-cheng/TPC-H%E5%B7%A5%E5%85%B7%E5%8C%85%E8%8E%B7%E5%8F%962.png" alt="TPC-H下载页面"></p></li><li><p>填写相应信息与验证,获取下载地址</p><p><img src="/2024/05/19/linux-huan-jing-xia-shi-yong-tpc-h-ce-shi-mysql-jiao-cheng/TPC-H%E5%B7%A5%E5%85%B7%E5%8C%85%E8%8E%B7%E5%8F%963.png"></p></li><li><p>获取成功,从邮件中获取下载地址</p></li></ol><h2 id="TPC-H-工具包的使用与介绍"><a href="#TPC-H-工具包的使用与介绍" class="headerlink" title="TPC-H 工具包的使用与介绍"></a>TPC-H 工具包的使用与介绍</h2><p>查看工具包中的内容,了解 TPC-H 如何使用。</p><ul><li>安装包解压后,会有附带的 <code>specification.docx</code> 与 <code>specification.pdf</code> 这里面介绍了测试的相关标准,可以为手动测试提供参考。</li><li><code>TPC-H_Tools_v3.0.0/dbgen</code>目录下的 <code>READEME</code>文件,介绍了工具相关用途。</li></ul><p><img src="/2024/05/19/linux-huan-jing-xia-shi-yong-tpc-h-ce-shi-mysql-jiao-cheng/TPC-H-README.png" alt="TPC-H README文件目录"></p><h3 id="相关文件介绍"><a href="#相关文件介绍" class="headerlink" title="相关文件介绍"></a>相关文件介绍</h3><h4 id="dss-ddl"><a href="#dss-ddl" class="headerlink" title="dss.ddl"></a>dss.ddl</h4><p>该文件里面包含了 创建表的 8 条 SQL 语句。</p><h4 id="dss-ri"><a href="#dss-ri" class="headerlink" title="dss.ri"></a>dss.ri</h4><p>这里是创建表约束的 SQL 语句,例如主键与外键。</p><h4 id="x2F-queries-文件夹"><a href="#x2F-queries-文件夹" class="headerlink" title="/queries 文件夹"></a>/queries 文件夹</h4><p>该文件夹下面是 22 条查询 SQL。</p><h4 id="上一层的-x2F-ref-data-文件夹"><a href="#上一层的-x2F-ref-data-文件夹" class="headerlink" title="上一层的 /ref_data 文件夹"></a>上一层的 /ref_data 文件夹</h4><p>该文件夹里面存放了,填充数据表所需要的数据。</p><h4 id="dbgen-与-qgen"><a href="#dbgen-与-qgen" class="headerlink" title="dbgen 与 qgen"></a>dbgen 与 qgen</h4><p><strong>dbgen</strong> 是用来生成与填充 符合基准的数据表的程序,来帮助我们简单的构建符合要求的数据表与数据。</p><p><strong>qgen</strong> 是基于基准的查询程序,来帮助我们构建一个简单的基准实现。</p><p>以上两个可执行文件,均需要编译生成。</p><h2 id="TPC-H工具包的编译与安装"><a href="#TPC-H工具包的编译与安装" class="headerlink" title="TPC-H工具包的编译与安装"></a>TPC-H工具包的编译与安装</h2><h3 id="准备与解压"><a href="#准备与解压" class="headerlink" title="准备与解压"></a>准备与解压</h3><p>将下载得到的安装包上传到服务器上,重命名后解压:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">unzip</span> tpc-h.zip<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>将文件夹放到你自己想要的位置</p><h3 id="修改部分文件"><a href="#修改部分文件" class="headerlink" title="修改部分文件"></a>修改部分文件</h3><ul><li><strong>后续大部分的 修改、编译、执行等操作都在</strong> <code>mysql-tpc-h/dbgen</code> <strong>目录下完成</strong></li></ul><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token builtin class-name">cd</span> mysql-tpc-h/dbgen<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h3 id="修改makefile文件"><a href="#修改makefile文件" class="headerlink" title="修改makefile文件"></a>修改makefile文件</h3><p>该文件约束了 编译 后可执行文件的部分条件以及需要的依赖。</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">cp</span> makefile.suite makefile<span class="token function">vim</span> makefile<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 修改 103 109 110 111 这四行的内容</span>CC <span class="token operator">=</span> gcc<span class="token comment"># Current values for DATABASE are: INFORMIX, DB2, TDAT (Teradata)</span><span class="token comment"># SQLSERVER, SYBASE, ORACLE, VECTORWISE</span><span class="token comment"># Current values for MACHINE are: ATT, DOS, HP, IBM, ICL, MVS, </span><span class="token comment"># SGI, SUN, U2200, VMS, LINUX, WIN32 </span><span class="token comment"># Current values for WORKLOAD are: TPCH</span><span class="token comment"># 这儿的 DATABASE 字段 指明了一些常用的商业数据库 如果写 MYSQL 需要在 tpcd.h 文件中添加部分宏定义</span><span class="token assign-left variable">DATABASE</span><span class="token operator">=</span> MYSQLMACHINE <span class="token operator">=</span> LINUXWORKLOAD <span class="token operator">=</span> TPCH<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="修改-tpcd-h-文件"><a href="#修改-tpcd-h-文件" class="headerlink" title="修改 tpcd.h 文件"></a>修改 tpcd.h 文件</h3><p>在该文件中添加以下信息:</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifdef</span> <span class="token expression">MYSQL</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">GEN_QUERY_PLAN</span> <span class="token string">""</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">START_TRAN</span> <span class="token string">"START TRANSACTION"</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">END_TRAN</span> <span class="token string">"COMMIT"</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">SET_OUTPUT</span> <span class="token string">""</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">SET_ROWCOUNT</span> <span class="token string">"limit %d;\n"</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">SET_DBASE</span> <span class="token string">"use %s;\n"</span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="编译"><a href="#编译" class="headerlink" title="编译"></a>编译</h3><p>修改了上述文件后 ,我们直接在 <code>/dbgen</code> 目录下使用 <code>make</code> 命令,来生成可执行文件 <code>dbgen</code> 和 <code>qgen</code>。</p><h2 id="TPC-H工具包数据的生成、导入与查询"><a href="#TPC-H工具包数据的生成、导入与查询" class="headerlink" title="TPC-H工具包数据的生成、导入与查询"></a>TPC-H工具包数据的生成、导入与查询</h2><h3 id="生成-lt-table-gt-tbl-数据文件"><a href="#生成-lt-table-gt-tbl-数据文件" class="headerlink" title="生成 <table>.tbl 数据文件"></a>生成 <code><table>.tbl</code> 数据文件</h3><p>可以使用 <code>./dbgen -h</code> 命令查看,也可以查看 <code>README</code> 中对 <code>dbgen</code> 的相关介绍。</p><p><img src="/2024/05/19/linux-huan-jing-xia-shi-yong-tpc-h-ce-shi-mysql-jiao-cheng/dbgen%E5%B8%AE%E5%8A%A9%E4%BF%A1%E6%81%AF.png" alt="dbgen帮助信息"></p><p>使用命令生成符合基准的8张表与相应的数据。由介绍可知, <code>-s</code> 后面是<strong>数据量的大小</strong>,若为 <strong>10</strong> 则生成 <strong>10GB</strong> 数据,若为<strong>100</strong>则生成<strong>100GB</strong>的数据。</p><p>-V :显示进度消息</p><p>-f :覆盖原始数据</p><p>-s : 生成数据的总体规模</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 生成数据</span>./dbgen -vf -s <span class="token number">100</span><span class="token comment"># 查看生成的数据</span><span class="token function">ls</span> *.tbl<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="导入构建脚本文件"><a href="#导入构建脚本文件" class="headerlink" title="导入构建脚本文件"></a>导入构建脚本文件</h3><p>编译之后有<code>dss.ddl</code>和<code>dss.ri</code>两个脚本,dss.ddl是建表脚本,dss.ri脚本是建立主键外键关联关系。</p><p>因为TPC-H benchmark本身不支持MySQL;并且由于MySQL的表名、库名等<strong>默认</strong>都是<strong>严格区分大小写的</strong>,而 <code>dss.ddl</code>(建表脚本)与 <code>dss.ri</code>(表约束脚本)中使用的都是<strong>大写的数据库名与表名</strong>,但是提供的 22 条查询语句又使用的<strong>小写的表名</strong>。所以需要先修改这两个脚本。</p><h4 id="修改-dss-dll"><a href="#修改-dss-dll" class="headerlink" title="修改 dss.dll"></a>修改 <code>dss.dll</code></h4><p>在文件开头添加以下语句:</p><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">DROP</span> <span class="token keyword">DATABASE</span> <span class="token keyword">IF</span> <span class="token keyword">EXISTS</span> tpch<span class="token punctuation">;</span><span class="token keyword">CREATE</span> <span class="token keyword">DATABASE</span> tpch<span class="token punctuation">;</span><span class="token keyword">USE</span> tpch<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><h4 id="修改-dss-ri"><a href="#修改-dss-ri" class="headerlink" title="修改 dss.ri"></a>修改 <code>dss.ri</code></h4><p>可先备份一份,之后将内容修改为以下内容:</p><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token comment">-- Sccsid: @(#)dss.ri 2.1.8.1</span><span class="token comment">-- tpch Benchmark Version 8.0</span><span class="token comment">-- CONNECT TO tpch;</span><span class="token keyword">use</span> tpch<span class="token punctuation">;</span><span class="token comment">-- ALTER TABLE tpch.REGION DROP PRIMARY KEY;</span><span class="token comment">-- ALTER TABLE tpch.NATION DROP PRIMARY KEY;</span><span class="token comment">-- ALTER TABLE tpch.PART DROP PRIMARY KEY;</span><span class="token comment">-- ALTER TABLE tpch.SUPPLIER DROP PRIMARY KEY;</span><span class="token comment">-- ALTER TABLE tpch.PARTSUPP DROP PRIMARY KEY;</span><span class="token comment">-- ALTER TABLE tpch.ORDERS DROP PRIMARY KEY;</span><span class="token comment">-- ALTER TABLE tpch.LINEITEM DROP PRIMARY KEY;</span><span class="token comment">-- ALTER TABLE tpch.CUSTOMER DROP PRIMARY KEY;</span><span class="token comment">-- For table REGION</span><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> tpch<span class="token punctuation">.</span>REGION<span class="token keyword">ADD</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>R_REGIONKEY<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">-- For table NATION</span><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> tpch<span class="token punctuation">.</span>NATION<span class="token keyword">ADD</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>N_NATIONKEY<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> tpch<span class="token punctuation">.</span>NATION<span class="token comment">-- ADD FOREIGN KEY NATION_FK1 (N_REGIONKEY) references tpch.REGION;</span><span class="token keyword">ADD</span> <span class="token keyword">FOREIGN</span> <span class="token keyword">KEY</span> NATION_FK1 <span class="token punctuation">(</span>N_REGIONKEY<span class="token punctuation">)</span> <span class="token keyword">references</span> tpch<span class="token punctuation">.</span>REGION<span class="token punctuation">(</span>R_REGIONKEY<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">COMMIT</span> <span class="token keyword">WORK</span><span class="token punctuation">;</span><span class="token comment">-- For table PART</span><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> tpch<span class="token punctuation">.</span>PART<span class="token keyword">ADD</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>P_PARTKEY<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">COMMIT</span> <span class="token keyword">WORK</span><span class="token punctuation">;</span><span class="token comment">-- For table SUPPLIER</span><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> tpch<span class="token punctuation">.</span>SUPPLIER<span class="token keyword">ADD</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>S_SUPPKEY<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> tpch<span class="token punctuation">.</span>SUPPLIER<span class="token keyword">ADD</span> <span class="token keyword">FOREIGN</span> <span class="token keyword">KEY</span> SUPPLIER_FK1 <span class="token punctuation">(</span>S_NATIONKEY<span class="token punctuation">)</span> <span class="token keyword">references</span> tpch<span class="token punctuation">.</span>NATION<span class="token punctuation">(</span>N_NATIONKEY<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">COMMIT</span> <span class="token keyword">WORK</span><span class="token punctuation">;</span><span class="token comment">-- For table PARTSUPP</span><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> tpch<span class="token punctuation">.</span>PARTSUPP<span class="token keyword">ADD</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>PS_PARTKEY<span class="token punctuation">,</span>PS_SUPPKEY<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">COMMIT</span> <span class="token keyword">WORK</span><span class="token punctuation">;</span><span class="token comment">-- For table CUSTOMER</span><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> tpch<span class="token punctuation">.</span>CUSTOMER<span class="token keyword">ADD</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>C_CUSTKEY<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> tpch<span class="token punctuation">.</span>CUSTOMER<span class="token keyword">ADD</span> <span class="token keyword">FOREIGN</span> <span class="token keyword">KEY</span> CUSTOMER_FK1 <span class="token punctuation">(</span>C_NATIONKEY<span class="token punctuation">)</span> <span class="token keyword">references</span> tpch<span class="token punctuation">.</span>NATION<span class="token punctuation">(</span>N_NATIONKEY<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">COMMIT</span> <span class="token keyword">WORK</span><span class="token punctuation">;</span><span class="token comment">-- For table LINEITEM</span><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> tpch<span class="token punctuation">.</span>LINEITEM<span class="token keyword">ADD</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>L_ORDERKEY<span class="token punctuation">,</span>L_LINENUMBER<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">COMMIT</span> <span class="token keyword">WORK</span><span class="token punctuation">;</span><span class="token comment">-- For table ORDERS</span><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> tpch<span class="token punctuation">.</span>ORDERS<span class="token keyword">ADD</span> <span class="token keyword">PRIMARY</span> <span class="token keyword">KEY</span> <span class="token punctuation">(</span>O_ORDERKEY<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">COMMIT</span> <span class="token keyword">WORK</span><span class="token punctuation">;</span><span class="token comment">-- For table PARTSUPP</span><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> tpch<span class="token punctuation">.</span>PARTSUPP<span class="token keyword">ADD</span> <span class="token keyword">FOREIGN</span> <span class="token keyword">KEY</span> PARTSUPP_FK1 <span class="token punctuation">(</span>PS_SUPPKEY<span class="token punctuation">)</span> <span class="token keyword">references</span> tpch<span class="token punctuation">.</span>SUPPLIER<span class="token punctuation">(</span>S_SUPPKEY<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">COMMIT</span> <span class="token keyword">WORK</span><span class="token punctuation">;</span><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> tpch<span class="token punctuation">.</span>PARTSUPP<span class="token keyword">ADD</span> <span class="token keyword">FOREIGN</span> <span class="token keyword">KEY</span> PARTSUPP_FK2 <span class="token punctuation">(</span>PS_PARTKEY<span class="token punctuation">)</span> <span class="token keyword">references</span> tpch<span class="token punctuation">.</span>PART<span class="token punctuation">(</span>P_PARTKEY<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">COMMIT</span> <span class="token keyword">WORK</span><span class="token punctuation">;</span><span class="token comment">-- For table ORDERS</span><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> tpch<span class="token punctuation">.</span>ORDERS<span class="token keyword">ADD</span> <span class="token keyword">FOREIGN</span> <span class="token keyword">KEY</span> ORDERS_FK1 <span class="token punctuation">(</span>O_CUSTKEY<span class="token punctuation">)</span> <span class="token keyword">references</span> tpch<span class="token punctuation">.</span>CUSTOMER<span class="token punctuation">(</span>C_CUSTKEY<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">COMMIT</span> <span class="token keyword">WORK</span><span class="token punctuation">;</span><span class="token comment">-- For table LINEITEM</span><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> tpch<span class="token punctuation">.</span>LINEITEM<span class="token keyword">ADD</span> <span class="token keyword">FOREIGN</span> <span class="token keyword">KEY</span> LINEITEM_FK1 <span class="token punctuation">(</span>L_ORDERKEY<span class="token punctuation">)</span> <span class="token keyword">references</span> tpch<span class="token punctuation">.</span>ORDERS<span class="token punctuation">(</span>O_ORDERKEY<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">COMMIT</span> <span class="token keyword">WORK</span><span class="token punctuation">;</span><span class="token keyword">ALTER</span> <span class="token keyword">TABLE</span> tpch<span class="token punctuation">.</span>LINEITEM<span class="token keyword">ADD</span> <span class="token keyword">FOREIGN</span> <span class="token keyword">KEY</span> LINEITEM_FK2 <span class="token punctuation">(</span>L_PARTKEY<span class="token punctuation">,</span>L_SUPPKEY<span class="token punctuation">)</span> <span class="token keyword">references</span>tpch<span class="token punctuation">.</span>PARTSUPP<span class="token punctuation">(</span>PS_PARTKEY<span class="token punctuation">,</span>PS_SUPPKEY<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">alter</span> <span class="token keyword">table</span> CUSTOMER <span class="token keyword">rename</span> <span class="token keyword">to</span> customer <span class="token punctuation">;</span><span class="token keyword">alter</span> <span class="token keyword">table</span> LINEITEM <span class="token keyword">rename</span> <span class="token keyword">to</span> lineitem <span class="token punctuation">;</span><span class="token keyword">alter</span> <span class="token keyword">table</span> NATION <span class="token keyword">rename</span> <span class="token keyword">to</span> nation <span class="token punctuation">;</span><span class="token keyword">alter</span> <span class="token keyword">table</span> ORDERS <span class="token keyword">rename</span> <span class="token keyword">to</span> orders <span class="token punctuation">;</span><span class="token keyword">alter</span> <span class="token keyword">table</span> PART <span class="token keyword">rename</span> <span class="token keyword">to</span> part <span class="token punctuation">;</span><span class="token keyword">alter</span> <span class="token keyword">table</span> PARTSUPP <span class="token keyword">rename</span> <span class="token keyword">to</span> partsupp <span class="token punctuation">;</span><span class="token keyword">alter</span> <span class="token keyword">table</span> REGION <span class="token keyword">rename</span> <span class="token keyword">to</span> region <span class="token punctuation">;</span><span class="token keyword">alter</span> <span class="token keyword">table</span> SUPPLIER <span class="token keyword">rename</span> <span class="token keyword">to</span> supplier <span class="token punctuation">;</span><span class="token keyword">COMMIT</span> <span class="token keyword">WORK</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h4 id="建表"><a href="#建表" class="headerlink" title="建表"></a>建表</h4><h5 id="修改MySQL配置"><a href="#修改MySQL配置" class="headerlink" title="修改MySQL配置"></a>修改MySQL配置</h5><p>在建表之前我们需要先修改MySQL的一些配置。一些版本的mysql对通过文件导入导出作了限制,默认不允许。</p><p>查看配置,执行以下MySQL命令:</p><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">SHOW</span> VARIABLES <span class="token operator">LIKE</span> <span class="token string">"secure_file_priv"</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>如果value值为null,则为禁止;如果有文件夹目录,则只允许改目录下文件(测试子目录也不行);如果为空,则不限制目录。</p><p>修改MySQL配置文件 <code>my.cnf</code> ,查看是否有 <code>secure_file_priv=</code> 这一行的内容,如果没有则需要手动添加。我这里是直接置空:</p><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql">secure_file_priv<span class="token operator">=</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>表示不限制目录。<strong>等号一定要有,否则mysql无法启动</strong>。修改完配置文件后,重启MySQL生效。</p><h5 id="生成数据表"><a href="#生成数据表" class="headerlink" title="生成数据表"></a>生成数据表</h5><p>开启MySQL服务,登录MySQL,使用命令 <code>\. path-to-tpc-h/dbgen/dss.ddl</code> 或者 <code>source path-to-tpc-h/dbgen/dss.ddl</code> 来使用修改后的 <code>dss.dll</code>文件创建数据表</p><p><img src="/2024/05/19/linux-huan-jing-xia-shi-yong-tpc-h-ce-shi-mysql-jiao-cheng/%E5%AF%BC%E5%85%A5%E5%91%BD%E4%BB%A4.png" alt="导入 dss.dll 的命令"></p><p>执行成功后查看数据库会发现有一个名叫<code>tpch</code>的数据库;切换到数据库下可以看到由8张表。</p><p><img src="/2024/05/19/linux-huan-jing-xia-shi-yong-tpc-h-ce-shi-mysql-jiao-cheng/%E5%AF%BC%E5%85%A5%E5%90%8E%E7%BB%93%E6%9E%9C%E5%B1%95%E7%A4%BA.png" alt="导入后结果展示"></p><h5 id="建立关联关系"><a href="#建立关联关系" class="headerlink" title="建立关联关系"></a>建立关联关系</h5><p>与上述同理,使用命令 <code>\. path-to-tpc-h/dbgen/dss.ri</code> 或者 <code>source path-to-tpc-h/dbgen/dss.ri</code> 来使用修改后的 <code>dss.dll</code>文件创建表约束</p><p>如果想查看关联关系是否建立成功,可以通过命令 <code>show create table customer\G;</code> 查看</p><h3 id="导入-tbl-数据文件"><a href="#导入-tbl-数据文件" class="headerlink" title="导入 tbl 数据文件"></a>导入 <code>tbl</code> 数据文件</h3><p>由于我要导入的数据很大,导入数据的时间会很长,我写了一个数据导入的脚本进行操作。</p><p>首先创建了一个 <code>loaddata.sql</code> 文件,该文件有所有导入数据所需的SQL命令。文件内容如下:</p><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">load</span> <span class="token keyword">data</span> <span class="token keyword">infile</span> <span class="token string">"/home/czhang/mysql-tpc-h/dbgen/part.tbl"</span> <span class="token keyword">into</span> <span class="token keyword">table</span> part <span class="token keyword">fields</span> <span class="token keyword">terminated</span> <span class="token keyword">by</span> <span class="token string">"|"</span><span class="token punctuation">;</span><span class="token keyword">load</span> <span class="token keyword">data</span> <span class="token keyword">infile</span> <span class="token string">"/home/czhang/mysql-tpc-h/dbgen/region.tbl"</span> <span class="token keyword">into</span> <span class="token keyword">table</span> region <span class="token keyword">fields</span> <span class="token keyword">terminated</span> <span class="token keyword">by</span> <span class="token string">"|"</span> <span class="token keyword">lines</span> <span class="token keyword">terminated</span> <span class="token keyword">by</span> <span class="token string">"|\n"</span><span class="token punctuation">;</span><span class="token keyword">load</span> <span class="token keyword">data</span> <span class="token keyword">infile</span> <span class="token string">"/home/czhang/mysql-tpc-h/dbgen/nation.tbl"</span> <span class="token keyword">into</span> <span class="token keyword">table</span> nation <span class="token keyword">fields</span> <span class="token keyword">terminated</span> <span class="token keyword">by</span> <span class="token string">"|"</span><span class="token punctuation">;</span><span class="token keyword">load</span> <span class="token keyword">data</span> <span class="token keyword">infile</span> <span class="token string">"/home/czhang/mysql-tpc-h/dbgen/customer.tbl"</span> <span class="token keyword">into</span> <span class="token keyword">table</span> customer <span class="token keyword">fields</span> <span class="token keyword">terminated</span> <span class="token keyword">by</span> <span class="token string">"|"</span><span class="token punctuation">;</span><span class="token keyword">load</span> <span class="token keyword">data</span> <span class="token keyword">infile</span> <span class="token string">"/home/czhang/mysql-tpc-h/dbgen/supplier.tbl"</span> <span class="token keyword">into</span> <span class="token keyword">table</span> supplier <span class="token keyword">fields</span> <span class="token keyword">terminated</span> <span class="token keyword">by</span> <span class="token string">"|"</span> <span class="token keyword">lines</span> <span class="token keyword">terminated</span> <span class="token keyword">by</span> <span class="token string">"|\n"</span><span class="token punctuation">;</span><span class="token keyword">load</span> <span class="token keyword">data</span> <span class="token keyword">infile</span> <span class="token string">"/home/czhang/mysql-tpc-h/dbgen/orders.tbl"</span> <span class="token keyword">into</span> <span class="token keyword">table</span> orders <span class="token keyword">fields</span> <span class="token keyword">terminated</span> <span class="token keyword">by</span> <span class="token string">"|"</span><span class="token punctuation">;</span><span class="token keyword">load</span> <span class="token keyword">data</span> <span class="token keyword">infile</span> <span class="token string">"/home/czhang/mysql-tpc-h/dbgen/partsupp.tbl"</span> <span class="token keyword">into</span> <span class="token keyword">table</span> partsupp <span class="token keyword">fields</span> <span class="token keyword">terminated</span> <span class="token keyword">by</span> <span class="token string">"|"</span><span class="token punctuation">;</span><span class="token keyword">load</span> <span class="token keyword">data</span> <span class="token keyword">infile</span> <span class="token string">"/home/czhang/mysql-tpc-h/dbgen/lineitem.tbl"</span> <span class="token keyword">into</span> <span class="token keyword">table</span> lineitem <span class="token keyword">fields</span> <span class="token keyword">terminated</span> <span class="token keyword">by</span> <span class="token string">"|"</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>需要注意的是添加了关联关系,<strong>导入数据的顺序按照关联关系导入</strong></p><p>之后编写bash脚本进行导入,脚本内容如下:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token shebang important">#!/bin/bash</span>mysql -u root -pPASSWORD tpch <span class="token operator"><</span> /home/czhang/mysql-tpc-h/loaddata.sql<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>将 <code>PASSWORD</code>改成自己登录数据库的密码,数据库名和SQL命令文件路径改成自己的。</p><p>随后运行该脚本即可</p><p>导入完成后可运行以下SQL命令查看导入结果:</p><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">SHOW</span> <span class="token keyword">TABLE</span> <span class="token keyword">STATUS</span> <span class="token keyword">FROM</span> tpch<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h3 id="生成查询语句"><a href="#生成查询语句" class="headerlink" title="生成查询语句"></a>生成查询语句</h3><p>前面介绍了 DBGEN ,是用来生成表与数据的,现在来介绍 QGEN ,这也是编译后生成的可执行文件,主要是用来生成查询 SQL 语句的,同样的,和dbgen一样,可以再README中查看该命令的介绍。</p><p>编写一个脚本用于生成查询语句,该脚本位于 <code>dbgen</code>文件夹下,脚本内容如下:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token shebang important">#! /bin/bash</span><span class="token comment"># 拷贝 dists.dss (SQL语句格式化参考) 与 qgen (生成SQL的可执行文件) 到官方提供的 queries (22条未格式化的SQL语句) 目录</span><span class="token function">cp</span> dists.dss qgen queries/<span class="token builtin class-name">cd</span> queries/<span class="token keyword">for</span> <span class="token for-or-select variable">i</span> <span class="token keyword">in</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">..</span><span class="token number">22</span><span class="token punctuation">}</span><span class="token keyword">do</span> <span class="token function">sed</span> -i <span class="token string">"/^:n./d"</span> <span class="token variable">$i</span>.sql ./qgen -d <span class="token variable">$i</span> <span class="token operator">></span> d<span class="token variable">$i</span>.sql<span class="token keyword">done</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p><strong>注意</strong>:在生成查询语句前,queries文件夹中的 22 条未格式化的SQL语句 模板文件不要改动,否则<code>qgen</code>会读取错误导致无法生成正确的查询语句文件</p><p>运行脚本后,可以看到现在已经有了 22 个SQL脚本,分别存放的是 22 条测试语句,但是这些测试语句还不能直接使用,需要做些许修改。</p><p>修改一些变量的内容:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 将所有 limit -1 替换为 limit 1</span><span class="token function">sed</span> -i <span class="token string">'$s/limit -1/limit 1/'</span> *.sql<span class="token comment"># 去掉脚本中的 date 函数</span><span class="token function">sed</span> -i <span class="token string">'s/ date / /g'</span> *.sql<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><p>然后需要手动修改一下 <code>1.sql</code> 、 <code>13.sql</code> 、<code>15.sql</code> 文件</p><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token comment">################################# 1.sql ###########################</span><span class="token comment"># 将 where 条件的内容修改 </span><span class="token comment"># where</span><span class="token comment"># l_shipdate <= date '1998-12-01' - interval '90' day (3)</span><span class="token comment"># 修改为 如下</span><span class="token keyword">where</span>l_shipdate <span class="token operator"><=</span> <span class="token string">'1998-12-01'</span> <span class="token operator">-</span> <span class="token keyword">interval</span> <span class="token string">'90'</span> <span class="token keyword">day</span><span class="token comment">################################# 13.sql ##########################</span><span class="token comment"># 加上字段别名 </span><span class="token comment">#from</span><span class="token comment">#(</span><span class="token comment">#select</span><span class="token comment">#c_custkey,</span><span class="token comment">#count(o_orderkey)</span><span class="token keyword">from</span><span class="token punctuation">(</span><span class="token keyword">select</span>c_custkey<span class="token punctuation">,</span><span class="token function">count</span><span class="token punctuation">(</span>o_orderkey<span class="token punctuation">)</span> <span class="token keyword">as</span> c_count<span class="token comment"># 去除掉 表别名</span><span class="token comment">#c_custkey</span><span class="token comment">#) as c_orders (c_custkey, c_count)</span><span class="token comment">#group by</span><span class="token comment">#c_count</span>c_custkey<span class="token punctuation">)</span> <span class="token keyword">as</span> c_orders<span class="token keyword">group</span> <span class="token keyword">by</span>c_count<span class="token comment">################################ 15.sql ############################</span><span class="token comment"># 以下是修改后的文件</span><span class="token keyword">use</span> tpch<span class="token punctuation">;</span> <span class="token keyword">with</span> revenue0 <span class="token punctuation">(</span>supplier_no<span class="token punctuation">,</span> total_revenue<span class="token punctuation">)</span> <span class="token keyword">as</span><span class="token punctuation">(</span><span class="token keyword">select</span>l_suppkey<span class="token punctuation">,</span><span class="token function">sum</span><span class="token punctuation">(</span>l_extendedprice <span class="token operator">*</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">-</span> l_discount<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token keyword">from</span>lineitem<span class="token keyword">where</span>l_shipdate <span class="token operator">>=</span> <span class="token string">'1996-01-01'</span><span class="token operator">and</span> l_shipdate <span class="token operator"><</span> <span class="token string">'1996-01-01'</span> <span class="token operator">+</span> <span class="token keyword">interval</span> <span class="token string">'3'</span> <span class="token keyword">month</span><span class="token keyword">group</span> <span class="token keyword">by</span>l_suppkey<span class="token punctuation">)</span><span class="token keyword">select</span>s_suppkey<span class="token punctuation">,</span>s_name<span class="token punctuation">,</span>s_address<span class="token punctuation">,</span>s_phone<span class="token punctuation">,</span>total_revenue<span class="token keyword">from</span>supplier<span class="token punctuation">,</span>revenue0<span class="token keyword">where</span>s_suppkey <span class="token operator">=</span> supplier_no<span class="token operator">and</span> total_revenue <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">select</span><span class="token function">max</span><span class="token punctuation">(</span>total_revenue<span class="token punctuation">)</span><span class="token keyword">from</span>revenue0<span class="token punctuation">)</span><span class="token keyword">order</span> <span class="token keyword">by</span>s_suppkey<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>至此,整个SQL就修改完成了</p><h2 id="进行TPC-H基准测试"><a href="#进行TPC-H基准测试" class="headerlink" title="进行TPC-H基准测试"></a>进行TPC-H基准测试</h2><p>首先使用命令进行统一的修改。为每条 SQL 语句指名使用的数据库。</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 在个 SQL 文件的第一行 添加 use tpch; 的语句。</span><span class="token function">sed</span> -i <span class="token string">'1a use tpch; \n'</span> *.sql<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>之后在 <code>queries</code>文件夹中运行命令:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token keyword">for</span> <span class="token for-or-select variable">i</span> <span class="token keyword">in</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">..</span><span class="token number">22</span><span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token keyword">do</span> mysql -u root -p123456 <span class="token operator"><</span> d<span class="token variable">$i</span>.sql<span class="token punctuation">;</span> <span class="token keyword">done</span> <span class="token operator">></span> run.log <span class="token operator"><span class="token file-descriptor important">2</span>></span><span class="token file-descriptor important">&1</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>即可开始基准测试。</p><p><strong>Reference:</strong></p><p><a href="https://gitee.com/hel10word/tpc-h#dssddl">TPC-H在MemSQL的测试</a></p><p><a href="https://blog.csdn.net/weixin_38806650/article/details/122876798">linux环境下测试mysql+TPC-H(实测全流程:环境安装+导入数据+执行查询)</a></p><p><a href="https://blog.csdn.net/qq_41822345/article/details/117463499">MySQL性能测试——tpch工具</a></p>]]></content>
<categories>
<category> 工作负载 </category>
</categories>
<tags>
<tag> MySQL </tag>
<tag> 工作负载 </tag>
<tag> 环境搭建 </tag>
<tag> TPC-H </tag>
<tag> 安装教程 </tag>
</tags>
</entry>
<entry>
<title>二叉树遍历代码模板</title>
<link href="/2024/05/08/er-cha-shu-bian-li-dai-ma-mo-ban/"/>
<url>/2024/05/08/er-cha-shu-bian-li-dai-ma-mo-ban/</url>
<content type="html"><![CDATA[<h2 id="1、二叉树的定义"><a href="#1、二叉树的定义" class="headerlink" title="1、二叉树的定义"></a>1、二叉树的定义</h2><pre class="line-numbers language-c++" data-language="c++"><code class="language-c++">strcut TreeNode{ int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x),left(NULL),right(NULL){};}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="2、二叉树的遍历"><a href="#2、二叉树的遍历" class="headerlink" title="2、二叉树的遍历"></a>2、二叉树的遍历</h2><ul><li>深度优先遍历<ul><li>前序遍历(递归法,迭代法)</li><li>中序遍历(递归法,迭代法)</li><li>后序遍历(递归法,迭代法)</li></ul></li><li>广度优先遍历<ul><li>层次遍历(迭代法)</li></ul></li></ul><h3 id="前-x2F-中-x2F-后序遍历:"><a href="#前-x2F-中-x2F-后序遍历:" class="headerlink" title="前/中/后序遍历:"></a><strong>前/中/后序遍历:</strong></h3><pre class="line-numbers language-c++" data-language="c++"><code class="language-c++">class Solution{public: // 前序遍历 void traversal(TreeNode* cur,vector<int>& res){ // 要加& if(cur == NULL){ return; } res.push_back(cur->val); // 根 traversal(cur->left,res); // 左 traversal(cur->right,res); // 右 } // 中序遍历 void traversal(TreeNode* cur, vector<int>& vec) { if (cur == NULL){ return; } traversal(cur->left, vec); // 左 vec.push_back(cur->val); // 根 traversal(cur->right, vec); // 右 } // 后序遍历 void traversal(TreeNode* cur, vector<int>& vec) { if (cur == NULL){ return; } traversal(cur->left, vec); // 左 traversal(cur->right, vec); // 右 vec.push_back(cur->val); // 根 } vector<int> PreorderTraversal(TreeNode* root){ vector<int> res; traversal(root,res); return res; }}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="层序遍历:"><a href="#层序遍历:" class="headerlink" title="层序遍历:"></a><strong>层序遍历:</strong></h3><pre class="line-numbers language-c++" data-language="c++"><code class="language-c++">class Solution {public: vector<vector<int>> levelOrder(TreeNode* root) { queue<TreeNode*> q; vector<vector<int>> res; if(root != nullptr){ q.push(root); } while(!q.empty()){ vector<int> temp; int size = q.size(); // 存放这一层节点个数 for(int i = 0;i < size;i++){ TreeNode* p = q.front(); q.pop(); temp.push_back(p->val); if(p->left != nullptr){ q.push(p->left); } if(p->right != nullptr){ q.push(p->right); } } res.push_back(temp); } return res; }};// 输入:root = [3,9,20,null,null,15,7] 输出:[[3],[9,20],[15,7]]<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><ul><li>二叉树节点的<strong>深度</strong>:指从<strong>根节点</strong>到<strong>该节点</strong>的最长简单路径边的条数。</li><li>二叉树节点的<strong>高度</strong>:指从<strong>该节点</strong>到<strong>叶子节点</strong>的最长简单路径边的条数。</li></ul><p>求<strong>深度</strong>可以从上到下去查,所以需要<strong>前序遍历</strong>(根左右),而<strong>高度</strong>需要从下到上去查,所以要<strong>后序遍历</strong>(左右根)</p><h2 id="3、二叉搜索树(二叉排序树)"><a href="#3、二叉搜索树(二叉排序树)" class="headerlink" title="3、二叉搜索树(二叉排序树)"></a>3、二叉搜索树(二叉排序树)</h2><p>遇到在<strong>二叉搜索树(二叉排序树)</strong>上求什么<strong>最值</strong>,求<strong>差值</strong>之类的,都要思考一下二叉搜索树可是<strong>有序的</strong>,要利用好这一特点</p><p>如果递归函数有返回值,如何区分要搜索一条边,还是搜索整个树:</p><p><strong>搜索一条边的写法:</strong></p><pre class="line-numbers language-c++" data-language="c++"><code class="language-c++">if (递归函数(root->left)) return ;if (递归函数(root->right)) return ;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p><strong>搜索整个树写法:</strong></p><pre class="line-numbers language-c++" data-language="c++"><code class="language-c++">left = 递归函数(root->left);right = 递归函数(root->right);left与right的逻辑处理;<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><p><strong>选择什么遍历顺序:</strong></p><ul><li>涉及到二叉树的构造,无论普通二叉树还是二叉搜索树一定前序,都是先构造中节点。</li><li>求普通二叉树的属性,一般是后序,一般要通过递归函数的返回值做计算。(具体问题具体分析)也有用前序的</li><li>求二叉搜索树的属性,一定是中序了,要不白瞎了有序性了。</li></ul><p><img src="/2024/05/08/er-cha-shu-bian-li-dai-ma-mo-ban/%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9B%B8%E5%85%B3%E7%9F%A5%E8%AF%86%E5%BD%92%E7%BA%B3.png" alt="二叉树相关知识归纳"></p>]]></content>
<categories>
<category> 算法 </category>
</categories>
<tags>
<tag> 树 </tag>
<tag> 二叉树 </tag>
<tag> 二叉树的遍历 </tag>
</tags>
</entry>
<entry>
<title>C++刷题STL</title>
<link href="/2024/05/07/c-shua-ti-stl/"/>
<url>/2024/05/07/c-shua-ti-stl/</url>
<content type="html"><![CDATA[<h1 id="C-之刷题常用STL容器整理"><a href="#C-之刷题常用STL容器整理" class="headerlink" title="C++之刷题常用STL容器整理"></a>C++之刷题常用STL容器整理</h1><h2 id="1-vector"><a href="#1-vector" class="headerlink" title="1.vector"></a>1.vector</h2><p>动态数组,方便的动态扩容,方便的变量初始化(int类型默认初始化为0,bool默认初始化为false),可以用来实现邻接表(结点数太多的图)。</p><p>头文件</p><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><vector></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>定义</p><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token comment">//typename 可以是基本数据类型,可以是其它标准stl容器,可以是自定义结构体</span>vector<span class="token operator"><</span><span class="token keyword">typename</span><span class="token operator">></span> name<span class="token punctuation">;</span>vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> v1<span class="token punctuation">;</span>vector<span class="token operator"><</span>vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token operator">></span> v2<span class="token punctuation">;</span> <span class="token comment">//两个维度都是动态的</span>vector<span class="token operator"><</span>student<span class="token operator">></span> <span class="token function">v3</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//一维固定为10,二维动态</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>元素访问</p><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token comment">//1.下标访问</span>v<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token comment">//2.迭代器访问</span>vector<span class="token operator"><</span><span class="token keyword">typename</span><span class="token operator">></span><span class="token double-colon punctuation">::</span>iterator it<span class="token punctuation">;</span><span class="token keyword">auto</span> it<span class="token punctuation">;</span><span class="token comment">//另一种迭代器简易定义方法</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">auto</span> it <span class="token operator">=</span> v<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>it<span class="token operator">!=</span>v<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>it<span class="token operator">++</span><span class="token punctuation">)</span> cout<span class="token operator"><<</span><span class="token operator">*</span>it<span class="token punctuation">;</span><span class="token comment">//迭代器it可以进行算术运算</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>常用函数</p><table><thead><tr><th>函数</th><th>说明</th></tr></thead><tbody><tr><td>push_back(x);</td><td>将元素x添加到容器末尾</td></tr><tr><td>pop_back();</td><td>删除容器末尾元素</td></tr><tr><td>size();</td><td>获取容器大小</td></tr><tr><td>clear();</td><td>清空元素</td></tr><tr><td>insert(it,x);</td><td>在it处插入一个元素x</td></tr><tr><td>erase(it);</td><td>删除it处的元素</td></tr><tr><td>erase(first,last);</td><td>删除[first,last)区间内的元素</td></tr></tbody></table><p>使用场景</p><ul><li>元素个数不确定</li><li>用于实现邻接表存储图</li></ul><h2 id="2-set"><a href="#2-set" class="headerlink" title="2.set"></a>2.set</h2><p>内部自动有序且不含重复元素的集合。</p><p>头文件</p><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><set></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>定义</p><pre class="line-numbers language-c" data-language="c"><code class="language-c">set<span class="token operator"><</span>typename<span class="token operator">></span> name<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>元素访问</p><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">auto</span> it <span class="token operator">=</span> v<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>it<span class="token operator">!=</span>v<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>it<span class="token operator">++</span><span class="token punctuation">)</span> cout<span class="token operator"><<</span><span class="token operator">*</span>it<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>常用函数</p><table><thead><tr><th>函数</th><th>说明</th></tr></thead><tbody><tr><td>insert(x);</td><td>将元素x插入set容器中,并自动递增排序和去重</td></tr><tr><td>find(value);</td><td>查找值为value的元素,返回对应迭代器</td></tr><tr><td>erase(it);</td><td>删除it处的元素,通常结合find函数使用</td></tr><tr><td>erase(value);</td><td>删除值为value的元素</td></tr><tr><td>erase(first,last);</td><td>删除[first,last)区间内的元素</td></tr><tr><td>size();</td><td>返回容器大小</td></tr><tr><td>clear();</td><td>清空容器</td></tr></tbody></table><p>使用场景:</p><ul><li>自动去重并按升序排序</li></ul><p>扩展:</p><ul><li>需要元素不唯一,使用multiset</li><li>需要元素不排序,unordered_set(內部以散列代替了set内部的红黑树),速度比set快得多</li></ul><h2 id="3-string"><a href="#3-string" class="headerlink" title="3.string"></a>3.string</h2><p>字符串,可以替换为C语言版的字符数组(str.c_str();)。</p><p>头文件</p><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><string></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>定义</p><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp">string str<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>内容访问</p><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token comment">//1.下标访问</span>str<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token comment">//2.输入输出只能用cin和cout,除非转化为字符数组</span><span class="token comment">//3.迭代器访问</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>常用函数</p><table><thead><tr><th>函数</th><th>说明</th></tr></thead><tbody><tr><td>operator+=</td><td>字符串拼接</td></tr><tr><td>compare operator</td><td>按照字典序比较大小</td></tr><tr><td>length()/size()</td><td>获取字符串大小</td></tr><tr><td>insert(pos,str);</td><td>pos是int类型,表示在指定位置插入str</td></tr><tr><td>insert(it,first,last);</td><td>在it处插入[first,last)范围内的字符串</td></tr><tr><td>erase(it);</td><td>删除it处的元素</td></tr><tr><td>erase(first,last);</td><td>删除[first,last)范围内的元素</td></tr><tr><td>erase(pos,length);</td><td>删除pos位置开始的length个字符</td></tr><tr><td>clear();</td><td>清空字符串</td></tr><tr><td>substr(pos,len);</td><td>返回从pos位置开始len长度的字符串</td></tr><tr><td>string::npos</td><td>作为find函数未找到的判断依据</td></tr><tr><td>find(str);</td><td>返回str第一次出现的位置</td></tr><tr><td>replace(pos,len,str2);</td><td>从pos位置开始,长度为len的子串替换为str2</td></tr></tbody></table><h2 id="4-map"><a href="#4-map" class="headerlink" title="4.map"></a>4.map</h2><p>将任何基本类型映射到任何基本类型(包括STL容器)。</p><p>可以用于hash散列(当元素个数比较多时,不适合用数组散列,就用map)</p><p>头文件及定义</p><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><map></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>map<span class="token operator"><</span>typename1<span class="token punctuation">,</span>typename2<span class="token operator">></span> mp<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><p>元素访问</p><pre class="line-numbers language-c" data-language="c"><code class="language-c"><span class="token comment">//1.下标</span>mp<span class="token punctuation">[</span><span class="token char">'c'</span><span class="token punctuation">]</span>mp<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>mp<span class="token punctuation">[</span><span class="token string">"key"</span><span class="token punctuation">]</span><span class="token comment">//2.迭代器</span><span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">auto</span> it <span class="token operator">=</span> mp<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> it <span class="token operator">!=</span> mp<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> it<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>cout<span class="token operator"><<</span> it<span class="token operator">-></span>first<span class="token operator"><<</span><span class="token string">" "</span><span class="token operator"><<</span>it<span class="token operator">-></span>second<span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>常用函数</p><table><thead><tr><th>函数</th><th>说明</th></tr></thead><tbody><tr><td>find(key);</td><td>返回键为key的映射的迭代器</td></tr><tr><td>erase(it);</td><td>删除it处的元素</td></tr><tr><td>erase(key);</td><td>删除key</td></tr><tr><td>erase(first,last);</td><td>删除[first,last)区间内的元素</td></tr><tr><td>size();</td><td>返回容器大小</td></tr><tr><td>clear();</td><td>清空容器</td></tr></tbody></table><p>使用场景:</p><ul><li>散列表</li><li>其他映射</li></ul><p>扩展:</p><ul><li>多映射,即一个key对应多个value,使用multimap</li><li>unordered_map可以替代map,内部实现使用散列代替了map内部的红黑树实现,用于处理只映射而不需要按key来排序的需求,速度快</li></ul><h2 id="5-queue"><a href="#5-queue" class="headerlink" title="5.queue"></a>5.queue</h2><p>队列,先进先出的容器,常用于广度优先搜索</p><p>头文件及定义</p><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><queue></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>queue<span class="token operator"><</span><span class="token keyword">typename</span><span class="token operator">></span> name<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><p>常用函数</p><table><thead><tr><th>函数</th><th>说明</th></tr></thead><tbody><tr><td>push(x);</td><td>元素x入队列</td></tr><tr><td>front();</td><td>访问队列的首元素</td></tr><tr><td>back();</td><td>访问队列的尾元素</td></tr><tr><td>pop();</td><td>队首元素出队</td></tr><tr><td>empty();</td><td>检测是否为空队列</td></tr><tr><td>size();</td><td>队列大小</td></tr></tbody></table><p>使用场景</p><ul><li>广度优先搜索</li></ul><p><strong>另外</strong>,需要注意使用front(),back(), pop()函数前,必须判断是否为空队列(使用empty函数)</p><p>扩展</p><ul><li>deque(double end queue,双端队列)首位皆可插入和删除</li><li>priority_queue,使用堆实现的默认将当前队列最大元素置于队首的容器</li></ul><h2 id="6-priority-queue"><a href="#6-priority-queue" class="headerlink" title="6.priority_queue"></a>6.priority_queue</h2><p>优先队列,默认情况下是将队列中最大元素置于队首,优先级可以自定义。每次进行push(),pop()操作,底层的数据结构堆(heap)都会随时调整结构,使优先级最高的元素永远都在队首。</p><p>头文件及定义</p><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><queue></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>priority_queue<span class="token operator"><</span><span class="token keyword">typename</span><span class="token operator">></span> name<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><p>常用函数</p><table><thead><tr><th>函数</th><th>说明</th></tr></thead><tbody><tr><td>push(x);</td><td>将元素x入队</td></tr><tr><td>top();</td><td>访问队首元素</td></tr><tr><td>pop();</td><td>将队首元素出队</td></tr><tr><td>empty();</td><td>检测是否为空队列</td></tr><tr><td>size();</td><td>返回队列大小</td></tr></tbody></table><p>优先级设定</p><p>基本数据类型(int,double,char)默认数字大或者字典序大的优先级高。</p><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp">priority_queue<span class="token operator"><</span><span class="token keyword">int</span><span class="token punctuation">,</span>vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span><span class="token punctuation">,</span>greater<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">>></span> q<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><ul><li>greater表示这个数字越小,优先级越高</li><li>less表示数字越大,优先级越高(默认情况)</li></ul><p>vector表示底层数据结构堆(heap)的容器</p><p>结构体优先级设置</p><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><string></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><queue></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">struct</span> <span class="token class-name">fruit</span><span class="token punctuation">{</span> string name<span class="token punctuation">;</span> <span class="token keyword">int</span> price<span class="token punctuation">;</span> <span class="token keyword">friend</span> <span class="token keyword">bool</span> <span class="token keyword">operator</span> <span class="token operator"><</span> <span class="token punctuation">(</span>fruit f1<span class="token punctuation">,</span>fruit f2<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> f1<span class="token punctuation">.</span>price <span class="token operator">></span> f2<span class="token punctuation">.</span>price<span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span>f1<span class="token punctuation">,</span>f2<span class="token punctuation">,</span>f3<span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> priority_queue<span class="token operator"><</span>fruit<span class="token operator">></span> q<span class="token punctuation">;</span> f1<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">"桃子"</span><span class="token punctuation">;</span> f1<span class="token punctuation">.</span>price <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span> f2<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">"梨子"</span><span class="token punctuation">;</span> f2<span class="token punctuation">.</span>price <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">;</span> f3<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">"苹果"</span><span class="token punctuation">;</span> f3<span class="token punctuation">.</span>price <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>f1<span class="token punctuation">)</span><span class="token punctuation">;</span> q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>f2<span class="token punctuation">)</span><span class="token punctuation">;</span> q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>f3<span class="token punctuation">)</span><span class="token punctuation">;</span> cout<span class="token operator"><<</span> q<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>name<span class="token operator"><<</span><span class="token string">" "</span><span class="token operator"><<</span>q<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>price <span class="token operator"><<</span>endl<span class="token punctuation">;</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>output:</p><pre class="line-numbers language-none"><code class="language-none">苹果 1<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>使用场景</p><p>可以用于dijkstra算法的顶点选择中</p><p>注意使用top()函数之前判断队列是否为空(使用empty()函数)</p><h2 id="7-stack"><a href="#7-stack" class="headerlink" title="7.stack"></a>7.stack</h2><p>栈,先进后出</p><p>头文件及定义</p><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><stack></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>stack<span class="token operator"><</span><span class="token keyword">typename</span><span class="token operator">></span> name<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><p>常用函数</p><table><thead><tr><th>函数</th><th>说明</th></tr></thead><tbody><tr><td>push(x);</td><td>元素x入栈</td></tr><tr><td>top();</td><td>获取栈顶元素</td></tr><tr><td>pop();</td><td>栈顶元素出栈</td></tr><tr><td>empty();</td><td>检测stack是否为空</td></tr><tr><td>size();</td><td>获取栈的大小</td></tr></tbody></table><p>使用场景</p><p>递归模拟,防止递归深度过深,深度优先搜索模拟。</p><h2 id="8-pair"><a href="#8-pair" class="headerlink" title="8.pair"></a>8.pair</h2><p>头文件及使用</p><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><utility></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token comment">//map实现中涉及pair,使用map时自动添加了该头文件</span>pair<span class="token operator"><</span>typename1<span class="token punctuation">,</span>typename2<span class="token operator">></span> name<span class="token punctuation">;</span>pair<span class="token operator"><</span>string <span class="token punctuation">,</span><span class="token keyword">int</span> <span class="token operator">></span> <span class="token function">p</span><span class="token punctuation">(</span><span class="token string">"hahahah"</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token function">make_pair</span><span class="token punctuation">(</span><span class="token string">"haha"</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>cout<span class="token operator"><<</span>p<span class="token punctuation">.</span>first<span class="token operator"><<</span><span class="token string">" "</span><span class="token operator"><<</span>p<span class="token punctuation">.</span>second<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>]]></content>
<categories>
<category> 小知识 </category>
</categories>
<tags>
<tag> C++ </tag>
<tag> 算法 </tag>
<tag> 小知识 </tag>
</tags>
</entry>
<entry>
<title>C++刷题API</title>
<link href="/2024/05/07/c-shua-ti-api/"/>
<url>/2024/05/07/c-shua-ti-api/</url>
<content type="html"><![CDATA[<h2 id="1-数组"><a href="#1-数组" class="headerlink" title="1.数组"></a>1.数组</h2><pre class="line-numbers language-c++" data-language="c++"><code class="language-c++">#include<iostream>#include<algorithm>using namespace std;int main() { int arr[] = {5, 4, 1, 7, 2}; //定义时创建数组 int length = sizeof(arr) / sizeof(int); // 求出数组的长度 cout << length << endl; sort(arr, arr + 5); for (int e : arr) cout << e << " "; int* arr2 = new int[10]; delete [] arr2;}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="2-char"><a href="#2-char" class="headerlink" title="2.char"></a>2.char</h2><pre class="line-numbers language-java" data-language="java"><code class="language-java">#include<span class="token generics"><span class="token punctuation"><</span>iostream<span class="token punctuation">></span></span>#include<span class="token generics"><span class="token punctuation"><</span>cstring<span class="token punctuation">></span></span>using namespace std<span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">char</span> c <span class="token operator">=</span> <span class="token char">'A'</span><span class="token punctuation">;</span> bool is <span class="token operator">=</span> <span class="token function">isalpha</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//字母(包括大小写)</span> cout <span class="token operator"><<</span> is <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token comment">//true:1</span> bool is2 <span class="token operator">=</span> <span class="token function">islower</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//小写字母</span> bool is3 <span class="token operator">=</span> <span class="token function">isupper</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//大写字母</span> bool is4 <span class="token operator">=</span> <span class="token function">isalnum</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//字母大写小写 + 数字</span> bool is5 <span class="token operator">=</span> <span class="token function">isblank</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//空格和 \t</span> bool is6 <span class="token operator">=</span> <span class="token function">isspace</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//空格、\t、\r、\n</span> <span class="token keyword">char</span> cc <span class="token operator">=</span> <span class="token function">toupper</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">char</span> tt <span class="token operator">=</span> <span class="token function">tolower</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="3-string"><a href="#3-string" class="headerlink" title="3.string"></a>3.string</h2><pre class="line-numbers language-c++" data-language="c++"><code class="language-c++">#include<iostream>#include<cstring>#include<string>using namespace std;int main() { string s = "Hello,World";//赋值字符串 string s2 = s; //产生一个新的字符串 string s3 = s + s2; //字符串拼接 string s4; cin>>s4; //输入字符串 cout<<s4; //输出字符串 s.length(); //字符串的真实长度 getline(cin, s); //读取一行字符串,包括空格 string s7 = s.substr(4); // 表示从下标4开始一直到结束 string s8 = s.substr(5,3); //表示从下标5开始,3个字符}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="4-vector"><a href="#4-vector" class="headerlink" title="4.vector"></a>4.vector</h2><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><vector></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//定义时不指定大小,不指定默认值</span> vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> v<span class="token punctuation">;</span> <span class="token comment">//定义一个vector v,定义的时候没有分配大小</span> cout<span class="token operator"><<</span>v<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//0</span> v<span class="token punctuation">.</span><span class="token function">resize</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//动态分配大小为8,默认这8个元素都是0</span> <span class="token comment">//定义时指定大小,指定默认值</span> vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token function">c</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//长度100,所有元素值为9</span> <span class="token comment">//增删改查</span> v<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//末尾添加一个元素</span> <span class="token comment">//遍历</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">auto</span> it <span class="token operator">=</span> c<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> it <span class="token operator">!=</span> c<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> it<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> cout<span class="token operator"><<</span> <span class="token operator">*</span>it <span class="token operator"><<</span> <span class="token string">" "</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="5-set-x2F-unordered-set"><a href="#5-set-x2F-unordered-set" class="headerlink" title="5.set/unordered_set"></a>5.set/unordered_set</h2><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><set></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><unordered_set></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> set<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> s<span class="token punctuation">;</span> <span class="token comment">// 定义一个空集合s 有序的,二叉树实现,O(logN)</span> unordered_set<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> set2<span class="token punctuation">;</span><span class="token comment">//无序的,哈希表实现,O(1)</span> s<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//向集合s里面插入一个1</span> cout <span class="token operator"><<</span> <span class="token operator">*</span><span class="token punctuation">(</span>s<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token comment">//输出集合s的第一个元素</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">6</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> s<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span>i <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//插入</span> <span class="token punctuation">}</span> <span class="token comment">//遍历</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">auto</span> it <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> it <span class="token operator">!=</span> s<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> it<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> cout <span class="token operator"><<</span> <span class="token operator">*</span>it <span class="token operator"><<</span> <span class="token string">" "</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//查找一个元素</span> <span class="token comment">//找到为true:1.找不到为false:0</span> cout <span class="token operator"><<</span> <span class="token punctuation">(</span>s<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span> <span class="token operator">!=</span> s<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token comment">//删除</span> s<span class="token punctuation">.</span><span class="token function">erase</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//删除集合s中的1这个元素</span> cout <span class="token operator"><<</span> <span class="token punctuation">(</span>s<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token operator">!=</span> s<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span><span class="token comment">//此时1已经找不到了</span> cout <span class="token operator"><<</span> <span class="token string">"true"</span> <span class="token operator"><<</span><span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token comment">//向下取整</span> cout <span class="token operator"><<</span> <span class="token operator">*</span>s<span class="token punctuation">.</span><span class="token function">lower_bound</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span><span class="token comment">//小于等于4中最大的</span> cout <span class="token operator"><<</span> <span class="token operator">*</span>s<span class="token punctuation">.</span><span class="token function">upper_bound</span><span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">)</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span><span class="token comment">//大于等于7中最小的</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="6-map-x2F-unordered-map"><a href="#6-map-x2F-unordered-map" class="headerlink" title="6.map/unordered_map"></a>6.map/unordered_map</h2><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><map></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><unordered_map></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> map<span class="token operator"><</span>string<span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span> m<span class="token punctuation">;</span> <span class="token comment">//定义一个空的 map,键是string类型,值是int类型</span> m<span class="token punctuation">[</span><span class="token string">"hello"</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">;</span> <span class="token comment">//将 key是"hello", 值是2的键值对存入 map中</span> cout <span class="token operator"><<</span> m<span class="token punctuation">[</span><span class="token string">"hello"</span><span class="token punctuation">]</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token comment">//访问map中key为"hello"的value,如果 key不存在,则返回0,在这之后,这个key就存在了</span> cout <span class="token operator"><<</span> m<span class="token punctuation">[</span><span class="token string">"world"</span><span class="token punctuation">]</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span> m<span class="token punctuation">[</span><span class="token string">"world"</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span> m<span class="token punctuation">[</span><span class="token string">"aaa"</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">77</span><span class="token punctuation">;</span> m<span class="token punctuation">[</span><span class="token string">"bbb"</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">999</span><span class="token punctuation">;</span> <span class="token comment">//使用迭代器遍历,输出 map 中的所有元素,键用it->first获取,值用it->second获取</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">auto</span> it <span class="token operator">=</span> m<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> it <span class="token operator">!=</span> m<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> it<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> cout <span class="token operator"><<</span> it <span class="token operator">-></span> first <span class="token operator"><<</span> <span class="token string">" "</span> <span class="token operator"><<</span> it <span class="token operator">-></span> second <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//访问 map 的第一个元素,输出它的键和值</span> cout <span class="token operator"><<</span> m<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-></span> first <span class="token operator"><<</span> <span class="token string">" "</span> <span class="token operator"><<</span> m<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-></span> second <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token comment">//访问map的最后一个元素,输出它的键和值</span> cout <span class="token operator"><<</span> m<span class="token punctuation">.</span><span class="token function">rbegin</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-></span> first <span class="token operator"><<</span> <span class="token string">" "</span> <span class="token operator"><<</span> m<span class="token punctuation">.</span><span class="token function">rbegin</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-></span> second <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token comment">//输出map的元素个数</span> cout <span class="token operator"><<</span> m<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token comment">//对于有序的map</span> map<span class="token operator"><</span>string<span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token operator">></span> mm<span class="token punctuation">;</span> <span class="token comment">//key是升序的a,b,c,d</span> map<span class="token operator"><</span>string<span class="token punctuation">,</span> <span class="token keyword">int</span><span class="token punctuation">,</span> greater<span class="token operator"><</span>string<span class="token operator">>></span> mmm<span class="token punctuation">;</span><span class="token comment">//key是降序的c,b,a</span> <span class="token comment">//end()是最后一个元素的后一个,是空。rbegin()才是最后一个元素</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="7-stack"><a href="#7-stack" class="headerlink" title="7.stack"></a>7.stack</h2><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><stack></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> stack<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> s<span class="token punctuation">;</span> <span class="token comment">//定义一个空栈</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">6</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> s<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//将元素i压入栈s中</span> <span class="token punctuation">}</span> cout <span class="token operator"><<</span> s<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token comment">//访问s的栈顶元素</span> cout <span class="token operator"><<</span> s<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token comment">//输出s的元素个数</span> s<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//移除栈顶元素</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="8-queue"><a href="#8-queue" class="headerlink" title="8.queue"></a>8.queue</h2><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><queue></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> queue<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> q<span class="token punctuation">;</span><span class="token comment">//定义一个空栈</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">6</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//将i的值依次压入到队列q中</span> <span class="token punctuation">}</span> cout <span class="token operator"><<</span> q<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator"><<</span> <span class="token string">""</span> <span class="token operator"><<</span> q<span class="token punctuation">.</span><span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span><span class="token comment">//访问队首和队尾元素</span> cout<span class="token operator"><<</span> q<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span> <span class="token comment">//输出队列的元素个数</span> q<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//移除队列的队首元素</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="9-排序"><a href="#9-排序" class="headerlink" title="9.排序"></a>9.排序</h2><pre class="line-numbers language-cpp" data-language="cpp"><code class="language-cpp"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><iostream></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><vector></span></span><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string"><algorithm></span></span><span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span><span class="token keyword">bool</span> <span class="token function">cmp</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span> <span class="token keyword">int</span> b<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> a <span class="token operator">></span> b<span class="token punctuation">;</span><span class="token comment">//从大到小排序</span><span class="token punctuation">}</span><span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> vector<span class="token operator"><</span><span class="token keyword">int</span><span class="token operator">></span> <span class="token function">v</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> cin <span class="token operator">>></span> v<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token function">sort</span><span class="token punctuation">(</span>v<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> v<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//默认,从小到大排序</span> <span class="token keyword">int</span> arr<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> cin <span class="token operator">>></span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token function">sort</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> arr <span class="token operator">+</span> <span class="token number">10</span><span class="token punctuation">,</span> cmp<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//arr从大到小排列</span> <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="10-其他"><a href="#10-其他" class="headerlink" title="10.其他"></a>10.其他</h2><p>auto 自动推断变量类型,用在迭代器里</p><pre class="line-numbers language-CPP" data-language="CPP"><code class="language-CPP">基于范围的for循环int arr[4] = {0, 1, 2, 3};for (int i : arr)cout << i << endl; // 输出数组中的每⼀个元素的值,每个元素占据⼀⾏// i 依次表示数组中的每⼀个元素,此时read onlyfor (int &i : arr) // i为引⽤变量i = i * 2; // 将数组中的每⼀个元素都乘以2,arr[4]的内容变为了{0, 2, 4, 6}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>类型转换</p><pre class="line-numbers language-CPP" data-language="CPP"><code class="language-CPP">to_string stoi stodstof (string to float)stold (string to long double)stol (string to long)stoll (string to long long)stoul(string to unsigned long)stoull (string to unsigned long long)<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>]]></content>
<categories>
<category> 小知识 </category>
</categories>
<tags>
<tag> C++ </tag>
<tag> 算法 </tag>
<tag> 小知识 </tag>
</tags>
</entry>
<entry>
<title>Java刷题API</title>
<link href="/2024/05/07/java-shua-ti-api/"/>
<url>/2024/05/07/java-shua-ti-api/</url>
<content type="html"><![CDATA[<p><img src="/2024/05/07/java-shua-ti-api/1.png" alt="图片一览"></p><h2 id="0:输入输出"><a href="#0:输入输出" class="headerlink" title="0:输入输出"></a>0:输入输出</h2><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">Scanner</span></span><span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">Scanner</span> scanner <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Scanner</span><span class="token punctuation">(</span><span class="token class-name">System</span><span class="token punctuation">.</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//方法1:一般用来接收数组。以空格分界</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> ints <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>scanner<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> num <span class="token operator">=</span> scanner<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>num <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span> ints<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> num<span class="token punctuation">;</span> <span class="token comment">/*或者*/</span> <span class="token class-name">String</span> next <span class="token operator">=</span> scanner<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//方法2:一般用来接受字符串,以换行符分界</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> <span class="token keyword">while</span> <span class="token punctuation">(</span>scanner<span class="token punctuation">.</span><span class="token function">hasNextLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">String</span> str <span class="token operator">=</span> scanner<span class="token punctuation">.</span><span class="token function">nextLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>str <span class="token operator">==</span> <span class="token string">""</span><span class="token punctuation">)</span> <span class="token keyword">break</span><span class="token punctuation">;</span> arr<span class="token punctuation">[</span>i<span class="token operator">++</span><span class="token punctuation">]</span> <span class="token operator">=</span> str<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="一:Integer-类"><a href="#一:Integer-类" class="headerlink" title="一:Integer 类"></a>一:Integer 类</h2><p>1.String -> int:int value = Integer.parseInt(str);<br>2.将二进制字符串str转换成int: int binary = Integer.parseInt(str,2);//“111”->7<br>3.十进制int转二进制字符串:String binaryN=Integer.toBinaryString(N);//15 -> “1111”</p><h2 id="二:String-类"><a href="#二:String-类" class="headerlink" title="二:String 类"></a>二:String 类</h2><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StringExample</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">String</span> s <span class="token operator">=</span> <span class="token string">"hel"</span><span class="token operator">+</span><span class="token string">"lo"</span><span class="token punctuation">;</span><span class="token comment">//连接</span> <span class="token keyword">int</span> len <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">char</span> c <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">charAt</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//返回指定字符在此字符串中第一次出现处的索引,找不到返回-1</span> <span class="token keyword">int</span> index1 <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token string">"el"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//1</span> <span class="token class-name">String</span> substr <span class="token operator">=</span> s<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//hel,前闭后开</span> <span class="token class-name">String</span> s2 <span class="token operator">=</span> <span class="token string">"Hi,Linda,Hello"</span><span class="token punctuation">;</span> <span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> ss <span class="token operator">=</span> s2<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">","</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//分割</span> <span class="token comment">//和Integer的相互转换</span> <span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">parseInt</span><span class="token punctuation">(</span><span class="token string">"24"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">String</span> string <span class="token operator">=</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//和Long的相互转换</span> <span class="token keyword">long</span> ll <span class="token operator">=</span> <span class="token class-name">Long</span><span class="token punctuation">.</span><span class="token function">parseLong</span><span class="token punctuation">(</span><span class="token string">"25"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> string <span class="token operator">=</span> <span class="token class-name">Long</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>ll<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//和char[]的相互转换</span> <span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> cc <span class="token operator">=</span> string<span class="token punctuation">.</span><span class="token function">toCharArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> string <span class="token operator">=</span> <span class="token class-name">String</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span>cc<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//字符串是否相等</span> <span class="token class-name">String</span> str1 <span class="token operator">=</span> <span class="token string">"hello"</span><span class="token punctuation">,</span>str2 <span class="token operator">=</span> <span class="token string">"hello"</span><span class="token punctuation">;</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>str1<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>str2<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//替换</span> str1<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">"1"</span><span class="token punctuation">,</span><span class="token string">"a"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//将1替换为a</span> <span class="token comment">//去除头尾空格</span> str1 <span class="token operator">=</span> str1<span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="三:数组"><a href="#三:数组" class="headerlink" title="三:数组"></a>三:数组</h2><p>关于多重数组【重点】</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token punctuation">]</span> nums <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>nums<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//3,一共有多少行</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>nums<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//2,第0行的长度</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token operator">*</span></span><span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ArrayExample</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr2 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">7</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment">//全部填充</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">fill</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//[3, 3, 3, 3, 3, 3, 3, 3, 3, 3]</span> <span class="token comment">//排序,默认从小到大</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>arr2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>arr2<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//arr2:[1 3 4 5 7 ]</span> <span class="token comment">//排序,从大到小,lambda方法</span> <span class="token class-name">Integer</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr3 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>arr3<span class="token punctuation">,</span> <span class="token punctuation">(</span>a<span class="token punctuation">,</span> b<span class="token punctuation">)</span> <span class="token operator">-></span> b <span class="token operator">-</span> a<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>arr3<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//[8, 6, 3, 2, 1]</span> <span class="token comment">//排序,从大到小,匿名类</span> <span class="token class-name">Integer</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr4 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">8</span><span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>arr4<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">Comparator</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">compare</span><span class="token punctuation">(</span><span class="token class-name">Integer</span> o1<span class="token punctuation">,</span> <span class="token class-name">Integer</span> o2<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> o2 <span class="token operator">-</span> o1<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>arr4<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//[8, 6, 3, 2, 1]</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="四:动态数组(ArrayList)"><a href="#四:动态数组(ArrayList)" class="headerlink" title="四:动态数组(ArrayList)"></a>四:动态数组(ArrayList)</h2><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token operator">*</span></span><span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ArrayListExample</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//初始化空的list</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> a1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation"><</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//另一种初始化方式,不过是List,不是ArrayList</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> a2 <span class="token operator">=</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">asList</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//填充</span> <span class="token class-name">Collections</span><span class="token punctuation">.</span><span class="token function">fill</span><span class="token punctuation">(</span>a2<span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//将a2全部元素设置为100</span> <span class="token comment">//增删</span> a1<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//将4插入到第三个位置</span> a1<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//删除下标为0的</span> <span class="token comment">//遍历</span> <span class="token comment">//1.下标</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator"><</span>a1<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span> a1<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//访问下标为i的</span> <span class="token punctuation">}</span> <span class="token comment">//2.循环</span> <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> item<span class="token operator">:</span>a1<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token comment">//排序</span> <span class="token class-name">Collections</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>a2<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//排序</span> <span class="token class-name">Collections</span><span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span>a2<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//最大值</span> <span class="token class-name">Collections</span><span class="token punctuation">.</span><span class="token function">min</span><span class="token punctuation">(</span>a2<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//最小值</span> <span class="token comment">//翻转</span> <span class="token class-name">Collections</span><span class="token punctuation">.</span><span class="token function">reverse</span><span class="token punctuation">(</span>a2<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="五:栈Stack"><a href="#五:栈Stack" class="headerlink" title="五:栈Stack"></a>五:栈Stack</h2><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token operator">*</span></span><span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">StackExample</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">Stack</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> stack <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Stack</span><span class="token generics"><span class="token punctuation"><</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> stack<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> stack<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//移除堆栈顶部的对象,并作为此函数的值返回该对象</span> stack<span class="token punctuation">.</span><span class="token function">peek</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//查看堆栈顶部的对象,但不从堆栈中移除它</span> <span class="token comment">//注意:另一方面,不推荐使用Stack(),应该使用Deque(双端队列)</span> <span class="token class-name">Deque</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> stack1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayDeque</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> stack1<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> stack1<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> stack1<span class="token punctuation">.</span><span class="token function">peek</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="六:队列Queue"><a href="#六:队列Queue" class="headerlink" title="六:队列Queue"></a>六:队列Queue</h2><h3 id="单向队列Queue"><a href="#单向队列Queue" class="headerlink" title="单向队列Queue"></a>单向队列Queue</h3><pre class="line-numbers language-none"><code class="language-none">Queue<Integer> q = new LinkedList<>(); //实例化 offer(E e); // 队尾加入元素e。 若成功入队返回值true,否则返回false --- O(1) poll(); // 出队头,返回出队元素e --- O(1) peek(); // 查看队头元素, 返回值队首元素e --- O(1) isEmpty() // 若队空返回true, 否则返回false --- O(1) size() // 返回队中元素个数 --- O(1)<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Queue</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> queue <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayDeque</span><span class="token generics"><span class="token punctuation"><</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//Queue<Integer> q = new LinkedList<>();</span>queue<span class="token punctuation">.</span><span class="token function">offer</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token keyword">int</span> a <span class="token operator">=</span> queue<span class="token punctuation">.</span><span class="token function">peek</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//返回第一个元素</span><span class="token keyword">int</span> b <span class="token operator">=</span> queue<span class="token punctuation">.</span><span class="token function">poll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//删除并返回第一个元素</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="双向队列Deque"><a href="#双向队列Deque" class="headerlink" title="双向队列Deque"></a>双向队列Deque</h3><p>Queue有一个子接口<code>Dueue</code>,即双向队列,和单向队列不同,它的出队入队可以从两个方向。</p><pre class="line-numbers language-none"><code class="language-none">Dueue<Integer> q = new LinkedList<>(); //实例化 offFirst(Object e) // 将指定元素添加到双端队列的头部 --- O(1) offLast(Object e) //将指定元素添加到双端队列的尾部 --- O(1) pollFirst() //获取并删除双端队列的第一个元素 --- O(1) pollLast() //获取并删除双端队列的最后一个元素 --- O(1) peekFirst() //获取但不删除双端队列的第一个元素 --- O(1) peekLast() //获取但不删除双端队列的最后一个元素 --- O(1) isEmpty() //非空返回true,否则返回false size() //队列中元素个数<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="七:Map-对象"><a href="#七:Map-对象" class="headerlink" title="七:Map 对象"></a>七:Map 对象</h2><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token operator">*</span></span><span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MapExample</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">String</span><span class="token punctuation">,</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> map <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation"><</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> map<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"Linda"</span><span class="token punctuation">,</span><span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//增加</span> <span class="token keyword">boolean</span> isContain <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">containsKey</span><span class="token punctuation">(</span><span class="token string">"Linda"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//时间复杂度O(1)</span> <span class="token keyword">boolean</span> isContain2 <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">containsValue</span><span class="token punctuation">(</span><span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> age <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">"Linda"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//18</span> <span class="token keyword">int</span> age2 <span class="token operator">=</span> map<span class="token punctuation">.</span><span class="token function">getOrDefault</span><span class="token punctuation">(</span><span class="token string">"Bob"</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//含有默认值</span> map<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token string">"Linda"</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//移除</span> <span class="token comment">//遍历map键值对</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Map<span class="token punctuation">.</span>Entry</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">String</span><span class="token punctuation">,</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> entry<span class="token operator">:</span>map<span class="token punctuation">.</span><span class="token function">entrySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">String</span> key <span class="token operator">=</span> entry<span class="token punctuation">.</span><span class="token function">getKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Integer</span> value <span class="token operator">=</span> entry<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//遍历key</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">String</span> key<span class="token operator">:</span>map<span class="token punctuation">.</span><span class="token function">keySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>key<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//遍历value</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Integer</span> value<span class="token operator">:</span>map<span class="token punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="八:Set-对象"><a href="#八:Set-对象" class="headerlink" title="八:Set 对象"></a>八:Set 对象</h2><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token operator">*</span></span><span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">SetExample</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">Set</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> set <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashSet</span><span class="token generics"><span class="token punctuation"><</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//创建</span> set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token number">23</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//增加</span> <span class="token keyword">boolean</span> isContain <span class="token operator">=</span> set<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//是否包含</span> set<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token number">23</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//移除</span> <span class="token comment">//遍历set</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Integer</span> item <span class="token operator">:</span> set<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">//清除</span> set<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//T</span><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token class-name">TreeSet</span></span><span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TreeSetExample</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">TreeSet</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> set <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">TreeSet</span><span class="token generics"><span class="token punctuation"><</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator"><</span> <span class="token number">12</span><span class="token punctuation">;</span> i <span class="token operator">+=</span> <span class="token number">2</span><span class="token punctuation">)</span> set<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">Integer</span> floor <span class="token operator">=</span> set<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//6,向下取整,包括自身 <=7</span> <span class="token class-name">Integer</span> ceiling <span class="token operator">=</span> set<span class="token punctuation">.</span><span class="token function">ceiling</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//6,向上取值,包括自身 >=5</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="九:优先队列PriorityQueue"><a href="#九:优先队列PriorityQueue" class="headerlink" title="九:优先队列PriorityQueue"></a>九:优先队列PriorityQueue</h2><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token function">offer</span><span class="token punctuation">(</span><span class="token class-name">E</span> e<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 在堆中加入元素e,并调整堆。若成功入堆返回值true,否则返回false --- O(logN)</span> <span class="token function">poll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 弹出堆顶元素,并重新调整堆,返回出队元素e --- O(logN)</span> <span class="token function">peek</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 查看堆顶元素, 返回值堆顶元素e --- O(1)j</span> <span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//判断队列是否为空</span> <span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//队列中元素数量</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>util<span class="token punctuation">.</span></span><span class="token operator">*</span></span><span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">PriorityQueueExample</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token class-name">PriorityQueue</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> minH <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PriorityQueue</span><span class="token generics"><span class="token punctuation"><</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//小根堆</span> <span class="token class-name">PriorityQueue</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> maxH <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PriorityQueue</span><span class="token generics"><span class="token punctuation"><</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span> <span class="token operator">-></span> b<span class="token operator">-</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//大根堆</span> <span class="token class-name">PriorityQueue</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span> pq2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PriorityQueue</span><span class="token generics"><span class="token punctuation"><</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Comparator</span><span class="token generics"><span class="token punctuation"><</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">compare</span><span class="token punctuation">(</span><span class="token class-name">Integer</span> o1<span class="token punctuation">,</span> <span class="token class-name">Integer</span> o2<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> o2 <span class="token operator">-</span> o1<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="十:保留小数有效数字(四舍五入)"><a href="#十:保留小数有效数字(四舍五入)" class="headerlink" title="十:保留小数有效数字(四舍五入)"></a>十:保留小数有效数字(四舍五入)</h2><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">import</span> <span class="token import"><span class="token namespace">java<span class="token punctuation">.</span>math<span class="token punctuation">.</span></span><span class="token operator">*</span></span><span class="token punctuation">;</span><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">BigDecimalExample</span> <span class="token punctuation">{</span> <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">double</span> a <span class="token operator">=</span> <span class="token number">2.5111</span><span class="token punctuation">;</span> <span class="token class-name">BigDecimal</span> decimal <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BigDecimal</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">setScale</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token class-name">RoundingMode</span><span class="token punctuation">.</span>HALF_UP<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>decimal<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h2 id="十一:长度计算"><a href="#十一:长度计算" class="headerlink" title="十一:长度计算"></a>十一:长度计算</h2><p>length属性是<strong>针对数组</strong>说的,比如说你声明了一个数组,想知道这个数组的长度则用到了length这个属性。<br>length()方法是<strong>针对字符串String</strong>说的,如果想看这个字符串的长度则用到length()这个方法。<br>size()方法是<strong>针对泛型集合泛型集合(Collection)如Set、List、Map</strong>说的,如果想看这个泛型有多少个元素,就调用此方法来查看。数组没有size()方法。</p><h2 id="工具类"><a href="#工具类" class="headerlink" title="工具类"></a>工具类</h2><h3 id="1、java-util-Math:主要包含数学内的应用方法"><a href="#1、java-util-Math:主要包含数学内的应用方法" class="headerlink" title="1、java.util.Math:主要包含数学内的应用方法"></a>1、java.util.Math:主要包含数学内的应用方法</h3><p>(1)Math.abs(参数)</p><p>作用:返回参数的绝对值</p><p>参数可以是 int,float,long,double等类型,返回值类型同参数类型。(参数也可以是short,byte,但是返回类型会转成int)</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Math</span><span class="token punctuation">.</span><span class="token function">abs</span><span class="token punctuation">(</span><span class="token keyword">double</span> a<span class="token punctuation">)</span><span class="token punctuation">;</span> <span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>(2)Math.max(参数1,参数2),Math.min(参数1,参数2)</p><p>作用:返回两个参数的最大值,最小值。</p><p>参数可以是 int,float,long,double等类型,返回值类型同参数类型。(参数也可以是short,byte,但是返回类型会转成int)</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Math</span><span class="token punctuation">.</span><span class="token function">max</span><span class="token punctuation">(</span><span class="token keyword">long</span> a<span class="token punctuation">,</span> <span class="token keyword">long</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//返回两个参数中较大的值</span><span class="token class-name">Math</span><span class="token punctuation">.</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token keyword">long</span> a<span class="token punctuation">,</span> <span class="token keyword">long</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//返回两个参数中较小的值</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>(3)Math.sqrt(参数)、Math.cbrt(参数)</p><p>作用:开平方,开立方</p><p>参数可以是 int,float,long,double,short,byte等类型,返回值类型为double。</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Math</span><span class="token punctuation">.</span><span class="token function">sqrt</span><span class="token punctuation">(</span><span class="token keyword">long</span> a<span class="token punctuation">)</span> <span class="token comment">//开平方</span><span class="token class-name">Math</span><span class="token punctuation">.</span><span class="token function">cbrt</span><span class="token punctuation">(</span><span class="token keyword">long</span> b<span class="token punctuation">)</span> <span class="token comment">//开立方</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>(4)Math.pow(参数1,参数2)</p><p>作用:返回参数1的参数2次方。</p><p>参数可以是 int,float,long,double,short,byte等类型,返回值类型为double。</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Math</span><span class="token punctuation">.</span><span class="token function">pow</span><span class="token punctuation">(</span><span class="token keyword">double</span> a<span class="token punctuation">,</span> <span class="token keyword">double</span> b<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>(5)Math.ceil(参数)</p><p>作用:对浮点数向上取整。</p><p>参数可以是 int,float,long,double,short,byte等类型,返回值类型为double。</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Math</span><span class="token punctuation">.</span><span class="token function">ceil</span><span class="token punctuation">(</span><span class="token keyword">double</span> a<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>(6)Math.floor(参数)</p><p>作用:对浮点数向下取整。</p><p>参数可以是 int,float,long,double,short,byte等类型,返回值类型为double。</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Math</span><span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span><span class="token keyword">double</span> a<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p> (7)Math.round(参数)</p><p>作用:对浮点数四舍五入。</p><p>参数可以是 int,float,long,double,short,byte等类型,返回值类型为int或者long。</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Math</span><span class="token punctuation">.</span><span class="token function">round</span><span class="token punctuation">(</span><span class="token keyword">double</span> a<span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>(8)Math.log(参数),Math.log10(参数)</p><p>作用:取对数。</p><p>参数可以是 int,float,long,double,short,byte等类型,返回值类型为double。</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Math</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">double</span> a<span class="token punctuation">)</span> <span class="token comment">//以e为底取对数</span><span class="token class-name">Math</span><span class="token punctuation">.</span><span class="token function">log10</span><span class="token punctuation">(</span><span class="token keyword">double</span> b<span class="token punctuation">)</span> <span class="token comment">//以10为底取对数</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p> 那么,以m为底取N的对数该如何求呢?</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token comment">//注意:logmN:以m为底取N的对数。</span><span class="token class-name">Math</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token operator">/</span><span class="token class-name">Math</span><span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//该代码意为:以2为底取8的对数为:3。</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>(9)Math.random()</p><p>作用:返回一个随机数,随机数范围为[0,1),左闭右开。返回值类型为double。</p><h3 id="2、java-util-Arrays:主要包含了操作数组的各种方法"><a href="#2、java-util-Arrays:主要包含了操作数组的各种方法" class="headerlink" title="2、java.util.Arrays:主要包含了操作数组的各种方法"></a>2、java.util.Arrays:主要包含了操作数组的各种方法</h3><p>(1)Arrays.toString(arr)</p><p>作用:返回字符串,便于打印数组内容</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Arrays类的toString()方法能将数组中的内容全部打印出来</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>(2)Arrays.sort(arr,fromIndex,toIndex)</p><p>作用:数组升序</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> a <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//默认对全部元素升序,对下标0到下标为5-1的5个元素进行排序。</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>(3)Arrays.sort(arr,fromIndex,toIndex,Collections.reverseOrder())</p><p>作用:数组降序</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token class-name">Collections</span><span class="token punctuation">.</span><span class="token function">reverseOrder</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p> (4)Arrays.equals()</p><p>作用:比较数组元素</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr1 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr2 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>arr1<span class="token punctuation">,</span>arr2<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//输出:true</span><span class="token comment">//如果是arr1.equals(arr2),则返回false,因为equals比较的是两个对象的地址,不是里面的数,而Arrays.equals重写了equals,所以,这里能比较元素是否相等。</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre><p>(5)Arrays.copeOf() 和Arrays.copeOfRange() </p><p>作用:截取数组,传回的数组是新的数组对象,改变传回数组中的元素值,不会影响原来的数组。</p><pre class="line-numbers language-java" data-language="java"><code class="language-java"><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">,</span><span class="token number">30</span><span class="token punctuation">,</span><span class="token number">40</span><span class="token punctuation">,</span><span class="token number">50</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> arr1 <span class="token operator">=</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">copyOf</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>arr1<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Arrays类的toString()方法能将数组中的内容全部打印出来</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//输出:[10, 20, 30] (截取arr数组的3个元素赋值给新数组arr1)</span><span class="token keyword">int</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>arr <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">,</span><span class="token number">30</span><span class="token punctuation">,</span><span class="token number">40</span><span class="token punctuation">,</span><span class="token number">50</span><span class="token punctuation">}</span><span class="token punctuation">;</span><span class="token keyword">int</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>arr1 <span class="token operator">=</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">copyOfRange</span><span class="token punctuation">(</span>arr<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token class-name">String</span> str <span class="token operator">=</span> <span class="token class-name">Arrays</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span>arr1<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Arrays类的toString()方法能将数组中的内容全部打印出来</span><span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//输出:[20, 30] (从第1位(index=0开始)截取到第3位(不包括)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="3、java-util-Collections:主要对集合进行操作"><a href="#3、java-util-Collections:主要对集合进行操作" class="headerlink" title="3、java.util.Collections:主要对集合进行操作"></a>3、java.util.Collections:主要对集合进行操作</h3><p>(1)Collections.sort()</p><p>作用:升序/降序</p><h3 id="4、基本类型的最大值与最小值"><a href="#4、基本类型的最大值与最小值" class="headerlink" title="4、基本类型的最大值与最小值"></a>4、基本类型的最大值与最小值</h3><pre class="line-numbers language-java" data-language="java"><code class="language-java">fmax <span class="token operator">=</span> <span class="token class-name">Float</span><span class="token punctuation">.</span>MAX_VALUE<span class="token punctuation">;</span>fmin <span class="token operator">=</span> <span class="token class-name">Float</span><span class="token punctuation">.</span>MIN_VALUE<span class="token punctuation">;</span>dmax <span class="token operator">=</span> <span class="token class-name">Double</span><span class="token punctuation">.</span>MAX_VALUE<span class="token punctuation">;</span>dmin <span class="token operator">=</span> <span class="token class-name">Double</span><span class="token punctuation">.</span>MIN_VALUE<span class="token punctuation">;</span>bmax <span class="token operator">=</span> <span class="token class-name">Byte</span><span class="token punctuation">.</span>MAX_VALUE<span class="token punctuation">;</span>bmin <span class="token operator">=</span> <span class="token class-name">Byte</span><span class="token punctuation">.</span>MIN_VALUE<span class="token punctuation">;</span>cmax <span class="token operator">=</span> <span class="token class-name">Character</span><span class="token punctuation">.</span>MAX_VALUE<span class="token punctuation">;</span>cmin <span class="token operator">=</span> <span class="token class-name">Character</span><span class="token punctuation">.</span>MIN_VALUE<span class="token punctuation">;</span>shmax <span class="token operator">=</span> <span class="token class-name">Short</span><span class="token punctuation">.</span>MAX_VALUE<span class="token punctuation">;</span>shmin <span class="token operator">=</span> <span class="token class-name">Short</span><span class="token punctuation">.</span>MIN_VALUE<span class="token punctuation">;</span>imax <span class="token operator">=</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span>MAX_VALUE<span class="token punctuation">;</span>imin <span class="token operator">=</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span>MIN_VALUE<span class="token punctuation">;</span>lmax <span class="token operator">=</span> <span class="token class-name">Long</span><span class="token punctuation">.</span>MAX_VALUE<span class="token punctuation">;</span>lmin <span class="token operator">=</span> <span class="token class-name">Long</span><span class="token punctuation">.</span>MIN_VALUE<span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>]]></content>
<categories>
<category> 小知识 </category>
</categories>
<tags>
<tag> 算法 </tag>
<tag> 小知识 </tag>
<tag> Java </tag>
</tags>
</entry>
<entry>
<title>二分算法模板</title>
<link href="/2024/05/07/er-fen-suan-fa-mo-ban/"/>
<url>/2024/05/07/er-fen-suan-fa-mo-ban/</url>
<content type="html"><![CDATA[<h2 id="一、整数二分"><a href="#一、整数二分" class="headerlink" title="一、整数二分"></a><strong>一、整数二分</strong></h2><p>二分的本质是<strong>查找边界</strong>,整数区间一分为二成两个区间,一部分满足性质1一部分满足性质2(如:左半边满足性质1,右半边满足性质2),那么既可以寻找性质1的边界,也可以寻找性质2的边界</p><p><strong>每次将区间缩小一半,选择答案所在的区间</strong></p><p><strong>二分的本质不是单调性。有单调性的题目可以二分,但是可以二分的题目不一定有单调性</strong></p><p>代码模板如下:</p><pre class="line-numbers language-c++" data-language="c++"><code class="language-c++">// 代码随想录写法,左闭右闭区间int search(int left,int right){ while (left <= right) { // 当left==right,区间[left, right]依然有效,所以用 <= int middle = left + ((right - left) / 2);// 防止溢出 等同于(left + right)/2 if (nums[middle] > target) { right = middle - 1; // target 在左区间,所以[left, middle - 1] } else if (nums[middle] < target) { left = middle + 1; // target 在右区间,所以[middle + 1, right] } else { // nums[middle] == target return middle; // 数组中找到目标值,直接返回下标 } }}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>题目示例:1,2,2,3,3,5找出3的起始位置和终止位置</p><p>例题:<a href="https://leetcode.cn/problems/find-first-and-last-position-of-element-in-sorted-array/description/">34.在排序数组中查找元素的第一个和最后一个位置</a></p><h2 id="二、浮点数二分"><a href="#二、浮点数二分" class="headerlink" title="二、浮点数二分"></a><strong>二、浮点数二分</strong></h2><p>直接取mid判断落在哪个区间上,更新区间不断重复,直到精度足够为止(如:r - l < 1e-6)</p><pre class="line-numbers language-c++" data-language="c++"><code class="language-c++">bool check(double x){/* 检查x是否符合性质 */}double bsearch(double l, double r){ const double eps = 1e-6; while(r - l > eps){ // eps 表示精度,取决于题目对精度的要求 double mid = (l + r) / 2; // 不断二分 if(check(mid)){ r = mid; }else{ l = mid; } } return 1;}<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>]]></content>
<categories>
<category> 算法 </category>
</categories>
<tags>
<tag> 二分算法 </tag>
<tag> 整数二分 </tag>
<tag> 浮点数二分 </tag>
</tags>
</entry>
<entry>
<title>【sysbench】OLTP基准测试使用</title>
<link href="/2024/04/19/sysbench-oltp-ji-zhun-ce-shi-shi-yong/"/>
<url>/2024/04/19/sysbench-oltp-ji-zhun-ce-shi-shi-yong/</url>
<content type="html"><![CDATA[<blockquote><p>MySQL版本:8.0.36</p><p>sysbench版本:1.0.20</p><p>操作的数据库名称:sysbench_test</p></blockquote><h2 id="1、创建sysbench-oltp测试的基础配置文件"><a href="#1、创建sysbench-oltp测试的基础配置文件" class="headerlink" title="1、创建sysbench oltp测试的基础配置文件"></a>1、创建sysbench oltp测试的基础配置文件</h2><p>一般使用sysbench测试MySQL性能的命令会很长,如以下命令:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">sysbench --threads<span class="token operator">=</span><span class="token number">16</span> --time<span class="token operator">=</span><span class="token number">60</span> --report-interval<span class="token operator">=</span><span class="token number">1</span> oltp_common.lua --mysql-host<span class="token operator">=</span>localhost --mysql-port<span class="token operator">=</span><span class="token number">3306</span> --mysql-user<span class="token operator">=</span>root --mysql-db<span class="token operator">=</span>sysbench_test --tables<span class="token operator">=</span><span class="token number">4</span> --table-size<span class="token operator">=</span><span class="token number">100000</span> prepare<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>需要指定很多参数。如果有配置文件的话可以简化操作。</p><p>创建基础配置文件my-sysbench.cnf,并写入一些内容(仅供参考):</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token assign-left variable">threads</span><span class="token operator">=</span><span class="token number">160</span><span class="token assign-left variable">time</span><span class="token operator">=</span><span class="token number">120</span>db-driver<span class="token operator">=</span>mysqlmysql-host<span class="token operator">=</span>localhostmysql-port<span class="token operator">=</span><span class="token number">3306</span>mysql-socket<span class="token operator">=</span>/home/czhang/mysql-8.0.36/mysql.sockmysql-user<span class="token operator">=</span>rootmysql-db<span class="token operator">=</span>sysbench_test<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>注意:在配置文件中不要指定 tables 等选项,因为这个参数时Lua脚本的参数,不是sysbench可执行文件的参数,所以该参数要在命令行中指定。</p><p>使用sysbench测试MySQL主要有三步:<br><strong>准备测试数据—>运行测试—>测试数据清理</strong></p><h2 id="2、准备测试数据"><a href="#2、准备测试数据" class="headerlink" title="2、准备测试数据"></a>2、准备测试数据</h2><p>我使用8张表,每张表包含10000条数据,表名使用默认值。命令执行目录是sysbench的Lua脚本所在目录。</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">sysbench --config-file<span class="token operator">=</span>/home/czhang/mysql-8.0.36/my-sysbench.cnf --tables<span class="token operator">=</span><span class="token number">8</span> --table-size<span class="token operator">=</span><span class="token number">10000</span> oltp_common.lua prepare<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>终端显示如下:<br><img src="/2024/04/19/sysbench-oltp-ji-zhun-ce-shi-shi-yong/prepare%E7%BB%93%E6%9E%9C%E6%98%BE%E7%A4%BA.png" alt="prepare结果显示"></p><h2 id="3、运行测试"><a href="#3、运行测试" class="headerlink" title="3、运行测试"></a>3、运行测试</h2><h3 id="1)oltp-read-write"><a href="#1)oltp-read-write" class="headerlink" title="1)oltp_read_write"></a>1)oltp_read_write</h3><p>建议先对支持 <code>prewarm</code> 选项的 Lua 脚本使用 <code>prewarm</code> 选项,以预热数据到内存,提高性能。</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">sysbench --config-file<span class="token operator">=</span>/home/czhang/mysql-8.0.36/my-sysbench.cnf --tables<span class="token operator">=</span><span class="token number">8</span> --table-size<span class="token operator">=</span><span class="token number">10000</span> oltp_read_write.lua prewarm<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>预热后再运行该脚本</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">sysbench --config-file<span class="token operator">=</span>/home/czhang/mysql-8.0.36/my-sysbench.cnf --tables<span class="token operator">=</span><span class="token number">8</span> --table-size<span class="token operator">=</span><span class="token number">10000</span> oltp_read_write.lua run<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h3 id="2)oltp-point-select"><a href="#2)oltp-point-select" class="headerlink" title="2)oltp_point_select"></a>2)oltp_point_select</h3><pre class="line-numbers language-none"><code class="language-none">sysbench --config-file=/home/czhang/mysql-8.0.36/my-sysbench.cnf --tables=8 --table-size=10000 oltp_point_select.lua run<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h3 id="3)oltp-write-only"><a href="#3)oltp-write-only" class="headerlink" title="3)oltp_write_only"></a>3)oltp_write_only</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">sysbench --config-file<span class="token operator">=</span>/home/czhang/mysql-8.0.36/my-sysbench.cnf --tables<span class="token operator">=</span><span class="token number">8</span> --table-size<span class="token operator">=</span><span class="token number">10000</span> oltp_write_only.lua run<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h3 id="4)oltp-read-only"><a href="#4)oltp-read-only" class="headerlink" title="4)oltp_read_only"></a>4)oltp_read_only</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">sysbench --config-file<span class="token operator">=</span>/home/czhang/mysql-8.0.36/my-sysbench.cnf --tables<span class="token operator">=</span><span class="token number">8</span> --table-size<span class="token operator">=</span><span class="token number">10000</span> oltp_read_only.lua run<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>……</p><p>还有一些脚本没有运行,把命令行中的脚本名称替换即可。</p><h2 id="4、清理数据"><a href="#4、清理数据" class="headerlink" title="4、清理数据"></a>4、清理数据</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">sysbench --config-file<span class="token operator">=</span>/home/czhang/mysql-8.0.36/my-sysbench.cnf --tables<span class="token operator">=</span><span class="token number">8</span> --table-size<span class="token operator">=</span><span class="token number">10000</span> oltp_common.lua cleanup<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p><strong>注意:</strong>清理数据时除了将prepare改成cleanup,其他全都得一样</p><h2 id="5、常用MySQL命令"><a href="#5、常用MySQL命令" class="headerlink" title="5、常用MySQL命令"></a>5、常用MySQL命令</h2><p>使用sysbench对MySQL进行基准测试时可能用得上的MySQL命令:</p><h3 id="5-1-查看指定数据库的容量大小"><a href="#5-1-查看指定数据库的容量大小" class="headerlink" title="5.1 查看指定数据库的容量大小"></a>5.1 查看指定数据库的容量大小</h3><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token function">SUM</span><span class="token punctuation">(</span>data_length <span class="token operator">+</span> index_length<span class="token punctuation">)</span> <span class="token keyword">AS</span> total_size_bytes<span class="token punctuation">,</span>CONCAT<span class="token punctuation">(</span><span class="token function">ROUND</span><span class="token punctuation">(</span><span class="token function">SUM</span><span class="token punctuation">(</span>data_length <span class="token operator">+</span> index_length<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">1024</span> <span class="token operator">/</span> <span class="token number">1024</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">' MB'</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> total_size_mb <span class="token keyword">FROM</span> information_schema<span class="token punctuation">.</span><span class="token keyword">TABLES</span> <span class="token keyword">WHERE</span> table_schema <span class="token operator">=</span> <span class="token string">'sysbench_test'</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>显示结果示例如下:<br><img src="/2024/04/19/sysbench-oltp-ji-zhun-ce-shi-shi-yong/Blog\source_posts\数据库容量大小.png"></p><h3 id="5-2-查看指定数据库中各表的大小"><a href="#5-2-查看指定数据库中各表的大小" class="headerlink" title="5.2 查看指定数据库中各表的大小"></a>5.2 查看指定数据库中各表的大小</h3><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">SELECT</span> table_name<span class="token punctuation">,</span>data_length <span class="token operator">+</span> index_length <span class="token keyword">AS</span> size_bytes<span class="token punctuation">,</span>CONCAT<span class="token punctuation">(</span><span class="token function">ROUND</span><span class="token punctuation">(</span><span class="token punctuation">(</span>data_length <span class="token operator">+</span> index_length<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">1024</span> <span class="token operator">/</span> <span class="token number">1024</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">' MB'</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> size_mb <span class="token keyword">FROM</span> information_schema<span class="token punctuation">.</span><span class="token keyword">TABLES</span> <span class="token keyword">WHERE</span> table_schema <span class="token operator">=</span> <span class="token string">'sysbench_test'</span> <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> size_bytes <span class="token keyword">DESC</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>显示结果示例如下:</p><p><img src="/2024/04/19/sysbench-oltp-ji-zhun-ce-shi-shi-yong/%E6%95%B0%E6%8D%AE%E5%BA%93%E5%90%84%E8%A1%A8%E5%A4%A7%E5%B0%8F.png" alt="数据库各表大小"></p><h3 id="5-3-查看数据库各表中的记录数量"><a href="#5-3-查看数据库各表中的记录数量" class="headerlink" title="5.3 查看数据库各表中的记录数量"></a>5.3 查看数据库各表中的记录数量</h3><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">SELECT</span> t1<span class="token punctuation">.</span>table_name<span class="token punctuation">,</span> <span class="token keyword">COALESCE</span><span class="token punctuation">(</span>t2<span class="token punctuation">.</span>row_count<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> record_count<span class="token keyword">FROM</span> information_schema<span class="token punctuation">.</span><span class="token keyword">tables</span> t1<span class="token keyword">LEFT</span> <span class="token keyword">JOIN</span> <span class="token punctuation">(</span> <span class="token keyword">SELECT</span> table_name<span class="token punctuation">,</span> table_rows <span class="token keyword">AS</span> row_count <span class="token keyword">FROM</span> information_schema<span class="token punctuation">.</span><span class="token keyword">tables</span> <span class="token keyword">WHERE</span> table_schema <span class="token operator">=</span> <span class="token string">'sysbench_test'</span> <span class="token operator">AND</span> table_type <span class="token operator">=</span> <span class="token string">'BASE TABLE'</span><span class="token punctuation">)</span> t2 <span class="token keyword">ON</span> t1<span class="token punctuation">.</span>table_name <span class="token operator">=</span> t2<span class="token punctuation">.</span>table_name<span class="token keyword">WHERE</span> t1<span class="token punctuation">.</span>table_schema <span class="token operator">=</span> <span class="token string">'sysbench_test'</span> <span class="token operator">AND</span> t1<span class="token punctuation">.</span>table_type <span class="token operator">=</span> <span class="token string">'BASE TABLE'</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>显示结果示例如下:</p><p><img src="/2024/04/19/sysbench-oltp-ji-zhun-ce-shi-shi-yong/%E6%95%B0%E6%8D%AE%E5%BA%93%E5%90%84%E8%A1%A8%E8%AE%B0%E5%BD%95%E6%95%B0%E5%A4%A7%E5%B0%8F.png"></p><h2 id="6、常用cmd命令"><a href="#6、常用cmd命令" class="headerlink" title="6、常用cmd命令"></a>6、常用cmd命令</h2><p>显示包含“mysql”的进程</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">ps</span> -eo pid,psr,user,cmd <span class="token operator">|</span> <span class="token function">grep</span> mysql<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p><strong>Reference:</strong></p><p><a href="https://blog.csdn.net/wudi53433927/article/details/136946422">sysbench oltp测试示例</a></p>]]></content>
<categories>
<category> 工作负载 </category>
</categories>
<tags>
<tag> MySQL </tag>
<tag> 工作负载 </tag>
<tag> sysbench </tag>
<tag> 基准测试 </tag>
</tags>
</entry>
<entry>
<title>Linux普通用户无root权限安装MySQL 8.0.36</title>
<link href="/2024/04/17/linux-pu-tong-yong-hu-wu-root-quan-xian-an-zhuang-mysql-8-0-36/"/>
<url>/2024/04/17/linux-pu-tong-yong-hu-wu-root-quan-xian-an-zhuang-mysql-8-0-36/</url>
<content type="html"><![CDATA[<blockquote><p>Linux版本:Ubuntu 22.04 </p><p>MySQL版本:8.0.36</p><p>用户名:czhang</p><p>安装路径:/home/czhang/mysql-8.0.36</p></blockquote><h2 id="一、下载安装包"><a href="#一、下载安装包" class="headerlink" title="一、下载安装包"></a>一、下载安装包</h2><p>进入MySQL官网(<a href="https://dev.mysql.com/downloads/mysql/">MySQL下载官网</a> )下载社区通用版 MySQL Community Server</p><p><img src="/2024/04/17/linux-pu-tong-yong-hu-wu-root-quan-xian-an-zhuang-mysql-8-0-36/MySQL%E5%AE%98%E7%BD%91%E6%88%AA%E5%9B%BE.png" alt="MySQL官网截图"></p><h2 id="二、解压并安装"><a href="#二、解压并安装" class="headerlink" title="二、解压并安装"></a>二、解压并安装</h2><h3 id="1、上传MySQL到服务器并解压:"><a href="#1、上传MySQL到服务器并解压:" class="headerlink" title="1、上传MySQL到服务器并解压:"></a>1、上传MySQL到服务器并解压:</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ <span class="token function">tar</span> -xvf mysql-8.0.36-linux-glibc2.28-x86_64.tar.xz<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>将所有文件放在了 /home/czhang/mysql-8.0.36 路径下,文件名太长可以进行重命名</p><h3 id="2、创建编辑my-cnf配置文件(最初没有该文件)"><a href="#2、创建编辑my-cnf配置文件(最初没有该文件)" class="headerlink" title="2、创建编辑my.cnf配置文件(最初没有该文件)"></a>2、创建编辑my.cnf配置文件(最初没有该文件)</h3><p>在当前MySQL安装目录下,依次执行以下命令:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ <span class="token builtin class-name">cd</span> mysql-8.0.36$ <span class="token function">vim</span> my.cnf <span class="token comment">#编辑配置文件</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><h3 id="3、编辑my-cnf文件"><a href="#3、编辑my-cnf文件" class="headerlink" title="3、编辑my.cnf文件"></a>3、编辑my.cnf文件</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token punctuation">[</span>client<span class="token punctuation">]</span><span class="token assign-left variable">port</span><span class="token operator">=</span><span class="token number">3306</span> <span class="token comment">#服务端口</span><span class="token assign-left variable">socket</span><span class="token operator">=</span>/home/czhang/mysql-8.0.36/mysql.sock <span class="token comment">#指定套接字文件</span><span class="token punctuation">[</span>mysqld<span class="token punctuation">]</span><span class="token comment"># skip-grant-tables #跳过root的密码验证</span><span class="token assign-left variable">port</span><span class="token operator">=</span><span class="token number">3306</span> <span class="token comment">#服务端口</span><span class="token assign-left variable">basedir</span><span class="token operator">=</span>/home/czhang/mysql-8.0.36 <span class="token comment">#mysql安装路径</span><span class="token assign-left variable">datadir</span><span class="token operator">=</span>/home/czhang/mysql-8.0.36/data <span class="token comment">#数据目录</span>pid-file<span class="token operator">=</span>/home/czhang/mysql-8.0.36/mysql.pid <span class="token comment">#指定pid文件</span><span class="token assign-left variable">socket</span><span class="token operator">=</span>/home/czhang/mysql-8.0.36/mysql.sock <span class="token comment">#指定套接字文件</span><span class="token assign-left variable">log_error</span><span class="token operator">=</span>/home/czhang/mysql-8.0.36/error.log <span class="token comment">#指定错误日志</span>server-id<span class="token operator">=</span><span class="token number">100</span> <span class="token comment">#Mysql主从唯一标识</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="4、安装启动MySQL"><a href="#4、安装启动MySQL" class="headerlink" title="4、安装启动MySQL"></a>4、安装启动MySQL</h3><h4 id="4-1-安装"><a href="#4-1-安装" class="headerlink" title="4.1 安装"></a>4.1 安装</h4><p>依次执行以下命令,指定配置文件安装并初始化mysql,没有报错即安装成功,返回了任何信息即为安装失败</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ <span class="token builtin class-name">cd</span> bin$ ./mysqld --defaults-file<span class="token operator">=</span>/home/czhang/mysql-8.0.36/my.cnf --initialize --user<span class="token operator">=</span>czhang --basedir<span class="token operator">=</span>/home/czhang/mysql-8.0.36 --datadir<span class="token operator">=</span>/home/czhang/mysql-8.0.36/data<span class="token comment">#安装并初始化mysql</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><h4 id="4-2-启动"><a href="#4-2-启动" class="headerlink" title="4.2 启动"></a>4.2 启动</h4><p>依次执行以下命令,没有报错并能成功监听3306端口即表示启动成功</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ ./mysqld_safe --defaults-file<span class="token operator">=</span>/home/czhang/mysql-8.0.36/my.cnf --user<span class="token operator">=</span>czhang <span class="token operator">&</span> <span class="token comment">#启动mysql</span>$ <span class="token function">netstat</span> -tln <span class="token operator">|</span> <span class="token function">grep</span> <span class="token number">3306</span> <span class="token comment">#查看是否成功监听3306端口</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><h2 id="四、登录MySQL"><a href="#四、登录MySQL" class="headerlink" title="四、登录MySQL"></a>四、登录MySQL</h2><h3 id="1、获取初始密码"><a href="#1、获取初始密码" class="headerlink" title="1、获取初始密码"></a>1、获取初始密码</h3><p>初始密码在安装根目录下的error.log文件中</p><h3 id="2、登录MySQL"><a href="#2、登录MySQL" class="headerlink" title="2、登录MySQL"></a>2、登录MySQL</h3><p>登录时需要指定mysql.sock文件启动,不然会报错。执行以下命令:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ ./mysql -u root -p -S /home/czhang/mysql-8.0.36/mysql.sock <span class="token comment">#指定sock文件登录</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><h3 id="3、修改密码"><a href="#3、修改密码" class="headerlink" title="3、修改密码"></a>3、修改密码</h3><p>成功登入mysql后,修改登录密码,执行以下sql语句:</p><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">USE</span> mysql<span class="token punctuation">;</span><span class="token keyword">ALTER</span> <span class="token keyword">USER</span> <span class="token string">'root'</span><span class="token variable">@'localhost'</span> IDENTIFIED <span class="token keyword">BY</span> <span class="token string">'123456'</span><span class="token punctuation">;</span> <span class="token comment">--设置登录密码为123456</span>flush <span class="token keyword">privileges</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre><h3 id="4、退出MySQL"><a href="#4、退出MySQL" class="headerlink" title="4、退出MySQL"></a>4、退出MySQL</h3><p>可以在终端中执行以下shell命令关闭MySQL:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">mysqladmin -u root <span class="token function">shutdown</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>或者在MySQL中执行SQL语句退出:</p><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">SHUTDOWN</span><span class="token punctuation">;</span> <span class="token keyword">EXIT</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>至此,MySQL安装完成</p><h2 id="五、常用MySQL命令"><a href="#五、常用MySQL命令" class="headerlink" title="五、常用MySQL命令"></a>五、常用MySQL命令</h2><p>使用sysbench对MySQL进行基准测试时可能用得上的MySQL命令:</p><h3 id="1-查看指定数据库的容量大小"><a href="#1-查看指定数据库的容量大小" class="headerlink" title="1 查看指定数据库的容量大小"></a>1 查看指定数据库的容量大小</h3><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token function">SUM</span><span class="token punctuation">(</span>data_length <span class="token operator">+</span> index_length<span class="token punctuation">)</span> <span class="token keyword">AS</span> total_size_bytes<span class="token punctuation">,</span>CONCAT<span class="token punctuation">(</span><span class="token function">ROUND</span><span class="token punctuation">(</span><span class="token function">SUM</span><span class="token punctuation">(</span>data_length <span class="token operator">+</span> index_length<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">1024</span> <span class="token operator">/</span> <span class="token number">1024</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">' MB'</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> total_size_mb <span class="token keyword">FROM</span> information_schema<span class="token punctuation">.</span><span class="token keyword">TABLES</span> <span class="token keyword">WHERE</span> table_schema <span class="token operator">=</span> <span class="token string">'sysbench_test'</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>显示结果示例如下:<br><img src="/2024/04/17/linux-pu-tong-yong-hu-wu-root-quan-xian-an-zhuang-mysql-8-0-36/%E6%95%B0%E6%8D%AE%E5%BA%93%E5%AE%B9%E9%87%8F%E5%A4%A7%E5%B0%8F.png" alt="数据库容量大小"></p><h3 id="2-查看指定数据库中各表的大小"><a href="#2-查看指定数据库中各表的大小" class="headerlink" title="2 查看指定数据库中各表的大小"></a>2 查看指定数据库中各表的大小</h3><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">SELECT</span> table_name<span class="token punctuation">,</span>data_length <span class="token operator">+</span> index_length <span class="token keyword">AS</span> size_bytes<span class="token punctuation">,</span>CONCAT<span class="token punctuation">(</span><span class="token function">ROUND</span><span class="token punctuation">(</span><span class="token punctuation">(</span>data_length <span class="token operator">+</span> index_length<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token number">1024</span> <span class="token operator">/</span> <span class="token number">1024</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">' MB'</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> size_mb <span class="token keyword">FROM</span> information_schema<span class="token punctuation">.</span><span class="token keyword">TABLES</span> <span class="token keyword">WHERE</span> table_schema <span class="token operator">=</span> <span class="token string">'sysbench_test'</span> <span class="token keyword">ORDER</span> <span class="token keyword">BY</span> size_bytes <span class="token keyword">DESC</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>显示结果示例如下:</p><p><img src="/2024/04/17/linux-pu-tong-yong-hu-wu-root-quan-xian-an-zhuang-mysql-8-0-36/%E6%95%B0%E6%8D%AE%E5%BA%93%E5%90%84%E8%A1%A8%E5%A4%A7%E5%B0%8F.png" alt="数据库各表大小"></p><h3 id="3-查看数据库各表中的记录数量"><a href="#3-查看数据库各表中的记录数量" class="headerlink" title="3 查看数据库各表中的记录数量"></a>3 查看数据库各表中的记录数量</h3><pre class="line-numbers language-sql" data-language="sql"><code class="language-sql"><span class="token keyword">SELECT</span> t1<span class="token punctuation">.</span>table_name<span class="token punctuation">,</span> <span class="token keyword">COALESCE</span><span class="token punctuation">(</span>t2<span class="token punctuation">.</span>row_count<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token keyword">AS</span> record_count<span class="token keyword">FROM</span> information_schema<span class="token punctuation">.</span><span class="token keyword">tables</span> t1<span class="token keyword">LEFT</span> <span class="token keyword">JOIN</span> <span class="token punctuation">(</span> <span class="token keyword">SELECT</span> table_name<span class="token punctuation">,</span> table_rows <span class="token keyword">AS</span> row_count <span class="token keyword">FROM</span> information_schema<span class="token punctuation">.</span><span class="token keyword">tables</span> <span class="token keyword">WHERE</span> table_schema <span class="token operator">=</span> <span class="token string">'sysbench_test'</span> <span class="token operator">AND</span> table_type <span class="token operator">=</span> <span class="token string">'BASE TABLE'</span><span class="token punctuation">)</span> t2 <span class="token keyword">ON</span> t1<span class="token punctuation">.</span>table_name <span class="token operator">=</span> t2<span class="token punctuation">.</span>table_name<span class="token keyword">WHERE</span> t1<span class="token punctuation">.</span>table_schema <span class="token operator">=</span> <span class="token string">'sysbench_test'</span> <span class="token operator">AND</span> t1<span class="token punctuation">.</span>table_type <span class="token operator">=</span> <span class="token string">'BASE TABLE'</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>显示结果示例如下:</p><p><img src="/2024/04/17/linux-pu-tong-yong-hu-wu-root-quan-xian-an-zhuang-mysql-8-0-36/%E6%95%B0%E6%8D%AE%E5%BA%93%E5%90%84%E8%A1%A8%E8%AE%B0%E5%BD%95%E6%95%B0%E5%A4%A7%E5%B0%8F.png" alt="数据库各表记录数大小"></p><h2 id="六、常用cmd命令"><a href="#六、常用cmd命令" class="headerlink" title="六、常用cmd命令"></a>六、常用cmd命令</h2><p>显示包含“mysql”的进程</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">ps</span> -eo pid,psr,user,cmd <span class="token operator">|</span> <span class="token function">grep</span> mysql<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p><strong>Reference</strong>:</p><p><a href="https://www.cnblogs.com/chenkx6/p/13366638.html">1</a></p><p><a href="https://cloud.tencent.com/developer/article/1583129">2</a></p><p><a href="https://blog.csdn.net/suixinfeixiangfei/article/details/121323984">3</a></p><p><a href="https://blog.csdn.net/Shadownow/article/details/115311933">4</a></p>]]></content>
<categories>
<category> 工作负载 </category>
</categories>
<tags>
<tag> MySQL </tag>
<tag> 工作负载 </tag>
<tag> 环境搭建 </tag>
</tags>
</entry>
<entry>
<title>NEMO海洋模型的安装、搭建与运行</title>
<link href="/2024/04/02/nemo-hai-yang-mo-xing-de-an-zhuang-da-jian-yu-yun-xing/"/>
<url>/2024/04/02/nemo-hai-yang-mo-xing-de-an-zhuang-da-jian-yu-yun-xing/</url>
<content type="html"><![CDATA[<p>笔者在安装构建NEMO(<strong>N</strong>ucleus for<strong>E</strong>uropean<strong>M</strong>odelling of the<strong>O</strong>cean)的时候苦于资料太少,花费了不少时间和精力,踩了不少坑。因此笔者将安装过程进行总结,以供大家参考。</p><h2 id="1-系统信息"><a href="#1-系统信息" class="headerlink" title="1. 系统信息"></a>1. 系统信息</h2><p>OS信息:Linux black 5.15.0-89-generic #99-Ubuntu x86_64 GNU/Linux</p><p>安装路径:软件安装包放在: /home/czhang/softwarepkg</p><p>相关依赖软件安装在:/home/czhang/local</p><p>NEMO安装在:/home/czhang/NEMO</p><p>XIOS安装在:/home/czhang/xios</p><blockquote><p>各软件安装版本如下:<br>curl-7.81.0<br>hdf5-1.14.3<br>mpich-4.2.0<br>netcdf-c-4.7.4<br>netcdf-fortran-4.5.3<br>pnetcdf-1.12.3<br>szip-2.1.1<br>zlib-1.2.11</p></blockquote><h2 id="2-安装MPICH"><a href="#2-安装MPICH" class="headerlink" title="2. 安装MPICH"></a>2. 安装MPICH</h2><p>在/home/czhang/local下创建mpich文件夹</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ <span class="token function">tar</span> -zxvf mpich-4.2.0.tar.gz$ <span class="token builtin class-name">cd</span> mpich-4.2.0$ ./configure --prefix<span class="token operator">=</span>/home/czhang/local/mpich$ <span class="token function">make</span>$ <span class="token function">make</span> <span class="token function">install</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>安装后需要将mpich的相应路径添加到环境变量当中。</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token builtin class-name">export</span> <span class="token assign-left variable">LD_LIBRARY_PATH</span><span class="token operator">=</span>/home/czhang/local/mpich/lib<span class="token builtin class-name">export</span> <span class="token assign-left variable"><span class="token environment constant">PATH</span></span><span class="token operator">=</span><span class="token environment constant">$PATH</span>:/home/czhang/local/mpich/bin<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>也可以在.bashrc中,添加随后 source ~/.bashrc</p><h2 id="3-安装NetCDF"><a href="#3-安装NetCDF" class="headerlink" title="3. 安装NetCDF"></a>3. 安装NetCDF</h2><p>在/home/czhang/local下创建netcdf文件夹。安装的每一个依赖软件均需在其源代码目录下进行</p><h3 id="1-安装zlib"><a href="#1-安装zlib" class="headerlink" title="(1)安装zlib"></a>(1)安装zlib</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ <span class="token function">tar</span> -zxvf zlib-1.2.11.tar.gz$ <span class="token builtin class-name">cd</span> zlib-1.2.11$ ./configure --prefix<span class="token operator">=</span>/home/czhang/local/netcdf$ <span class="token function">make</span>$ <span class="token function">make</span> <span class="token function">install</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="2-安装szip"><a href="#2-安装szip" class="headerlink" title="(2)安装szip"></a>(2)安装szip</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ <span class="token function">tar</span> -zxvf szip-2.1.1.tar.gz$ <span class="token builtin class-name">cd</span> szip-2.1.1$ ./configure --prefix<span class="token operator">=</span>/home/czhang/local/netcdf$ <span class="token function">make</span>$ <span class="token function">make</span> <span class="token function">install</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="3-安装curl"><a href="#3-安装curl" class="headerlink" title="(3)安装curl"></a>(3)安装curl</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ <span class="token function">tar</span> -zxvf curl-7.81.0.tar.gz$ <span class="token builtin class-name">cd</span> curl-7.81.0$ ./configure --prefix<span class="token operator">=</span>/home/czhang/local/netcdf -with-openssl <span class="token comment"># 这里要看自己系统上用的什么ssl</span>$ <span class="token function">make</span>$ <span class="token function">make</span> <span class="token function">install</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="4-安装HDF5"><a href="#4-安装HDF5" class="headerlink" title="(4)安装HDF5"></a>(4)安装HDF5</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ <span class="token function">tar</span> -zxvf hdf5-1.14.3.tar.gz$ <span class="token builtin class-name">cd</span> hdf5-1.14.3$ <span class="token builtin class-name">export</span> <span class="token assign-left variable">CPPFLAGS</span><span class="token operator">=</span>-I /home/czhang/local/netcdf/include$ <span class="token builtin class-name">export</span> <span class="token assign-left variable">LDFLAGS</span><span class="token operator">=</span>-L /home/czhang/local/netcdf/lib$ <span class="token builtin class-name">export</span> <span class="token assign-left variable">LD_LIBRARY_PATH</span><span class="token operator">=</span>/home/czhang/local/netcdf/lib$ ./configure --prefix<span class="token operator">=</span>/home/czhang/local/netcdf --enable-parallel --enable-fortran$ <span class="token function">make</span>$ <span class="token function">make</span> <span class="token function">install</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="5-安装PNetCDF"><a href="#5-安装PNetCDF" class="headerlink" title="(5)安装PNetCDF"></a>(5)安装PNetCDF</h3><p>在/home/czhang/local下创建pnetcdf文件夹,pnetcdf安装在该文件夹中</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ <span class="token function">tar</span> -zxvf pnetcdf-1.12.3.tar.gz$ <span class="token builtin class-name">cd</span> pnetcdf-1.12.3$ ./configure --prefix<span class="token operator">=</span>/home/czhang/local/pnetcdf --enable-fortran --enable-large-file-test --enable-shared$ <span class="token function">make</span>$ <span class="token function">make</span> <span class="token function">install</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><h3 id="6-安装NetCDF-C"><a href="#6-安装NetCDF-C" class="headerlink" title="(6)安装NetCDF-C"></a>(6)安装NetCDF-C</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ <span class="token function">tar</span> -zxvf netcdf-c-4.7.4.tar.gz$ <span class="token builtin class-name">cd</span> netcdf-c-4.7.4$ ./configure --prefix<span class="token operator">=</span>/home/czhang/local/netcdf --enable-pnetcdf --enable-large-file-tests --enable-largefile --enable-parallel-tests --enable-shared --with-pic --disable-doxygen --enable-netcdf-4 $ <span class="token function">make</span>$ <span class="token function">make</span> <span class="token function">install</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>成功后会有 congratulations</p><h3 id="7-安装NetCDF-Fortran"><a href="#7-安装NetCDF-Fortran" class="headerlink" title="(7)安装NetCDF-Fortran"></a>(7)安装NetCDF-Fortran</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ <span class="token function">tar</span> -zxvf netcdf-fortran-4.5.3.tar.gz <span class="token comment"># 注意:该版本需要netcdf-c在4.7.4及以上</span>$ <span class="token builtin class-name">cd</span> netcdf-fortran-4.5.3$ <span class="token builtin class-name">export</span> <span class="token assign-left variable">LIBS</span><span class="token operator">=</span>-lnetcdf$ ./configure --prefix<span class="token operator">=</span>/home/czhang/local/netcdf --disable-static --enable-shared --with-pic --enable-parallel-tests --enable-large-file-tests --enable-largefile $ <span class="token function">make</span>$ <span class="token function">make</span> <span class="token function">install</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>成功后会有 congratulations</p><p><strong>安装完成后需要配置环境变量</strong>,笔者在 .bashrc 文件添加的内容如下:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token builtin class-name">export</span> <span class="token assign-left variable">DIR</span><span class="token operator">=</span>/home/czhang/local<span class="token builtin class-name">export</span> <span class="token assign-left variable">NETCDF</span><span class="token operator">=</span><span class="token variable">$DIR</span>/netcdf<span class="token builtin class-name">export</span> <span class="token assign-left variable">MPI</span><span class="token operator">=</span><span class="token variable">$DIR</span>/mpich<span class="token builtin class-name">export</span> <span class="token assign-left variable">PNETCDF</span><span class="token operator">=</span><span class="token variable">$DIR</span>/pnetcdf<span class="token builtin class-name">export</span> <span class="token assign-left variable">CPPFLAGS</span><span class="token operator">=</span><span class="token string">"-I<span class="token variable">$MPI</span>/include -I<span class="token variable">$NETCDF</span>/include -I<span class="token variable">$PNETCDF</span>/include"</span><span class="token builtin class-name">export</span> <span class="token assign-left variable">LDFLAGS</span><span class="token operator">=</span><span class="token string">"<span class="token variable">$LDFLAGS</span> -L<span class="token variable">$NETCDF</span>/lib -L<span class="token variable">$MPI</span>/lib -L<span class="token variable">$PNETCDF</span>/lib -I<span class="token variable">$NETCDF</span>/include"</span><span class="token builtin class-name">export</span> <span class="token assign-left variable">FCFLAGS</span><span class="token operator">=</span><span class="token string">"-fallow-argument-mismatch"</span><span class="token builtin class-name">export</span> <span class="token assign-left variable">FFLAGS</span><span class="token operator">=</span><span class="token string">"-I<span class="token variable">$MPI</span>/include -L<span class="token variable">$MPI</span>/lib -I<span class="token variable">$NETCDF</span>/include -fallow-argument-mismatch"</span><span class="token builtin class-name">export</span> <span class="token assign-left variable">NCDF_F_HOME</span><span class="token operator">=</span><span class="token variable">$NETCDF</span><span class="token builtin class-name">export</span> <span class="token assign-left variable">NCDF_C_HOME</span><span class="token operator">=</span><span class="token variable">$NETCDF</span><span class="token builtin class-name">export</span> <span class="token assign-left variable">HDF5_HOME</span><span class="token operator">=</span><span class="token variable">$NETCDF</span><span class="token builtin class-name">export</span> <span class="token assign-left variable">LD_LIBRARY_PATH</span><span class="token operator">=</span><span class="token variable">$MPI</span>/lib:<span class="token variable">$NETCDF</span>/lib:<span class="token variable">$PNETCDF</span>/lib:<span class="token variable">$LD_LIBRARY_PATH</span><span class="token builtin class-name">export</span> <span class="token assign-left variable"><span class="token environment constant">PATH</span></span><span class="token operator">=</span><span class="token environment constant">$PATH</span><span class="token builtin class-name">:</span><span class="token variable">$MPI</span>/bin:<span class="token variable">$NETCDF</span>/bin:<span class="token variable">$NETCDF</span>/include<span class="token builtin class-name">export</span> <span class="token assign-left variable">CC</span><span class="token operator">=</span>mpicc <span class="token comment">#gcc</span><span class="token builtin class-name">export</span> <span class="token assign-left variable">CXX</span><span class="token operator">=</span>mpicxx <span class="token comment">#g++</span><span class="token builtin class-name">export</span> <span class="token assign-left variable">FC</span><span class="token operator">=</span>mpif90 <span class="token comment">#gfortran</span><span class="token builtin class-name">export</span> <span class="token assign-left variable">FCFLAGS</span><span class="token operator">=</span><span class="token string">"-m64 <span class="token variable">$FCFLAGS</span>"</span> <span class="token builtin class-name">export</span> <span class="token assign-left variable">F77</span><span class="token operator">=</span>mpif77 <span class="token comment">#gfortran</span><span class="token builtin class-name">export</span> <span class="token assign-left variable">FFLAGS</span><span class="token operator">=</span><span class="token string">"-m64 <span class="token variable">$FFLAGS</span>"</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>配置好了之后,检查一下版本和路径</p><h2 id="4-安装XIOS"><a href="#4-安装XIOS" class="headerlink" title="4. 安装XIOS"></a>4. 安装XIOS</h2><p>提取并解析XIOS源代码,并将安装文件夹命名为xios。</p><pre class="line-numbers language-text" data-language="text"><code class="language-text">svn co http://forge.ipsl.jussieu.fr/ioserver/svn/XIOS2/trunk xios<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>在 xios/arch 目录下找到相应的架构配置文件并修改。笔者这里是用的GCC_LINUX,需要修改 .path, .env, .fcm 文件。</p><pre class="line-numbers language-text" data-language="text"><code class="language-text"># arch-GCC_LINUX.envexport HDF5_INC_DIR=$HOME/local/netcdf/include #根据自己情况修改export HDF5_LIB_DIR=$HOME/local/netcdf/lib #根据自己情况修改export NETCDF_INC_DIR=$HOME/local/netcdf/include #根据自己情况修改export NETCDF_LIB_DIR=$HOME/local/netcdf/lib #根据自己情况修改export BOOST_INC_DIR=$HOME/boostexport BOOST_LIB_DIR=$HOME/boost<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>具体路径需要根据自己的安装路径做相应修改。</p><pre class="line-numbers language-text" data-language="text"><code class="language-text"># arch-GCC_LINUX.fcm################################################################################################### Projet XIOS ###################################################################################################%CCOMPILER /home/czhang/local/mpich/bin/mpicc #根据自己情况修改%FCOMPILER /home/czhang/local/mpich/bin/mpif90 #根据自己情况修改%LINKER /home/czhang/local/mpich/bin/mpif90 #根据自己情况修改%BASE_CFLAGS -w -std=c++11 -D__XIOS_EXCEPTION -D_GLIBCXX_USE_CXX11_ABI=0 -ffree-line-length-none #这个标志需要在原基础上添加一些东西%PROD_CFLAGS -O3 -DBOOST_DISABLE_ASSERTS%DEV_CFLAGS -g -O2%DEBUG_CFLAGS -g%BASE_FFLAGS -D__NONE__%PROD_FFLAGS -O3%DEV_FFLAGS -g -O2%DEBUG_FFLAGS -g%BASE_INC -D__NONE__%BASE_LD -lstdc++%CPP cpp%FPP cpp -P%MAKE make<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>具体路径需要根据自己的安装路径做相应修改,FLAGS按照上述修改即可。</p><pre class="line-numbers language-text" data-language="text"><code class="language-text"># arch-GCC_LINUX.pathNETCDF_INCDIR="-I $NETCDF_INC_DIR"NETCDF_LIBDIR="-L $NETCDF_LIB_DIR"NETCDF_LIB="-lnetcdff -lnetcdf"MPI_INCDIR=""MPI_LIBDIR=""MPI_LIB=""HDF5_INCDIR="-I $HDF5_INC_DIR"HDF5_LIBDIR="-L $HDF5_LIB_DIR"HDF5_LIB="-lhdf5_hl -lhdf5 -lhdf5 -lz"<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>以上只展示到了 HDF5 的设置,后面的不用改变。去除 lcurl 标志即可。</p><p>配置完成后就可以在xios源代码目录下进行编译了。输入命令:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">./make_xios --full --arch GCC_LINUX -j <span class="token number">160</span> <span class="token operator">|&</span> <span class="token function">tee</span> compile_log.txt<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>选用GCC_LINUX架构配置进行编译。-j 表示使用多少个线程进行编译,tee compile_log.txt 表示将编译产生的信息存储在txt文件中,这是为了出问题时方便查看。</p><p>XIOS编译的时间很长,同时编译完成后不会有成功或恭喜信息,看到 finished 并且没有 error 报错就说明完成了。</p><h2 id="5-安装并编译NEMO"><a href="#5-安装并编译NEMO" class="headerlink" title="5. 安装并编译NEMO"></a>5. 安装并编译NEMO</h2><p>创建NEMO文件夹,并将压缩包解压。编译NEMO之前同样需要修改架构配置文件。笔者这里使用的是linux_gfortran架构</p><p>在arch文件夹下找到 arch-linux_gfortran.fcm文件并修改</p><pre class="line-numbers language-text" data-language="text"><code class="language-text">%NCDF_HOME /home/czhang/local/netcdf%HDF5_HOME /home/czhang/local/netcdf%NCDF_C_HOME /home/czhang/local/netcdf%NCDF_F_HOME /home/czhang/local/netcdf%XIOS_HOME /home/czhang/xios%NCDF_INC -I%NCDF_F_HOME/include -I%NCDF_C_HOME/include -I%HDF5_HOME/include%NCDF_LIB -L%NCDF_F_HOME/lib -lnetcdff -L%NCDF_C_HOME/lib -lnetcdf -lstdc++%XIOS_INC -I%XIOS_HOME/inc%XIOS_LIB -L%XIOS_HOME/lib -lxios -lstdc++%OASIS_INC -I%OASIS_HOME/build/lib/mct -I%OASIS_HOME/build/lib/psmile.MPI1%OASIS_LIB -L%OASIS_HOME/lib -lpsmile.MPI1 -lmct -lmpeu -lscrip%CPP cpp -Dkey_nosignedzero%FC mpif90 -c -cpp%FCFLAGS -I/home/czhang/local/netcdf/include $(FCFLAGS) -fdefault-real-8 -O3 -funroll-all-loops -fcray-pointer -ffree-line-length-none -fallow-argument-mismatch -cpp%FFLAGS %FCFLAGS%LD mpif90%LDFLAGS%FPPFLAGS -P -C -traditional%AR ar%ARFLAGS rs%MK make%USER_INC %XIOS_INC %NCDF_INC%USER_LIB %XIOS_LIB %NCDF_LIB%CC cc%CFLAGS -O0<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>在该配置文件中需要指明netcdf,hdf5,xios的路径</p><p>之后就可以进行NEMO 的编译。在源代码根目录进行:</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">./makenemo -m linux_gfortran -r GYRE_PISCES -n test1<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>之后就可以在cfgs文件夹下看到test1文件夹,其中包含 BLD, EXP00, MY_SRC, WORK, cpp_test1.fcm,在EXP00文件夹下有nemo执行程序</p><h2 id="6-运行NEMO"><a href="#6-运行NEMO" class="headerlink" title="6. 运行NEMO"></a>6. 运行NEMO</h2><p>在EXP00文件夹下,有一些初始化的输入文件(namelist,用于输入的*.xml文件等),如果需要使用其他输入文件需要将他们放在这个文件夹中</p><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ <span class="token builtin class-name">cd</span> cfgs/test1/EXP00/ <span class="token comment"># 进入EXP00文件夹中</span>$ mpirun -n <span class="token variable">$NPROCS</span> ./nemo <span class="token comment"># $NPROCS 是指要运行的线程数 , mpirun是自己系统的MPI的封装接口</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><h2 id="7-对于NEMO的一些配置"><a href="#7-对于NEMO的一些配置" class="headerlink" title="7. 对于NEMO的一些配置"></a>7. 对于NEMO的一些配置</h2><h3 id="7-1-更改输入输出文件路径"><a href="#7-1-更改输入输出文件路径" class="headerlink" title="7.1 更改输入输出文件路径"></a>7.1 更改输入输出文件路径</h3><p>在<strong>namelist_ref</strong>文件中</p><pre class="line-numbers language-fortran" data-language="fortran"><code class="language-fortran">cn_ocerst_indir <span class="token operator">=</span> <span class="token string">"./input"</span> <span class="token comment">! directory from which to read input ocean restarts</span>cn_ocerst_outdir <span class="token operator">=</span> <span class="token string">"./output_ocean_restart"</span> <span class="token comment">! directory in which to write output ocean restarts</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre><p>在<strong>namelist_top_ref</strong>文件中</p><pre class="line-numbers language-fortran" data-language="fortran"><code class="language-fortran">cn_trcrst_outdir <span class="token operator">=</span> <span class="token string">"./output_restart_trcrst"</span> <span class="token comment">! directory to which to write output passive tracer restarts</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>在<strong>file_def_nemo.xml</strong>文件中</p><pre class="line-numbers language-markup" data-language="markup"><code class="language-markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>file_definition</span> <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>multiple_file<span class="token punctuation">"</span></span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>output_time/@expname@_@freq@_@startdate@_@enddate@<span class="token punctuation">"</span></span> <span class="token attr-name">sync_freq</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>1mo<span class="token punctuation">"</span></span> <span class="token attr-name">min_digits</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>4<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>mesh_mask文件 和 ocean_output文件无法更改输出目录,除非修改NEMO源代码</p><h3 id="7-2-部分参数的作用"><a href="#7-2-部分参数的作用" class="headerlink" title="7.2 部分参数的作用"></a>7.2 部分参数的作用</h3><pre class="line-numbers language-fortran" data-language="fortran"><code class="language-fortran"><span class="token comment">! 在namelist_cfg文件中,调整分辨率</span><span class="token comment">! jpiglo, jpjglo参数不应该出现在该文件中,不用调整</span>nn_GYRE <span class="token operator">=</span> <span class="token number">25</span> <span class="token comment">! GYRE resolution [1/degrees] 。分辨率(度)的倒数</span>ln_bench <span class="token operator">=</span> <span class="token boolean">.true.</span> <span class="token comment">! ! =T benchmark with gyre: the gridsize is kept constant。将NEMO用于作为Benchmark</span>jpkglo <span class="token operator">=</span> <span class="token number">31</span> <span class="token comment">! number of model levels。模拟时的模型层数或深度</span>rn_Dt <span class="token operator">=</span> <span class="token number">7200.</span> <span class="token comment">! time step for the dynamics。设置模拟的时间步长</span><span class="token comment">! 在namelist_ref文件中,调整用于MPI并行计算的网格点,将两个参数设置为0使得系统自动调整网格</span>jpni <span class="token operator">=</span> <span class="token number">0</span> <span class="token comment">! number of processors following i (set automatically if < 1), see also ln_listonly = T</span>jpnj <span class="token operator">=</span> <span class="token number">0</span> <span class="token comment">! number of processors following j (set automatically if < 1), see also ln_listonly = T</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre><p>模型运行的信息在 ocean.output 文件中,通过该文件展示的信息来判断应该用多少MPI进程</p><p><strong>References:</strong></p>]]></content>
<categories>
<category> 工作负载 </category>
</categories>
<tags>
<tag> 工作负载 </tag>
<tag> 环境搭建 </tag>
<tag> HPC </tag>
<tag> 分布式 </tag>
</tags>
</entry>
<entry>
<title>IntelliJ IDEA 常用快捷键一览表</title>
<link href="/2024/04/02/intellij-idea-chang-yong-kuai-jie-jian-yi-lan-biao/"/>
<url>/2024/04/02/intellij-idea-chang-yong-kuai-jie-jian-yi-lan-biao/</url>
<content type="html"><![CDATA[<h2 id="1-IDEA的日常快捷键"><a href="#1-IDEA的日常快捷键" class="headerlink" title="1-IDEA的日常快捷键"></a><strong>1-IDEA的日常快捷键</strong></h2><h3 id="第1组:通用型"><a href="#第1组:通用型" class="headerlink" title="第1组:通用型"></a><strong>第1组:通用型</strong></h3><table><thead><tr><th>说明</th><th>快捷键</th></tr></thead><tbody><tr><td>复制代码-copy</td><td>ctrl + c</td></tr><tr><td>粘贴-paste</td><td>ctrl + v</td></tr><tr><td>剪切-cut</td><td>ctrl + x</td></tr><tr><td>撤销-undo</td><td>ctrl + z</td></tr><tr><td>反撤销-redo</td><td>ctrl + shift + z</td></tr><tr><td>保存-save all</td><td>ctrl + s</td></tr><tr><td>全选-select all</td><td>ctrl + a</td></tr></tbody></table><h3 id="第2组:提高编写速度(上)"><a href="#第2组:提高编写速度(上)" class="headerlink" title="第2组:提高编写速度(上)"></a><strong>第2组:提高编写速度(上)</strong></h3><table><thead><tr><th>说明</th><th>快捷键</th></tr></thead><tbody><tr><td>智能提示-edit</td><td>alt + enter</td></tr><tr><td>提示代码模板-insert live template</td><td>ctrl+j</td></tr><tr><td>使用xx块环绕-surround with …</td><td>ctrl+alt+t</td></tr><tr><td>调出生成getter/setter/构造器等结构-generate …</td><td>alt+insert</td></tr><tr><td>自动生成返回值变量-introduce variable …</td><td>ctrl+alt+v</td></tr><tr><td>复制指定行的代码-duplicate line or selection</td><td>ctrl+d</td></tr><tr><td>删除指定行的代码-delete line</td><td>ctrl+y</td></tr><tr><td>切换到下一行代码空位-start new line</td><td>shift + enter</td></tr><tr><td>切换到上一行代码空位-start new line before current</td><td>ctrl +alt+ enter</td></tr><tr><td>向上移动代码-move statement up</td><td>ctrl+shift+↑</td></tr><tr><td>向下移动代码-move statement down</td><td>ctrl+shift+↓</td></tr><tr><td>向上移动一行-move line up</td><td>alt+shift+↑</td></tr><tr><td>向下移动一行-move line down</td><td>alt+shift+↓</td></tr><tr><td>方法的形参列表提醒-parameter info</td><td>ctrl+p</td></tr></tbody></table><h3 id="第3组:提高编写速度(下)"><a href="#第3组:提高编写速度(下)" class="headerlink" title="第3组:提高编写速度(下)"></a><strong>第3组:提高编写速度(下)</strong></h3><table><thead><tr><th>说明</th><th>快捷键</th></tr></thead><tbody><tr><td>批量修改指定的变量名、方法名、类名等-rename</td><td>shift+f6</td></tr><tr><td>抽取代码重构方法-extract method …</td><td>ctrl+alt+m</td></tr><tr><td>重写父类的方法-override methods …</td><td>ctrl+o</td></tr><tr><td>实现接口的方法-implements methods …</td><td>ctrl+i</td></tr><tr><td>选中的结构的大小写的切换-toggle case</td><td>ctrl+shift+u</td></tr><tr><td>批量导包-optimize imports</td><td>ctrl+alt+o</td></tr></tbody></table><h3 id="第4组:类结构、查找和查看源码"><a href="#第4组:类结构、查找和查看源码" class="headerlink" title="第4组:类结构、查找和查看源码"></a><strong>第4组:类结构、查找和查看源码</strong></h3><table><thead><tr><th>说明</th><th>快捷键</th></tr></thead><tbody><tr><td>如何查看源码-go to class…</td><td>ctrl + 选中指定的结构 或 ctrl+n</td></tr><tr><td>显示当前类结构,支持搜索指定的方法、属性等-file structure</td><td>ctrl+f12</td></tr><tr><td>退回到前一个编辑的页面-back</td><td>ctrl+alt+←</td></tr><tr><td>进入到下一个编辑的页面-forward</td><td>ctrl+alt+→</td></tr><tr><td>打开的类文件之间切换-select previous/next tab</td><td>alt+←/→</td></tr><tr><td>光标选中指定的类,查看继承树结构-Type Hierarchy</td><td>ctrl+h</td></tr><tr><td>查看方法文档-quick documentation</td><td>ctrl+q</td></tr><tr><td>类的UML关系图-show uml popup</td><td>ctrl+alt+u</td></tr><tr><td>定位某行-go to line/column</td><td>ctrl+g</td></tr><tr><td>回溯变量或方法的来源-go to implementation(s)</td><td>ctrl+alt+b</td></tr><tr><td>折叠方法实现-collapse all</td><td>ctrl+shift+ -</td></tr><tr><td>展开方法实现-expand all</td><td>ctrl+shift+ +</td></tr></tbody></table><h3 id="第5组:查找、替换与关闭"><a href="#第5组:查找、替换与关闭" class="headerlink" title="第5组:查找、替换与关闭"></a><strong>第5组:查找、替换与关闭</strong></h3><table><thead><tr><th>说明</th><th>快捷键</th></tr></thead><tbody><tr><td>查找指定的结构</td><td>ctlr+f</td></tr><tr><td>快速查找:选中的Word快速定位到下一个-find next</td><td>ctrl+l</td></tr><tr><td>查找与替换-replace</td><td>ctrl+r</td></tr><tr><td>直接定位到当前行的首位-move caret to line start</td><td>home</td></tr><tr><td>直接定位到当前行的末位 -move caret to line end</td><td>end</td></tr><tr><td>查询当前元素在当前文件中的引用,然后按 F3 可以选择</td><td>ctrl+f7</td></tr><tr><td>全项目搜索文本-find in path …</td><td>ctrl+shift+f</td></tr><tr><td>关闭当前窗口-close</td><td>ctrl+f4</td></tr></tbody></table><h3 id="第6组:调整格式"><a href="#第6组:调整格式" class="headerlink" title="第6组:调整格式"></a><strong>第6组:调整格式</strong></h3><table><thead><tr><th>说明</th><th>快捷键</th></tr></thead><tbody><tr><td>格式化代码-reformat code</td><td>ctrl+alt+l</td></tr><tr><td>使用单行注释-comment with line comment</td><td>ctrl + /</td></tr><tr><td>使用/取消多行注释-comment with block comment</td><td>ctrl + shift + /</td></tr><tr><td>选中数行,整体往后移动-tab</td><td>tab</td></tr><tr><td>选中数行,整体往前移动-prev tab</td><td>shift + tab</td></tr></tbody></table><h2 id="2-Debug快捷键"><a href="#2-Debug快捷键" class="headerlink" title="2-Debug快捷键"></a><strong>2-Debug快捷键</strong></h2><table><thead><tr><th>说明</th><th>快捷键</th></tr></thead><tbody><tr><td>单步调试(不进入函数内部)- step over</td><td>F8</td></tr><tr><td>单步调试(进入函数内部)- step into</td><td>F7</td></tr><tr><td>强制单步调试(进入函数内部) - force step into</td><td>alt+shift+f7</td></tr><tr><td>选择要进入的函数 - smart step into</td><td>shift + F7</td></tr><tr><td>跳出函数 - step out</td><td>shift + F8</td></tr><tr><td>运行到断点 - run to cursor</td><td>alt + F9</td></tr><tr><td>继续执行,进入下一个断点或执行完程序 - resume program</td><td>F9</td></tr><tr><td>停止 - stop</td><td>Ctrl+F2</td></tr><tr><td>查看断点 - view breakpoints</td><td>Ctrl+Shift+F8</td></tr><tr><td>关闭 - close</td><td>Ctrl+F4</td></tr></tbody></table><p><strong>转载来源:</strong></p>]]></content>
<categories>
<category> 工具 </category>
</categories>
<tags>
<tag> IDE </tag>
<tag> IntelliJ IDEA </tag>
<tag> 快捷键 </tag>
<tag> 效率提升 </tag>
</tags>
</entry>
<entry>
<title>欢迎使用Hexo</title>
<link href="/2024/04/01/huan-ying-shi-yong-hexo/"/>
<url>/2024/04/01/huan-ying-shi-yong-hexo/</url>
<content type="html"><![CDATA[<p>Welcome to <a href="https://hexo.io/">Hexo</a>! This is your very first post. Check <a href="https://hexo.io/docs/">documentation</a> for more info. If you get any problems when using Hexo, you can find the answer in <a href="https://hexo.io/docs/troubleshooting.html">troubleshooting</a> or you can ask me on <a href="https://github.com/hexojs/hexo/issues">GitHub</a>.</p><h2 id="Quick-Start"><a href="#Quick-Start" class="headerlink" title="Quick Start"></a>Quick Start</h2><h3 id="Create-a-new-post"><a href="#Create-a-new-post" class="headerlink" title="Create a new post"></a>Create a new post</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ hexo new <span class="token string">"My New Post"</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>More info: <a href="https://hexo.io/docs/writing.html">Writing</a></p><h3 id="Run-server"><a href="#Run-server" class="headerlink" title="Run server"></a>Run server</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ hexo server<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>More info: <a href="https://hexo.io/docs/server.html">Server</a></p><h3 id="Generate-static-files"><a href="#Generate-static-files" class="headerlink" title="Generate static files"></a>Generate static files</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ hexo generate<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>More info: <a href="https://hexo.io/docs/generating.html">Generating</a></p><h3 id="Deploy-to-remote-sites"><a href="#Deploy-to-remote-sites" class="headerlink" title="Deploy to remote sites"></a>Deploy to remote sites</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">$ hexo deploy<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre><p>More info: <a href="https://hexo.io/docs/one-command-deployment.html">Deployment</a></p>]]></content>
<tags>
<tag> Hexo </tag>
</tags>
</entry>
</search>