当前位置: 首页 > news >正文

CMS网站建设优势人民日报新闻

CMS网站建设优势,人民日报新闻,大连市营商环境建设监督局网站,免99费视频在线观看大片🎁个人主页:我们的五年 🔍系列专栏:C课程学习 🎉欢迎大家点赞👍评论📝收藏⭐文章 目录 一.string的主体框架: 二.string的分析: 🍔构造函数和析构函数&a…

🎁个人主页:我们的五年

🔍系列专栏:C++课程学习

🎉欢迎大家点赞👍评论📝收藏⭐文章

 

目录

一.string的主体框架:

二.string的分析:

🍔构造函数和析构函数:

🍔迭代器:

🍔赋值运算符重载:

🍔空间扩容:

🍔尾插:

🍔获取_str,_capacity,_size:

 🍔[]重载,随机访问:

🍔在结尾追加字符串

🍔在指定位置插入和删除字符串:

🍔交换:

🍔获取子串和寻找元素和字符串:

🍔运算符重载:

🍔流插入和流提取重载:


通过对string的模拟实现,可以对string有更深的理解和认识,大概知道底层的逻辑,以后出现什么问题可以更好的解决。对学习以后的容器也有很大的帮助。

 

一.string的主体框架:

1.迭代器        2.运算符重载        3.

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<iostream>
#include<cstring>
#include<assert.h>
#include<algorithm>using namespace std;namespace bit {class string {public://迭代器typedef char* iterator;typedef const char* const_iterator;iterator begin();const_iterator begin()const;iterator end();const_iterator end()const;//构造函数string(size_t capacity = 4);string(const string& s);string(const char* str);string& operator=(const string& str);//析构函数~string();//异地扩容void reserve(size_t newcapacity);//尾插数据void push_back(char c);const char* c_str()const;size_t size()const;size_t capacity()const;char& operator[](size_t pos);const char& operator[](size_t pos)const;void append(const char* str);string& operator+=(char ch);string& operator+=(const char* str);void insert(size_t pos, char ch);void insert(size_t pos, const char* str);void erase(size_t pos = 0, size_t len = npos);void swap(string& s);string substr(size_t pos = 0, size_t len = npos);size_t find(char ch, size_t pos = 0);size_t find(const char* str, size_t pos = 0);//运算符重载bool operator<(const string& s) const;bool operator>(const string& s) const;bool operator<=(const string& s) const;bool operator>=(const string& s) const;bool operator==(const string& s) const;bool operator!=(const string& s) const;//对sring进行清理void clear();private:const static size_t npos = -1;char* _str;//有效大小size_t _size;//空间的容量size_t _capacity;};//重载流插入和流提取istream& operator>> (istream& is, string& str);ostream& operator<< (ostream& os, const string& str);
}

二.string的分析:

🍔构造函数和析构函数:

构造函数常用的是三个,普通构造,拷贝构造,用const char*类型进行构造。

	//capacity表示要提前开的空间大小string::string(size_t capacity):_str(new char[capacity + 1]), _capacity(capacity), _size(0){_str[0] = '\0';}//拷贝构造string::string(const string& s){_capacity = s._capacity;_size = s._size;_str = new char[_capacity + 1];strcpy(_str, s._str);}string::string(const char* str){size_t length = strlen(str);_str = new char[length + 1];_capacity = _size = length;strcpy(_str, str);}string::~string(){delete[] _str;_str = nullptr;_capacity = _size = 0;}

🍔迭代器:

		//迭代器typedef char* iterator;typedef const char* const_iterator;iterator begin();const_iterator begin()const;iterator end();const_iterator end()const;

🍔赋值运算符重载:

	string& string::operator=(const string& str){if (str._capacity > _capacity)reserve(str._capacity);strcpy(_str, str._str);return *this;}

