-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1861 lines (659 loc) · 70.1 KB
/
index.html
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">
<link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">
<meta name="keywords" content="Hexo, NexT" />
<meta name="description" content="选择性记录,习惯性遗忘">
<meta property="og:type" content="website">
<meta property="og:title" content="寒江雪的博客">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="寒江雪的博客">
<meta property="og:description" content="选择性记录,习惯性遗忘">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="寒江雪的博客">
<meta name="twitter:description" content="选择性记录,习惯性遗忘">
<script type="text/javascript" id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Gemini',
version: '5.1.3',
sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
fancybox: true,
tabs: true,
motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
duoshuo: {
userId: '0',
author: '博主'
},
algolia: {
applicationID: '',
apiKey: '',
indexName: '',
hits: {"per_page":10},
labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
}
};
</script>
<link rel="canonical" href="http://yoursite.com/"/>
<title>寒江雪的博客</title><!-- hexo-inject:begin --><!-- hexo-inject:end -->
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<!-- hexo-inject:begin --><!-- hexo-inject:end --><div class="container sidebar-position-left
page-home">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-wrapper">
<div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">寒江雪的博客</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle">勇敢De心</p>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-fw fa-home"></i> <br />
首页
</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags/" rel="section">
<i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
标签
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section">
<i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
归档
</a>
</li>
</ul>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/05/11/强化学习基础/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="寒江雪">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="寒江雪的博客">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/05/11/强化学习基础/" itemprop="url">强化学习基础</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2019-05-11T22:09:30+08:00">
2019-05-11
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="概念"><a href="#概念" class="headerlink" title="概念"></a>概念</h3><p>强化学习研究的是智能体agent与环境之间交互的任务,也就是让agent像人类一样通过试错,不断地学习在不同的环境下做出最优的动作,而不是有监督地直接告诉agent在什么环境下应该做出什么动作。既然与环境交互,必然会有反馈,也即回报。</p>
<p>强化学习和监督学习的区别主要有以下两点:</p>
<ol>
<li>强化学习是试错学习(Trail-and-error),由于没有直接的指导信息,智能体要以不断与环境进行交互,通过试错的方式来获得最佳策略。(可以认为是交互产生样本)</li>
<li>延迟回报,强化学习的指导信息很少,而且往往是在事后(最后一个状态)才给出的,这就导致了一个问题,就是获得正回报或者负回报以后,如何将回报分配给前面的状态。</li>
</ol>
<h3 id="MDP"><a href="#MDP" class="headerlink" title="MDP"></a>MDP</h3><p>显然MDP是一个离散的过程,当前的执行的动作的回报依赖于未来的回报(延迟回报),一个使得在任意时刻和状态下的长期回报都是最大的策略是我们最终需要得到的。因此最简单的就是累计回报</p>
<p>$$G<em>{t}=R</em>{t+1}+R<em>{t+2}+R</em>{t+3}+\ldots=\sum<em>{k=t+1}^{\infty} R</em>{k}$$<br>但实际上我们一般会用下面更通用的公式来代替:</p>
<p>$$G<em>{t}=R</em>{t+1}+\gamma R<em>{t+2}+\gamma^{2} R</em>{t+3}+\ldots+\gamma^{T-t-1} R<em>{T}=\sum</em>{k=0}^{T-t-1} \gamma^{k} R_{t+k+1}$$</p>
<h3 id="马尔可夫决策过程"><a href="#马尔可夫决策过程" class="headerlink" title="马尔可夫决策过程"></a>马尔可夫决策过程</h3><p>一个有限马尔可夫决策过程由一个四元组构成 M=(S,A,P,R)。如上所述,S表示状态集空间,A表示动作集空间,P表示状态转移概率矩阵,R表示期望回报值。</p>
<p>给定当前状态和动作,转移到另一个状态的概率计算:</p>
<p>$$p\left(s^{\prime} | s, a\right)=\operatorname{Pr}\left{S<em>{t+1}=s^{\prime} | S</em>{t}=s, A_{t}=a\right} \in \mathbf{P}$$</p>
<p>在给定状态、动作和下一个状态的前提下,回报的计算:</p>
<p>$$r\left(s, a, s^{\prime}\right)=\mathbf{E}\left[R<em>{t+1} | S</em>{t}=s, A<em>{t}=a, S</em>{t+1}=s^{\prime}\right] \in \mathbf{R}$$</p>
<h3 id="值函数及贝尔曼公式"><a href="#值函数及贝尔曼公式" class="headerlink" title="值函数及贝尔曼公式"></a>值函数及贝尔曼公式</h3><p>增强学习的最终结果是找到一个环境到动作的映射—即策略$\pi(a|s)$,在几乎所有的强化学习理论中都会定义值函数来表示给定策略下期望的未来回报,并将值函数作为评估学习效果的指标。值函数有多种定义,目前常见的是将值函数直接定义为未来回报的期望:</p>
<p>$$v<em>{\pi}(s)=\mathbf{E}</em>{\pi}\left[G<em>{t} | S</em>{t}=s\right]=\mathbf{E}<em>{\pi}\left[\sum</em>{k=0}^{\infty} \gamma^{k} R<em>{t+k+1} | S</em>{t}=s\right]$$</p>
<p>上面表示的是在某个策略$\pi$下,当环境处于状态s时未来回报的期望,因此又叫做状态值函数(state-value function for policy),只跟当前状态有关。同样,我们也可以定义动作值函数(action-value function for policy),如下:</p>
<p>$$q<em>{\pi}(s, a)=\mathbf{E}</em>{\pi}\left[G<em>{t} | S</em>{t}=s, A<em>{t}=a\right]=\mathbf{E}</em>{\pi}\left[\sum<em>{k=0}^{\infty} \gamma^{k} R</em>{t+k+1} | S<em>{t}=s, A</em>{t}=a\right]$$</p>
<p>值函数和动作值函数的区别在于动作值是在给定动作下的期望回报,从形式上看,都可以写成递归的形式:</p>
<p>$$v<em>{\pi}(s)=\mathbf{E}</em>{\pi}\left[G<em>{t} | S</em>{t}=s\right]=\mathbf{E}<em>{\pi}\left[\sum</em>{k=0}^{\infty} \gamma^{k} R<em>{t+k+1} | S</em>{t}=s\right]$$</p>
<p>$$=\mathbf{E}<em>{\pi}\left[R</em>{t+1}+\gamma \sum<em>{k=0}^{\infty} \gamma^{k} R</em>{t+k+2} | S_{t}=s\right]$$</p>
<p>$$=\sum<em>{a} \pi(a | s) \cdot \mathbf{E}</em>{\pi}\left[R<em>{t+1}+\gamma \sum</em>{k=0}^{\infty} \gamma^{k} R<em>{t+k+2} | S</em>{t}=s, A_{t}\right]$$</p>
<p>$$=\sum<em>{a} \pi(a | s) \sum</em>{s^{\prime}} p\left(s^{\prime} | s, a\right)\left[r\left(s, a, s^{\prime}\right)+\gamma \mathbf{E}<em>{\pi}\left[\sum</em>{k=0}^{\infty} \gamma^{k} R<em>{t+k+2} | S</em>{t+1}=s^{\prime}\right]\right]$$</p>
<p>$$=\sum<em>{a} \pi(a | s) \sum</em>{s^{\prime}} p\left(s^{\prime} | s, a\right)\left[r\left(s, a, s^{\prime}\right)+\gamma v_{\pi}\left(s^{\prime}\right)\right]$$</p>
<p>因此当前状态下的回报可以用下一状态下的值函数求解,这个公式就是贝尔曼公式。</p>
<p>同理状态值函数也可以递归求解:</p>
<p>$$\begin{aligned} q<em>{\pi}(s, a) &=\mathbf{E}</em>{\pi}\left[G<em>{t} | S</em>{t}=s, A<em>{t}=a\right] \ &=\mathbf{E}</em>{\pi}\left[R<em>{t+1}+\gamma \sum</em>{k=0}^{\infty} \gamma^{k} R<em>{t+k+2} | S</em>{t}=s, A<em>{t}=a\right] \ &=\sum</em>{s^{\prime}} p\left(s^{\prime} | s, a\right)\left[r\left(s, a, s^{\prime}\right)+\gamma v_{\pi}\left(s^{\prime}\right)\right] \end{aligned}$$</p>
<p>根据贝叶斯公式可以把两者关联:</p>
<p>$$v<em>{\pi}(s)=\sum</em>{a} \pi(a | s) q_{\pi}(s, a)$$</p>
<h3 id="最优值函数及贝尔曼最优公式"><a href="#最优值函数及贝尔曼最优公式" class="headerlink" title="最优值函数及贝尔曼最优公式"></a>最优值函数及贝尔曼最优公式</h3><p>我们的目标就是使得任意时刻未来回报的期望值都是最大的,为此目标可以是:</p>
<p>$$\pi<em>{*}=\underset{\pi}{\arg \max } v</em>{\pi}(s)$$</p>
<p>策略可能有多个,但是最优的状态值函数和动作值函数是一致的:</p>
<p>$$v<em>{*}(s)=\max </em>{\pi} v_{\pi}(s)$$</p>
<p>$$q<em>{*}(s, a)=\max </em>{\pi} q_{\pi}(s, a)$$</p>
<p>目标函数有了,最重要的就是如何求解最优策略$\pi_*(a|s)$,由于状态之间的转义是依赖动作的,因此不需要求解状态转义的概率。</p>
<p>贝尔曼公式与贝尔曼最优公式是MDP求解的基础,下面主要介绍几种MDP求解的方法。</p>
<p><a href="https://blog.csdn.net/chenhoujiangsir/article/details/52909921" target="_blank" rel="external">参考链接</a></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/05/04/FM模型/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="寒江雪">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="寒江雪的博客">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/05/04/FM模型/" itemprop="url">FM模型</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2019-05-04T19:17:19+08:00">
2019-05-04
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<center> <h3> FM算法 </h3> </center>
<ol>
<li><p>线性模型</p>
<p>$$y=\omega<em>{0}+\sum</em>{i=1}^{n} \omega<em>{i} x</em>{i}$$</p>
</li>
<li><p>因子模型</p>
<p>$$y=\omega<em>{0}+\sum</em>{i=1}^{n} \omega<em>{i} x</em>{i}+\sum<em>{i=1}^{n-1} \sum</em>{j=i+1}^{n}<v_{i}, v_{j}="">x<em>{i} x</em>{j}$$</v_{i},></p>
<p>其中$v \in \mathbb{R}^{n, k}$, $ \quad<\mathbf{v}<em>{i}, \mathbf{v}</em>{j}>$ <vi,vj>表示的是两个大小为$k$的向量之间的<strong>点积</strong>:</vi,vj></p>
<p>$$<\mathbf{v}<em>{i}, \mathbf{v}</em>{j}>=\sum<em>{f=1}^{k} v</em>{i, f} \cdot v_{j, f}$$</p>
</li>
<li><p>FM模型求解 </p>
<p>FM模型在基础的线性模型的基础上引入交叉项</p>
<p>$$y=\omega<em>{0}+\sum</em>{i=1}^{n} \omega<em>{i} x</em>{i}+\sum<em>{i=1}^{n-1} \sum</em>{j=1+1}^{n} \omega<em>{i j} x</em>{i} x_{j}$$</p>
<p>一般来说数据是非常稀疏的,$w_{i,j}$ 无法通过训练得到。因此,辅助向量$v<em>i$ 对$w</em>{i,j}$进行估计:</p>
<p>$$\hat{\omega}<em>{i j}=\mathbf{v}</em>{i} \mathbf{v}_{j}^{T}$$</p>
</li>
<li><p>交叉项的求解 (时间复杂度 $O(kn)$)</p>
</li>
</ol>
<p>$$\begin{aligned} & \sum<em>{i=1}^{n} \sum</em>{j=i+1}^{n}\left\langle\mathbf{v}<em>{i}, \mathbf{v}</em>{j}\right\rangle x<em>{i} x</em>{j} \=& \frac{1}{2} \sum<em>{i=1}^{n} \sum</em>{j=1}^{n}\left\langle\mathbf{v}<em>{i}, \mathbf{v}</em>{j}\right\rangle x<em>{i} x</em>{j}-\frac{1}{2} \sum<em>{i=1}^{n}\left\langle\mathbf{v}</em>{i}, \mathbf{v}<em>{i}\right\rangle x</em>{i} x_{i} \end{aligned}$$</p>
<p>$$=\frac{1}{2}\left(\sum<em>{i=1}^{n} \sum</em>{j=1}^{n} \sum<em>{f=1}^{k} v</em>{i, f} v<em>{j, f} x</em>{i} x<em>{j}-\sum</em>{i=1}^{n} \sum<em>{f=1}^{k} v</em>{i, f} v<em>{i, f} x</em>{i} x_{i}\right)$$</p>
<p>$$=\frac{1}{2} \sum<em>{f=1}^{k}\left(\left(\sum</em>{i=1}^{n} v<em>{i, f} x</em>{i}\right)\left(\sum<em>{j=1}^{n} v</em>{j, f} x<em>{j}\right)-\sum</em>{i=1}^{n} v<em>{i, f}^{2} x</em>{i}^{2}\right)$$</p>
<p>$$=\frac{1}{2} \sum<em>{f=1}^{k}\left(\left(\sum</em>{i=1}^{n} v<em>{i, f} x</em>{i}\right)^{2}-\sum<em>{i=1}^{n} v</em>{i, f}^{2} x_{i}^{2}\right)$$</p>
<ol>
<li><p>反向梯度</p>
<p>损失函数(假设是MSE)</p>
<p>$$\operatorname{loss}^{R}(\hat{y}, y)=\frac{1}{2} \sum_{i=1}^{m}\left(\hat{y}^{(i)}-y^{(i)}\right)^{2}$$</p>
<p>梯度:</p>
<p>$$\frac{\partial \operatorname{loss}^{R}(\hat{y}, y)}{\partial \theta}=(\hat{y}-y) \frac{\partial \hat{y}}{\partial \theta}$$</p>
<p>$$\frac{\partial}{\partial \theta} \hat{y}(\mathbf{x})=\left{\begin{array}{ll}{1,} & {\text { if } \theta \text { is } w<em>{0}} \ {x</em>{i},} & {\text { if } \theta \text { is } w<em>{i}} \ {x</em>{i} \sum<em>{j=1}^{n} v</em>{j, f} x<em>{j}-v</em>{i, f} x<em>{i}^{2},} & {\text { if } \theta \text { is } v</em>{i, f}}\end{array}\right.$$</p>
</li>
<li><p>c++代码实现</p>
</li>
</ol>
<p>fm.h</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><iostream></span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><vector></span></span></div><div class="line"></div><div class="line"><span class="meta"># <span class="meta-keyword">ifndef</span> SRC_FM_</span></div><div class="line"><span class="meta"># <span class="meta-keyword">define</span> SRC_FM_</span></div><div class="line"></div><div class="line"><span class="keyword">using</span> <span class="built_in">std</span>::<span class="built_in">vector</span>;</div><div class="line"><span class="keyword">using</span> <span class="built_in">std</span>::<span class="built_in">string</span>;</div><div class="line"></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">FM</span> {</span></div><div class="line"> <span class="keyword">public</span>:</div><div class="line"> FM();</div><div class="line"> ~FM();</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">Init</span><span class="params">(<span class="built_in">string</span> file_name, <span class="keyword">int</span> n_sample, <span class="keyword">int</span> n, <span class="keyword">int</span> k)</span></span>;</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">Fit</span><span class="params">(<span class="keyword">int</span> epoch)</span></span>;</div><div class="line"> <span class="function"><span class="keyword">float</span> <span class="title">Predict</span><span class="params">(<span class="built_in">vector</span><<span class="keyword">float</span>> &sample)</span></span>;</div><div class="line"> <span class="keyword">private</span>:</div><div class="line"> <span class="function"><span class="keyword">float</span> <span class="title">GradYToTheta</span><span class="params">(<span class="keyword">int</span> sample, <span class="keyword">int</span> i, <span class="keyword">int</span> f)</span></span>;</div><div class="line"> <span class="function"><span class="keyword">void</span> <span class="title">CalcVDot</span><span class="params">()</span></span>;</div><div class="line"> <span class="keyword">private</span>:</div><div class="line"> <span class="built_in">vector</span><<span class="built_in">vector</span><<span class="keyword">float</span>> > data_;</div><div class="line"> <span class="built_in">vector</span><<span class="keyword">float</span>> y_;</div><div class="line"> <span class="built_in">vector</span><<span class="keyword">float</span>> w_;</div><div class="line"> <span class="built_in">vector</span><<span class="built_in">vector</span><<span class="keyword">float</span>> > v_;</div><div class="line"> <span class="built_in">vector</span><<span class="built_in">vector</span><<span class="keyword">float</span>> > v_dot_;</div><div class="line">};</div><div class="line"></div><div class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></div></pre></td></tr></table></figure>
<p>fm.cpp</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><fstream></span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string"><iomanip></span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"fm.h"</span></span></div><div class="line"></div><div class="line"><span class="keyword">using</span> <span class="built_in">std</span>::ifstream;</div><div class="line"></div><div class="line">FM::FM() {}</div><div class="line"></div><div class="line">FM::~FM() {}</div><div class="line"></div><div class="line"><span class="keyword">void</span> FM::Init(<span class="built_in">string</span> file_name, <span class="keyword">int</span> n_sample, <span class="keyword">int</span> n, <span class="keyword">int</span> k) {</div><div class="line"> ifstream fin(file_name.c_str());</div><div class="line"> <span class="keyword">if</span>(!fin.is_open()) {</div><div class="line"> <span class="built_in">std</span>::<span class="built_in">cerr</span> << <span class="string">"opep file failure"</span> << <span class="built_in">std</span>::<span class="built_in">endl</span>;</div><div class="line"> }</div><div class="line"></div><div class="line">}</div><div class="line"><span class="keyword">void</span> FM::Fit(<span class="keyword">int</span> epoch) {</div><div class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> ep = <span class="number">0</span>; ep < epoch; ep++) {</div><div class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < data_.size(); i++) {</div><div class="line"> <span class="keyword">float</span> y_p = Predict(data_[i]);</div><div class="line"> <span class="keyword">float</span> resid = y_p - y_[i];</div><div class="line"> w_[<span class="number">0</span>] += resid;</div><div class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">1</span>; j < w_.size(); j++) {</div><div class="line"> w_[j] = resid*data_[i][j<span class="number">-1</span>];</div><div class="line"> }</div><div class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> iter_i = <span class="number">0</span>; iter_i < v_.size(); iter_i++) {</div><div class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> f = <span class="number">0</span>; f < v_[iter_i].size(); f++) {</div><div class="line"> v_[iter_i][f] += resid*GradYToTheta(i, iter_i, f);</div><div class="line"> }</div><div class="line"> }</div><div class="line"> CalcVDot();</div><div class="line"> }</div><div class="line"></div><div class="line"> }</div><div class="line"></div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">float</span> FM::Predict(<span class="built_in">vector</span><<span class="keyword">float</span>>& sample) {</div><div class="line"> <span class="keyword">float</span> result = <span class="number">0</span>;</div><div class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i < w_.size(); i++) {</div><div class="line"> result += sample[i]*w_[i];</div><div class="line"> }</div><div class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < sample.size(); i++) {</div><div class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j= i+<span class="number">1</span>; j < sample.size(); j++) {</div><div class="line"> result += v_dot_[i][j]*sample[i]*sample[j];</div><div class="line"> }</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> result;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">void</span> FM::CalcVDot() {</div><div class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < v_.size(); i++) {</div><div class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> j = <span class="number">0</span>; j < v_.size(); j++) {</div><div class="line"> <span class="keyword">float</span> temp = <span class="number">0</span>;</div><div class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> f = <span class="number">0</span>; f < v_[i].size(); f++) {</div><div class="line"> temp += v_[i][f]*v_[j][f];</div><div class="line"> }</div><div class="line"> v_dot_[i][j] = temp;</div><div class="line"> }</div><div class="line"> }</div><div class="line">}</div><div class="line"><span class="keyword">float</span> FM::GradYToTheta(<span class="keyword">int</span> sample, <span class="keyword">int</span> i, <span class="keyword">int</span> f) {</div><div class="line"> <span class="keyword">float</span> sum = <span class="number">0</span>;</div><div class="line"> <span class="keyword">for</span> (<span class="keyword">int</span> iter_j; iter_j < v_.size(); iter_j++) {</div><div class="line"> sum += v_[iter_j][f]*data_[sample][iter_j];</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keyword">return</span> data_[sample][i]*(sum - v_[i][f]*data_[sample][i]);</div><div class="line">}</div></pre></td></tr></table></figure>
<p>//</p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/05/01/c-基础之工厂模式/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="寒江雪">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="寒江雪的博客">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/05/01/c-基础之工厂模式/" itemprop="url">c++基础之工厂模式</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2019-05-01T21:26:45+08:00">
2019-05-01
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="简单工厂模式"><a href="#简单工厂模式" class="headerlink" title="简单工厂模式"></a>简单工厂模式</h3><h3 id="工厂模式"><a href="#工厂模式" class="headerlink" title="工厂模式"></a>工厂模式</h3><h3 id="抽象工厂模式"><a href="#抽象工厂模式" class="headerlink" title="抽象工厂模式"></a>抽象工厂模式</h3><p><a href="https://www.cnblogs.com/cxjchen/p/3143633.html" target="_blank" rel="external">link</a></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/05/01/c-基础之单例模式/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="寒江雪">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="寒江雪的博客">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/05/01/c-基础之单例模式/" itemprop="url">c++基础之单例模式</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2019-05-01T20:53:27+08:00">
2019-05-01
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="单例模式"><a href="#单例模式" class="headerlink" title="单例模式"></a>单例模式</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">ifndef</span> _SINGLETON_H_</span></div><div class="line"><span class="meta">#<span class="meta-keyword">define</span> _SINGLETON_H_</span></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">Singleton</span>{</span></div><div class="line"><span class="keyword">public</span>:</div><div class="line"> <span class="function"><span class="keyword">static</span> Singleton* <span class="title">getInstance</span><span class="params">()</span></span>;</div><div class="line"></div><div class="line"><span class="keyword">private</span>:</div><div class="line"> Singleton();</div><div class="line"> <span class="comment">//把复制构造函数和=操作符也设为私有,防止被复制</span></div><div class="line"> Singleton(<span class="keyword">const</span> Singleton&);</div><div class="line"> Singleton& <span class="keyword">operator</span>=(<span class="keyword">const</span> Singleton&);</div><div class="line"></div><div class="line"> <span class="keyword">static</span> Singleton* instance;</div><div class="line">};</div><div class="line"></div><div class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></div></pre></td></tr></table></figure>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"Singleton.h"</span></span></div><div class="line"></div><div class="line">Singleton::Singleton(){</div><div class="line"></div><div class="line">}</div><div class="line"></div><div class="line">Singleton::Singleton(<span class="keyword">const</span> Singleton&){</div><div class="line"></div><div class="line">}</div><div class="line"></div><div class="line">Singleton& Singleton::<span class="keyword">operator</span>=(<span class="keyword">const</span> Singleton&){</div><div class="line"></div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">//在此处初始化, 饿汉实现</span></div><div class="line">Singleton* Singleton::instance = <span class="keyword">new</span> Singleton();</div><div class="line">Singleton* Singleton::getInstance(){</div><div class="line"> <span class="keyword">return</span> instance;</div><div class="line">}</div></pre></td></tr></table></figure>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"Singleton.h"</span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdio.h></span></span></div><div class="line"></div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>{</div><div class="line"> Singleton* singleton1 = Singleton::getInstance();</div><div class="line"> Singleton* singleton2 = Singleton::getInstance();</div><div class="line"> <span class="built_in">std</span>::<span class="built_in">cout</span> << singleton1 << <span class="string">"=="</span><< singleton2<< <span class="built_in">std</span>::<span class="built_in">endl</span>;</div><div class="line"> <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">}</div></pre></td></tr></table></figure>
<h3 id="线程安全的懒汉实现"><a href="#线程安全的懒汉实现" class="headerlink" title="线程安全的懒汉实现"></a>线程安全的懒汉实现</h3><h4 id="经典实现"><a href="#经典实现" class="headerlink" title="经典实现"></a>经典实现</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">singleton</span></span></div><div class="line"><span class="class">{</span></div><div class="line"><span class="keyword">protected</span>:</div><div class="line"> singleton()</div><div class="line"> {</div><div class="line"> pthread_mutex_init(&mutex);</div><div class="line"> }</div><div class="line"><span class="keyword">private</span>:</div><div class="line"> <span class="keyword">static</span> singleton* p;</div><div class="line"><span class="keyword">public</span>:</div><div class="line"> <span class="keyword">static</span> <span class="keyword">pthread_mutex_t</span> mutex;</div><div class="line"> <span class="function"><span class="keyword">static</span> singleton* <span class="title">initance</span><span class="params">()</span></span>;</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keyword">pthread_mutex_t</span> singleton::mutex;</div><div class="line">singleton* singleton::p = <span class="literal">NULL</span>;</div><div class="line">singleton* singleton::initance()</div><div class="line">{</div><div class="line"> <span class="keyword">if</span> (p == <span class="literal">NULL</span>)</div><div class="line"> {</div><div class="line"> pthread_mutex_lock(&mutex);</div><div class="line"> <span class="keyword">if</span> (p == <span class="literal">NULL</span>)</div><div class="line"> p = <span class="keyword">new</span> singleton();</div><div class="line"> pthread_mutex_unlock(&mutex);</div><div class="line"> }</div><div class="line"> <span class="keyword">return</span> p;</div><div class="line">}</div></pre></td></tr></table></figure>
<h4 id="内部静态变量的懒汉实现"><a href="#内部静态变量的懒汉实现" class="headerlink" title="内部静态变量的懒汉实现"></a>内部静态变量的懒汉实现</h4><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">singleton</span></span></div><div class="line"><span class="class">{</span></div><div class="line"><span class="keyword">protected</span>:</div><div class="line"> singleton()</div><div class="line"> {</div><div class="line"> pthread_mutex_init(&mutex);</div><div class="line"> }</div><div class="line"><span class="keyword">public</span>:</div><div class="line"> <span class="keyword">static</span> <span class="keyword">pthread_mutex_t</span> mutex;</div><div class="line"> <span class="function"><span class="keyword">static</span> singleton* <span class="title">initance</span><span class="params">()</span></span>;</div><div class="line"> <span class="keyword">int</span> a;</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keyword">pthread_mutex_t</span> singleton::mutex;</div><div class="line">singleton* singleton::initance()</div><div class="line">{</div><div class="line"> pthread_mutex_lock(&mutex);</div><div class="line"> <span class="keyword">static</span> singleton obj;</div><div class="line"> pthread_mutex_unlock(&mutex);</div><div class="line"> <span class="keyword">return</span> &obj;</div><div class="line">}</div></pre></td></tr></table></figure>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/05/01/c-容易混淆的概念/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="寒江雪">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="寒江雪的博客">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/05/01/c-容易混淆的概念/" itemprop="url">c++容易混淆的概念</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2019-05-01T11:07:55+08:00">
2019-05-01
</time>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h3 id="amp-(左值引用)-和-amp-amp-右值引用"><a href="#amp-(左值引用)-和-amp-amp-右值引用" class="headerlink" title="&(左值引用) 和 && 右值引用"></a>&(左值引用) 和 && 右值引用</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">test</span><span class="params">(<span class="keyword">int</span> & i)</span> </span>{</div><div class="line"> i++;</div><div class="line">}</div><div class="line">test(<span class="number">1</span>) <span class="comment">// 编译不通过,非常量的引用值必须是左值。</span></div></pre></td></tr></table></figure>
<p>&(左值引用):需要注意的是生命周期,<br>c++ 中的左值就像指针,它可以捕获实实在在的实体,但是我们要注意被捕获值的生命周期。不要随便把生命周期和栈同步的实体传给了它;</p>
<p>&& 右值引用<br>右值其实也是指针,但是它功能是专门捕获匿名的实体(可以理解为编译产生的中间变量)。同时我们要注意,右值在定义时捕获了实体以后,右值的名字就变成了被捕获的实体。</p>
<h3 id="类类型"><a href="#类类型" class="headerlink" title="类类型"></a>类类型</h3><p>类的声明:仅声明类暂时不定义它</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">Test</span>;</span> <span class="comment">// 类的声明</span></div></pre></td></tr></table></figure>
<ol>
<li><p>这种声明被称作 “前向声明”,在它声明之后定义之前是一个不完全类型(incomplete type),即我们知道是个类型,但是不知道内部情况</p>
</li>
<li><p>不完全类型只能在非常有限的情况下使用:指针或者引用,参数或者返回值(都不会创建类型对象)</p>
</li>
<li><p>只有类被定义之后,数据成员才能才能被申明成这种类型,所以一个类的成员类型不能是自己(如果可以的话,就会无限循环了)</p>
</li>
</ol>
<h3 id="static"><a href="#static" class="headerlink" title="static"></a>static</h3><ol>
<li>静态成员的定义和声明要加个关键static。静态成员可以通过双冒号来使用,即<类名>::<静态成员名>。</li>
<li>初始化静态成员变量要在类的外面进行。初始化的格式如下:数据类型 类名::静态成员变量名 = 初值;<figure class="highlight c++"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">static</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">(<span class="keyword">int</span> i)</span> </span>{</div><div class="line"> <span class="keyword">static</span> j = <span class="number">0</span>;</div><div class="line"> <span class="built_in">std</span>::<span class="built_in">cout</span> << i << <span class="string">" static j 地址"</span> << &j<< <span class="built_in">std</span>::<span class="built_in">endl</span>;</div><div class="line">}</div></pre></td></tr></table></figure>
</li>
</ol>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/03/24/g-编译参数说明/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="寒江雪">
<meta itemprop="description" content="">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="寒江雪的博客">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/03/24/g-编译参数说明/" itemprop="url">g++ 编译参数说明</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2019-03-24T23:06:19+08:00">
2019-03-24
</time>
</span>
</div>
</header>