|
1 |
| -#链接 |
2 |
| -------- |
3 |
| ->牛客OJ:[数字在排序数组中出现的次数](http://www.nowcoder.com/practice/70610bf967994b22bb1c26f9ae901fa2?tpId=13&tqId=11190&rp=2&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking) |
4 |
| -> |
5 |
| ->九度OJ:http://ac.jobdu.com/problem.php?pid=1349 |
6 |
| -> |
7 |
| ->GitHub代码: [038-数字在排序数组中出现的次数](https://github.com/gatieme/CodingInterviews/tree/master/038-数字在排序数组中出现的次数) |
8 |
| -> |
9 |
| ->CSDN题解:[剑指Offer--038-数字在排序数组中出现的次数](http://blog.csdn.net/gatieme/article/details/51330871) |
10 |
| -
|
11 |
| - |
12 |
| -| 牛客OJ | 九度OJ | CSDN题解 | GitHub代码 | |
13 |
| -| ------------- |:-------------:| -----:| |
14 |
| -|[038-数字在排序数组中出现的次数](http://www.nowcoder.com/practice/70610bf967994b22bb1c26f9ae901fa2?tpId=13&tqId=11190&rp=2&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking) | [1505-数字在排序数组中出现的次数](http://ac.jobdu.com/problem.php?pid=1349) | [剑指Offer--038-数字在排序数组中出现的次数](http://blog.csdn.net/gatieme/article/details/51330871) | [038-数字在排序数组中出现的次数](https://github.com/gatieme/CodingInterviews/tree/master/038-数字在排序数组中出现的次数) | |
15 |
| - |
16 |
| -#题意 |
17 |
| -------- |
18 |
| - |
19 |
| -**题目描述** |
20 |
| - |
21 |
| ->统计一个数字在排序数组中出现的次数。 |
| 1 | +#1 链接 |
| 2 | +------- |
| 3 | + |
| 4 | +>>牛客OJ:[数字在排序数组中出现的次数](http://www.nowcoder.com/practice/70610bf967994b22bb1c26f9ae901fa2?tpId=13&tqId=11190&rp=2&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking) |
| 5 | +> |
| 6 | +>九度OJ:http://ac.jobdu.com/problem.php?pid=1349 |
| 7 | +> |
| 8 | +>GitHub代码: [038-数字在排序数组中出现的次数](https://github.com/gatieme/CodingInterviews/tree/master/038-数字在排序数组中出现的次数) |
| 9 | +> |
| 10 | +>CSDN题解:[剑指Offer--038-数字在排序数组中出现的次数](http://blog.csdn.net/gatieme/article/details/51330871) |
| 11 | +
|
| 12 | + |
| 13 | +| 牛客OJ | 九度OJ | CSDN题解 | GitHub代码 | |
| 14 | +| ------------- |:-------------:| -----:| |
| 15 | +|[038-数字在排序数组中出现的次数](http://www.nowcoder.com/practice/70610bf967994b22bb1c26f9ae901fa2?tpId=13&tqId=11190&rp=2&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking) | [1505-数字在排序数组中出现的次数](http://ac.jobdu.com/problem.php?pid=1349) | [剑指Offer--038-数字在排序数组中出现的次数](http://blog.csdn.net/gatieme/article/details/51330871) | [038-数字在排序数组中出现的次数](https://github.com/gatieme/CodingInterviews/tree/master/038-数字在排序数组中出现的次数) | |
| 16 | + |
| 17 | +#2 题意 |
| 18 | +------- |
| 19 | + |
| 20 | +**题目描述** |
| 21 | + |
| 22 | +>设计一个不能被继承的类 |
| 23 | +
|
| 24 | + |
| 25 | + |
| 26 | +#3 分析 |
| 27 | +------- |
| 28 | + |
| 29 | +首先想到的是在C++中, 子类的构造函数会自动调用父类的构造函数, 同样, 子类的析构函数也会自动调用父类的析构函数 |
| 30 | + |
| 31 | +要想一个类不能被继承, 我们只要把它的构造函数和析构函数都定义为私有函数. |
| 32 | + |
| 33 | +那么当一个类试图从它那继承的时候, 必然会由于试图调用构造函数、析构函数而导致编译错误 |
| 34 | + |
| 35 | +```cpp |
| 36 | +class SealedClass |
| 37 | +{ |
| 38 | +private : |
| 39 | + SealedClass( ){ } |
| 40 | + ~SealedClass( ){ }; |
| 41 | +}; |
| 42 | + |
| 43 | +``` |
| 44 | + |
| 45 | +可是这个类的构造函数和析构函数都是私有函数了, 我们怎样才能得到该类的实例呢 |
| 46 | + |
| 47 | + |
| 48 | +我们有很多种方法来实现类的创建 |
| 49 | + |
| 50 | +* 定义静态函数或者静态变量来创建和释放类的实例 |
| 51 | + |
| 52 | +* 友元函数可以访问类的私有成员 |
| 53 | + |
| 54 | +* 友元类可以访问类的私有成员 |
| 55 | + |
| 56 | +#4 静态的创建和释放类的实例 |
| 57 | +------- |
| 58 | + |
| 59 | + |
| 60 | +##4.1 静态函数 |
| 61 | +------- |
| 62 | + |
| 63 | +```cpp |
| 64 | +#include <iostream> |
| 65 | +using namespace std; |
| 66 | + |
| 67 | + |
| 68 | +class SealedClass |
| 69 | +{ |
| 70 | +public : |
| 71 | + static SealedClass* GetInstance( ) |
| 72 | + { |
| 73 | + return new SealedClass( ); |
| 74 | + } |
| 75 | +private : |
| 76 | + SealedClass( ){ } |
| 77 | + ~SealedClass( ){ }; |
| 78 | + |
| 79 | +}; |
| 80 | + |
| 81 | +class Base : public SealedClass |
| 82 | +{ |
| 83 | +}; |
| 84 | + |
| 85 | +int main( ) |
| 86 | +{ |
| 87 | + SealedClass *pb = Base::GetInstance( ); |
| 88 | + Base base; |
| 89 | + |
| 90 | + return 0; |
| 91 | +} |
| 92 | +``` |
| 93 | +
|
| 94 | +##4.2 静态变量 |
| 95 | +------- |
| 96 | +
|
| 97 | +
|
| 98 | +```cpp |
| 99 | +#include <iostream> |
| 100 | +
|
| 101 | +using namespace std; |
| 102 | +
|
| 103 | +
|
| 104 | +class SealedClass |
| 105 | +{ |
| 106 | +public : |
| 107 | + static SealedClass* GetInstance( ) |
| 108 | + { |
| 109 | + if(m_sc == NULL) |
| 110 | + { |
| 111 | + m_sc = new SealedClass( ); |
| 112 | + } |
| 113 | +
|
| 114 | + return m_sc; |
| 115 | + } |
| 116 | + static void Destroy( ) |
| 117 | + { |
| 118 | + if(m_sc != NULL) |
| 119 | + { |
| 120 | + delete m_sc; |
| 121 | + } |
| 122 | + } |
| 123 | +private : |
| 124 | + SealedClass( ){ } |
| 125 | + ~SealedClass( ){ } |
| 126 | +
|
| 127 | +
|
| 128 | + static SealedClass *m_sc; |
| 129 | +}; |
| 130 | +
|
| 131 | +
|
| 132 | +SealedClass* SealedClass::m_sc = NULL; |
| 133 | +
|
| 134 | +
|
| 135 | +class Base : public SealedClass |
| 136 | +{ |
| 137 | +}; |
| 138 | +
|
| 139 | +
|
| 140 | +int main( ) |
| 141 | +{ |
| 142 | + SealedClass *pb = SealedClass::GetInstance( ); |
| 143 | + SealedClass::Destroy( ); |
| 144 | +
|
| 145 | + pb = NULL; |
| 146 | +
|
| 147 | + //Base b; // error |
| 148 | + return 0; |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | +#5 友元实现 |
| 156 | +------- |
| 157 | + |
| 158 | +##5.1 友元函数实现 |
| 159 | +------- |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | +```cpp |
| 164 | +#include <iostream> |
| 165 | + |
| 166 | +using namespace std; |
| 167 | + |
| 168 | + |
| 169 | +class SealedClass |
| 170 | +{ |
| 171 | +private : |
| 172 | + SealedClass( ){ } |
| 173 | + ~SealedClass( ){ } |
| 174 | + |
| 175 | +public : |
| 176 | + friend SealedClass* GetInstance( ); |
| 177 | + |
| 178 | +}; |
| 179 | + |
| 180 | + |
| 181 | +SealedClass* GetInstance( ) |
| 182 | +{ |
| 183 | + return new SealedClass( ); |
| 184 | +} |
| 185 | + |
| 186 | + |
| 187 | +class Base : public SealedClass |
| 188 | +{ |
| 189 | +}; |
| 190 | + |
| 191 | +int main( ) |
| 192 | +{ |
| 193 | + SealedClass *p = GetInstance( ); |
| 194 | + |
| 195 | + //Base base; // error |
| 196 | + |
| 197 | + return 0; |
| 198 | +} |
| 199 | +``` |
| 200 | +
|
| 201 | +##5.2 友元类实现 |
| 202 | +------- |
| 203 | +
|
| 204 | +```cpp |
| 205 | +#include <iostream> |
| 206 | +using namespace std; |
| 207 | +
|
| 208 | +
|
| 209 | +
|
| 210 | +class CNoHeritance |
| 211 | +{ |
| 212 | + friend class SealedClass; |
| 213 | +private: |
| 214 | + CNoHeritance(){ } |
| 215 | + ~CNoHeritance(){ } |
| 216 | +}; |
| 217 | +
|
| 218 | +
|
| 219 | +
|
| 220 | +class SealedClass : virtual public CNoHeritance |
| 221 | +{ |
| 222 | +public: |
| 223 | + SealedClass( ){ } |
| 224 | +
|
| 225 | + ~SealedClass( ){ } |
| 226 | +}; |
| 227 | +
|
| 228 | +
|
| 229 | +/* |
| 230 | +class Base : public SealedClass |
| 231 | +{ |
| 232 | +public: |
| 233 | + Base():SealedClass( ){ } |
| 234 | + ~Base(){ } |
| 235 | +}; |
| 236 | +*/ |
| 237 | +
|
| 238 | +int main( ) |
| 239 | +{ |
| 240 | + SealedClass sc; |
| 241 | + //Base base; |
| 242 | +
|
| 243 | + return 0; |
| 244 | +} |
| 245 | +``` |
| 246 | + |
| 247 | +#6 总结 |
| 248 | +------- |
| 249 | + |
| 250 | +```cpp |
| 251 | +#include <iostream> |
| 252 | +using namespace std; |
| 253 | + |
| 254 | +template <typename T> |
| 255 | +class CNoHeritance |
| 256 | +{ |
| 257 | + friend T; |
| 258 | +private: |
| 259 | + CNoHeritance(){ } |
| 260 | + ~CNoHeritance(){ } |
| 261 | +}; |
| 262 | + |
| 263 | + |
| 264 | +class SealedClass : virtual public CNoHeritance<SealedClass> |
| 265 | +{ |
| 266 | +public: |
| 267 | + SealedClass( ){ } |
| 268 | + |
| 269 | + ~SealedClass( ){ } |
| 270 | +}; |
| 271 | + |
| 272 | +/* |
| 273 | +class Base : public SealedClass |
| 274 | +{ |
| 275 | +public: |
| 276 | + Base():SealedClass( ){ } |
| 277 | + ~Base(){ } |
| 278 | +}; |
| 279 | +*/ |
| 280 | + |
| 281 | +int main( ) |
| 282 | +{ |
| 283 | + SealedClass sc; |
| 284 | + //Base base; |
| 285 | + |
| 286 | + return 0; |
| 287 | +} |
| 288 | +``` |
0 commit comments