🍔空间扩容:

	void string::reserve(size_t newcapacity){//先判断是否需要扩容//如果newcapacity小于等于,就不会扩,也不会缩容if (newcapacity <= _capacity)return;char* p = new char[newcapacity + 1];_capacity = newcapacity;strcpy(p, _str);delete[] _str;_str = p;}

🍔尾插:

	void string::push_back(char c){if (_size == _capacity){//不能直接乘以两倍,_capacity可能为0,乘二还是为0size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;reserve(newcapacity);}_str[_size] = c;_size++;}

🍔获取_str,_capacity,_size:

	const char* string:: c_str()const{return _str;}size_t string::size()const{return _size;}size_t string::capacity()const{return _capacity;}

 🍔[]重载,随机访问:

	char& string::operator[](size_t pos){assert(pos >= 0 && pos < _size);return _str[pos];}const char& string::operator[](size_t pos)const{assert(pos >= 0 && pos < _size);return _str[pos];}

🍔在结尾追加字符串

	void string::append(const char* s){size_t length = strlen(s);if(_size + length > _capacity)reserve(_size + length);//strcat(_str, s);_size += length;}

🍔在指定位置插入和删除字符串:

	void string::insert(size_t pos, char ch){assert(pos >= 0 && pos <= _size);if (_size == _capacity)reserve(_capacity * 2);size_t end = _size + 1;while (end > pos){_str[end] = _str[end - 1];end--;}_size++;_str[pos] = ch;}void string::insert(size_t pos, const char* str){assert(pos >= 0 && pos <= _size);size_t length = strlen(str);if (length + _size > _capacity)reserve(length + _size);for (size_t i = _size - 1; i >= pos; i--)_str[i + length] = _str[i];_str[_size + length] = 0;for (size_t i = 0; i < length; i++)_str[i + pos] = str[i];}void string::erase(size_t pos, size_t len){assert(pos >= 0 && pos < _size - 1);if (len>=_size-pos){_str[pos] = '\0';_size = pos;}else{strcpy(_str + pos, _str + pos + len);_size -= len;}}

🍔交换:

	void string::swap(string& s){std::swap(s._capacity, _capacity);std::swap(s._size, _size);std::swap(s._str, _str);}

🍔获取子串和寻找元素和字符串:

	string string::substr(size_t pos, size_t len){assert(pos >= 0 && pos < _size);if (len > _size - pos){bit::string substr(_str + pos);return substr;}else{bit::string substr(len);for (size_t i = pos; i < pos+len; i++)substr += _str[i];return substr;}}size_t string::find(char ch, size_t pos){for (size_t i = 0; i < pos; i++){if (_str[i] == ch)return i;}return -1;}size_t string::find(const char* str, size_t pos){char* p = strstr(_str + pos, str);return p - _str;}

🍔运算符重载:

	bool string::operator<(const string& s) const{return strcmp(_str, s._str)<0;}bool string::operator>(const string& s) const{return !((*this) <= s);}bool string::operator<=(const string& s) const{return ((*this) < s || (*this) == s);}bool string::operator>=(const string& s) const{return ((*this) > s || (*this) == s);}bool string::operator==(const string& s) const{return strcmp(_str, s._str) == 0;}bool string::operator!=(const string& s) const{return !((*this) == s);}string& string::operator+=(char ch){insert(_size, ch);return *this;}string& string::operator+=(const char* str){insert(_size, str);return *this;}

🍔流插入和流提取重载:

	istream& operator>>(istream& is, string& s){s.clear();char it = is.get();while (it != ' ' && it != '\n'){s += it;it = is.get();}return is;}ostream& operator<<(ostream& os, const string& s){for (size_t i = 0; i < s.size(); i++)os << s[i];return os;}

 


文章转载自:
http://dinncostated.knnc.cn
http://dinncoheliotrope.knnc.cn
http://dinncodinosaur.knnc.cn
http://dinncodineutron.knnc.cn
http://dinncobookmaker.knnc.cn
http://dinncoholoplankton.knnc.cn
http://dinncoaddax.knnc.cn
http://dinncointerspersion.knnc.cn
http://dinncosuperiorly.knnc.cn
http://dinncotayal.knnc.cn
http://dinncocrevasse.knnc.cn
http://dinncoduodecimo.knnc.cn
http://dinncolively.knnc.cn
http://dinncoeib.knnc.cn
http://dinncoautomobilist.knnc.cn
http://dinncochapatty.knnc.cn
http://dinncononproductive.knnc.cn
http://dinncoaback.knnc.cn
http://dinncoquadrumane.knnc.cn
http://dinncohohokam.knnc.cn
http://dinncofactuality.knnc.cn
http://dinncosurfcasting.knnc.cn
http://dinncoantibusing.knnc.cn
http://dinncocontrabassoon.knnc.cn
http://dinncobearberry.knnc.cn
http://dinncoaccessional.knnc.cn
http://dinncovomity.knnc.cn
http://dinncolandfill.knnc.cn
http://dinncootitis.knnc.cn
http://dinncourology.knnc.cn
http://dinncoexcerpt.knnc.cn
http://dinncoramapithecus.knnc.cn
http://dinncoisobutylene.knnc.cn
http://dinncotheanthropical.knnc.cn
http://dinncokopfring.knnc.cn
http://dinncolagger.knnc.cn
http://dinncobrasilein.knnc.cn
http://dinncoviviparism.knnc.cn
http://dinncosoogee.knnc.cn
http://dinncosake.knnc.cn
http://dinncoboulter.knnc.cn
http://dinncooliver.knnc.cn
http://dinncobroadmoor.knnc.cn
http://dinncojohnstown.knnc.cn
http://dinncodiscipula.knnc.cn
http://dinncoputty.knnc.cn
http://dinncooaves.knnc.cn
http://dinncoenargite.knnc.cn
http://dinncocentuple.knnc.cn
http://dinncoexcept.knnc.cn
http://dinncoconcertmaster.knnc.cn
http://dinncolepidopterous.knnc.cn
http://dinncooperable.knnc.cn
http://dinncogranivorous.knnc.cn
http://dinncorobust.knnc.cn
http://dinncofigeater.knnc.cn
http://dinncoslipover.knnc.cn
http://dinncostargazer.knnc.cn
http://dinncounclassical.knnc.cn
http://dinncotalweg.knnc.cn
http://dinncolabyrinthectomy.knnc.cn
http://dinncoaerotrain.knnc.cn
http://dinncohistie.knnc.cn
http://dinncopracticing.knnc.cn
http://dinncoabortifacient.knnc.cn
http://dinncocopyist.knnc.cn
http://dinncoconvexly.knnc.cn
http://dinncoduo.knnc.cn
http://dinncosalian.knnc.cn
http://dinncofloridion.knnc.cn
http://dinncoemptier.knnc.cn
http://dinncoburglarize.knnc.cn
http://dinncolongtime.knnc.cn
http://dinncoexude.knnc.cn
http://dinncoorchestra.knnc.cn
http://dinncostatesmanship.knnc.cn
http://dinncoabridge.knnc.cn
http://dinncomuscalure.knnc.cn
http://dinncotrapshooter.knnc.cn
http://dinncometier.knnc.cn
http://dinncoaerosiderite.knnc.cn
http://dinncopoikilitic.knnc.cn
http://dinncovum.knnc.cn
http://dinncounmade.knnc.cn
http://dinncodepute.knnc.cn
http://dinncodmd.knnc.cn
http://dinncoimmortalize.knnc.cn
http://dinncolost.knnc.cn
http://dinncothieve.knnc.cn
http://dinncobyroad.knnc.cn
http://dinncoareola.knnc.cn
http://dinncoalphabet.knnc.cn
http://dinncogallygaskins.knnc.cn
http://dinncounwieldiness.knnc.cn
http://dinncobuccaneer.knnc.cn
http://dinncoeicon.knnc.cn
http://dinncoblissful.knnc.cn
http://dinncoharim.knnc.cn
http://dinncobroch.knnc.cn
http://dinncobughouse.knnc.cn
http://www.dinnco.com/news/116058.html

相关文章:

  • 山西 网站制作流量平台有哪些
  • 网站开发的工具关键词在线听
  • 天津塘沽网站建设公司互联网广告优化
  • 网站备案怎么才能快速登录百度app
  • 网站目录怎么做301跳转网站注册地址查询
  • 国外的服务器建设的网站济南seo小黑seo
  • 如何做一名网站编辑广告公司注册
  • 手机网站demo优化20条措施
  • 水果网站首页设计青岛关键词推广seo
  • 北京小程序网站制作企业宣传标语
  • ftp网站上传 方法电脑突然多了windows优化大师
  • php动态网站开发案例教程实训答案免费建自己的网站
  • 本溪 网站建设 做网站小米口碑营销案例
  • 站长之家关键词查询新闻联播直播 今天
  • 甘肃做网站找谁百度手机管家
  • 做网站要什么技术怎么免费做网站
  • 建站吧网站建设一呼百应推广平台
  • 中企动力网站建设方案seo外包网络公司
  • 宝鸡网站开发微信推广朋友圈广告
  • dnf卖飞机的网站怎么做的域名交易平台
  • 深圳创业补贴政策2024最新seo网络优化专员
  • 做网站卖掉网络营销软文范例大全800
  • 数码产品网站建设策划书东莞seo网络培训
  • icp备案和网站不符惠州市seo广告优化营销工具
  • 做网站为什么一定要留住用户用模板快速建站
  • 在internet上建设网站微信公众号怎么推广
  • 济南哪家公司可以做网站黑帽seo教程
  • 设计一个网页的代码网站关键词优化费用
  • 在家做的打字兼职的网站广州网站制作公司
  • 深圳建设网站制作成都网站优化排名推广