• string的学习


    #pragma once
    #include
    //建立一个命名空间,防止和库里的string 冲突
    namespace chen
    {
    	class string
    	{
    	public:
    
    		//迭代器
    		//非const版本 
    		typedef char* iterator;
    	  
    		iterator begin()
    		{
    			return _str;
    		}
    			 
    		iterator end()
    		{
    			return _str+_size;
    		}
    
    		//const版本
    		typedef const char* const_iterator;
    
    		const_iterator begin() const
    		{
    			return _str;
    		}
    
    		const_iterator end() const
    		{
    			return _str + _size;
    		}
    
    		初始化,无参的构造函数
    		//string()
    		//	:_str(new char[1]{'\0'})
    		//	,_size(0)
    		//	,_capacity(0)
    		//{
    		//	// _str[0] = '\0'; //这是一种初始化的方式,你也可以在初始化列表中初始化
    		//}
    
    		// 初始化 带参数的构造函数 最好给全缺省
    		// string(const char* str = '\0') // 第一种
    	    // 不行,因为类型不一样,'\0' 是 char类型,而前面的是 const char*
    		// 可以用常量字符串的方式来初始化------第二种
    		 string(const char* str = "") // 这样就行了
    		//string(const char* str)
    			:_size(strlen(str))
    			,_capacity(_size)
    			
    		{
    			_str = new char[_capacity + 1];
    			strcpy(_str, str);
    		}
    		 
    
    		 //拷贝构造
    		 string(const string&s)
    		 {
    			 _str = new char[s._capacity+1];
    			 strcpy(_str, s._str);
    			 _size = s._size;
    			 _capacity = s._capacity;
    		 }
    
    
    		//拷贝构造的 现代写法  第一种方式
    				string(const string& s) {
    					string tmp(s._str);
    					swap(_str, tmp._str);
    					swap(_size, tmp._size);
    					swap(_capacity, tmp._capacity);
    
    				}
    
    				//第二种方式
    				void swap(string& s) {
    					std::swap(_str, s._str);
    					std::swap(_size, s._size);
    					std::swap(_capacity, s._capacity);
    				}
    
    				string(const string& s) 
    				:_str(nullptr)
    				,_size(0)
    				,_capacity(0)
    				{
    					string tmp(s._str);
    					swap(tmp);
    				}
    
    		  
    
    		 //赋值  
    		 string& operator=(const string& s){
    			 if (this != &s) {
    				 char* tmp = new char[s._capacity + 1];
    				 strcpy(tmp, s._str);
    				 delete[] _str; //将旧空间释放
    				 _str = tmp;    //赋值给新的空间
    				 _size = s._size;
    				 _capacity = s._capacity;
    			 }
    			 return *this;
    		 }
    		  
    		 
    		 //赋值   现代写法 第一种
    		 string& operator=(const string& s) {
    			 if (this != &s) {
    				 string tmp(s);
    				 swap(tmp);
    			 }
    			 return *this;
    		 }
    
    		 //赋值   现代写法 第二种 传值传参要调用拷贝构造
    		 string& operator=( string tmp) {
    			 swap(tmp);
    			 return *this;
    		 }
    
    
    		//析构函数
    		~string()
    		{
    			delete[] _str; 
    			_str = nullptr;
    			_size = _capacity = 0; 
    		}    
    
    		//运算符重载,返回pos位置的值
    	     char& operator[](size_t pos) 
    		{
    			assert(pos < _size);
    			return _str[pos];
    		}
    
    		//运算符重载,返回pos位置的值 const版本
    		const char& operator[](size_t pos) const
    		{
    			assert(pos < _size);
    			return _str[pos];
    		}
    
    		//提供一下大小
    		size_t size() const   
    		{
    			return _size; 
    		}
    
    		size_t capacity() const
    		{
    			return _capacity;
    		}
    
    		const char* c_str() const
    		{
    			return _str;
    		} 
    
    
    		void reserve(size_t n)
    		{
    			if (n > _capacity) //下面的new不用检查失败,因为如果失败的话,他会抛异常
    			{
    				char* tmp = new char[n + 1] ;  // 这个 1 是留给 '\0'的;
    				strcpy(tmp, _str);//会把'\0' 也拷过去
    
    				//释放旧空间
    				delete[] _str;
    				//指向新的空间
    				_str = tmp;
    				  
    				_capacity = n; 
    			}
    		}
    
    
    		void resize(size_t n, char ch = '\0')
    		{
    			if (n <= 5)
    			{
    				_str[n] = '\0';
    				_size = n;
    			}
    			else
    			{
    				reserve(n);
    				while (_size < n)
    				{
    					_str[_size] = ch;
    					++_size;
    				}
    				_str[_size] = '\0';
    			}
    		}
    
    		//fing 系列
    
    		//查找一个字符
    		size_t find(char ch, size_t pos = 0)
    		{
    			for (size_t i = pos; i < _size; i++)
    			{
    				if (_str[i] = ch)
    				{
    					return i;
    				}
    			}
    			return npos;
    		}
    
    		//查找一个字符串
    		size_t find(const char*sub, size_t pos = 0)
    		{
    			// 子串匹配 kmp 但是实际用的少,效率更高的是字符串BM算法
    			//我们这里用strstr;
    			const char* p = strstr(_str, sub);
    			if (p)
    			{
    				return p - _str; //指针相减得到的是数字
    			} 
    			else
    			{
    				return npos;
    			}
    
    			return npos;
    		}
    
    		//提取一个字符串中的子字符串,从pos位置开始,取len个字符
    		string substr(size_t pos, size_t len = npos)
    		{
    			string s;
    			size_t  end = pos + len;
    			if (len == npos || pos + len >= _size) //有多少就取多少的情况
    			{
    				len = _size - pos;
    				end = _size;
    
    			}
    
    			s.reserve(len);
    			for (size_t i = pos; i < end; i++)
    			{
    				s += _str[i];
    			}
    			return s;
    		}
    
    		//尾插  注意如果容量不够就需要扩容
    		void push_back(char ch)
    		{
    			if (_size == _capacity)
    			{
    				reserve(_capacity == 0? 4 :_capacity * 2);
    		    }
    
    			_str[_size] = ch;
    			_size++;
    			_str[_size] = '\0';
    		}
    
    		//追加(也相当于是尾插) 注意容量不够的话还是得扩容
    		void append(const char* str)
    		{
    			//要插入的字符串的长度
    			size_t len = strlen(str);
    			
    			if (_size + len > _capacity)
    			{
    				reserve(_capacity * 2);
    			}  
    
    			strcpy(_str+_size, str);  //strcpy会自动的'\0' 也拷贝过去
    			_size += len;
    
    		}
    
    		//运算符重载 +=
    		string& operator+=(char ch)
    		{
    			push_back(ch);
    			return *this;
    		}
    
    		//运算符重载 +=
    		string& operator+=(const char* str)
    		{
    			append(str);
    			return *this;
    		}
    
           //在某个位置插入一个字符
    		void insert(size_t pos, char ch)
    		{
    			assert(pos <= _size);
    
    			if (_size == _capacity)
    			{
    				reserve(_capacity == 0 ? 4 : _capacity * 2);
    			}
    
    			size_t end = _size + 1;
    			while (end > pos)
    			{
    				_str[end] = _str[end-1];
    				--end;
    				 
    			}
    			_str[pos] = ch;
    			_size++;
    		}
    
    		//在pos位置插入一个字符串
    		void insert(size_t pos, char* str)
    		{
    			assert(pos <= _size);
    			size_t len = strlen(str);
    			if (_size + len > _capacity)
    			{
    				reserve(_size + len);
    			}
    
    			//挪动数据
    			int end = _size;
    			while (end >= (int)pos)
    			{
    				_str[end + len] = _str[end];
    				end--;
    			}
    			//拷数据进去 不能用strcpy 因为strcpy会把'\0'也拷进去,会出事,所以我们用strncpy
    			strncpy(_str + pos, str, len);
    			_size += len;
    
    
    
    
    		}
    
    		//删除容器中某个位置元素后的所有元素
    		void erase(size_t pos, size_t len = npos)
    		{
    			assert(pos < _size);
    			if (len == npos || pos + len >= _size) //把pos后面的全部删完
    			{
    				_str[pos] = '\0';
    				_size = pos;
    			}
    			else //只删除pos后面的一小部分,我们就要挪数据覆盖
    			{
    				size_t begin = pos + len;
    				while (begin < _size)
    				{
    					_str[pos] = _str[begin];
    					begin++;
    					pos++;
    				}
    				_size -= len;
    			}
    
    		}
    
    
    
    		bool operator<(const string& s) const
    		{
    			return strcmp(_str, s._str) < 0;
    		}
    
    		bool operator==(const string& s) const 
    		{
    			return strcmp(_str, s._str) == 0;
    		}
    
    		bool operator<=(const string& s) const
    		{
    			return *this < s || *this == s;
    		}
    
    		bool operator>(const string& s) const
    		{
    			return !(*this <= s);
    		}
    
    		bool operator>=(const string& s) const
    		{
    			return !(*this <= s);
    		}
    
    		bool operator!=(const string& s) const
    		{
    			return !(*this == s);
    		}
    
    
    		//清除数据
    		void clear()
    		{
    			_str[0] = '\0';
    		   
    			_size = 0;
    		}
    
    	private:    
    		char* _str;   
    		size_t _size;
    		size_t _capacity;
    		const static size_t npos; //静态成员变量在类中声明,类外定义
    	};
    
    	//静态成员变量的定义
    	const static size_t npos = size_t(-1);
    
    	//流插入 第一种写法
    	ostream& operator<<(ostream& out, const string& s)
    	{
    		for (size_t i = 0; i < s.size(); i++)
    		{
    			out << s[i];
    		}
    		return out;
    	}
    
    	流插入 第二种写法
    	//ostream& operator<<(ostream& out, const string& s)
    	//{
    	//	for (auto ch : s)
    	//		out << ch;
    	//	return out;  
    	//}
    
    	//流提取 
    	ostream& operator>>(ostream& in, const string& s)
    	{
    		s.clear();  
    		char ch;
    		ch = in.get();
    
    		while (ch != ' ' && ch != '  \n')
    		{
    			s +=  ch;
    			ch = in.get();
    		}
    		return in;
    	}
    
    
    
    	//流提取   改进版
    	ostream& operator>>(ostream& in, const string& s)
    	{
    		s.clear();
    
    		char buff[129];
    		size_t i = 0;
    
    		char ch;
    		ch = in.get();
    
    		while (ch != ' ' && ch != '  \n')
    		{
    			buff[i++] = ch;
    			if (i == 128) {
    				buff[i] = '\0';
    				s += buff;
    				i == 0;
    			}
    			ch = in.get();
    		}
    
    		if (i != 0) {
    			buff[i] = '\0';
    			s += buff;
    		}
    		return in;
    	}
    
    
    
    
    	void test_string1()
    	{
    		string s1("hello world");
    		cout << s1.c_str() << endl;
    
    		/*string s2;
    		cout << s2.c_str() << endl;*/
    
    		for (size_t i = 0; i < s1.size(); i++)
    		{
    			cout << s1[i] << " ";
    		}
    		cout << endl;
    	
    		string::iterator it = s1.begin();
    		while (it != s1.end())
    		{
    			cout << *it << " ";
    			it++;
    		}
    		cout << endl;
    
    		for (auto ch : s1)
    		{
    			cout << ch << " ";
    		}
    		cout << endl;
    	} 
    
    
    
    	void test_string2()
    	{
    		string s1("hello word");
    		cout << s1.c_str() << endl;  
    
    		s1.push_back(' ');
    		s1.append("hello laochen");
    
    		cout << s1.c_str() << endl;
    
    		s1 += '*';
    		s1 += '*';
    		s1 += '*';
    		s1 += '*';
    		s1 += '*';
    		s1 += '*';
    		cout << s1.c_str() << endl;
    	}
    
    
    	void test_string3()
    	{
    		string s1("hello world");
    		cout << s1.c_str() << endl;
    
    		s1.insert(5, '%'); //第5个位置 插入一个百分号
    	    cout << s1.c_str() << endl;
    	}
    
    	void test_string5()
    	{
    		string s1("hello world");
    		s1.insert(5, "abc");
    		cout << s1 << endl;
    		s1.erase(0, 3);
    		cout << s1 << endl;
    	}
    
    	void test_string6()
    	{
    		string s1("hello world");
    		s1.resize(5);
    		cout << s1 << endl;
    
    		s1.resize(25, 'x');
    		
    	}
    
    	void test_string8()
    	{
    		string s1("hello world");
    
    		string s2 = s1; //用一个已经存在的对象去初始化一个新的对象是拷贝构造
    		
    		cout << s1 << endl;
    		cout << s2 << endl;
    		
    		string s3("XXXXXXXXXXXXXXXXXXXXXXX");
    		s1 = s3;//这里是赋值
    	}
    
    	void test_string9() {
    		string s1("hello world");
    		cin >> s1;
    		cout << s1 << endl;
    	}
    
    }
    
    
    • 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
  • 相关阅读:
    你还不会用数据库吗?一篇文章带你入门!!!#sql #Mysql
    二叉树中的topk问题(带图详解)
    x-api接口鉴权架构设计和实现【万字+深度】
    大数据Doris(十):添加BE步骤
    Xilinx FPGA 程序固化重新上电程序不运行的问题
    C/C++|智能指针的shared_from_this和enable_shared_from_this
    4.如何终止线程
    强化学习——策略梯度理解点
    黑客帝国代码雨
    OneNote 深度评测:使用资源、插件、模版
  • 原文地址:https://blog.csdn.net/laochendashuaibi/article/details/132792634