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

php 建网站网络营销项目策划书

php 建网站,网络营销项目策划书,建小程序需要网站吗,广东新闻联播目录 🌞专栏导读 🌛定义string类 🌛构造函数 🌛拷贝构造函数 🌛赋值函数 🌛析构函数 🌛[]操作符重载 🌛c_str、size、capacity函数 🌛比较运算符重载 &#…

 

目录

🌞专栏导读

🌛定义string类

 🌛构造函数

🌛拷贝构造函数

🌛赋值函数

🌛析构函数 

🌛[]操作符重载 

🌛c_str、size、capacity函数 

🌛比较运算符重载 

 🌛resize与reserve函数

🌛push_back、append函数 

🌛insert函数 

🌛erase函数

🌛find函数

 🌛swap函数

🌛clean函数

 🌛迭代器

🌛>> 与 << 重载


🌞专栏导读

🌟作者简介:日出等日落,在读本科生一枚,致力于 C/C++、Linux 学习。

🌟本文收录于 C++系列,本专栏主要内容为 C++ 初阶、C++ 进阶、STL 详解等,持续更新!

🌟相关专栏推荐:C语言系列 、Linux系列 、数据结构与算法

本章我们将模拟实现string,但不一定非要与库中完全相同。我们将其中重要的、常用的接口进行模拟实现,旨在加深string类的学习与记忆。 

🌛定义string类

为了区别于标准库中的string,这里使用自己的命名空间,在自己的命名空间模拟实现string

string包含这三个基本成员

  • char* _str 字符数组;
  • size_t _size 有效字符大小;
  • size_t _capacity 容量;

 此外还需声明一个static成员nposnpos为将来实现的某些成员函数的缺省值,值位-1

namespace yxb
{class string{public:private:char* _str;size_t _capacity;size_t _size;//类中声明static const size_t npos;};//类外定义const size_t string::npos = -1;
}

 🌛构造函数

//构造函数string(const char* str = "")  //使用缺省值:_size(strlen(str)){_capacity = _size == 0 ? 3 : _size;  //_capacity初始值不能为0_str = new char[_capacity + 1];  //为'\0'预留位置strcpy(_str, str);}

注意

  • _capacity的值不能初始化为0,因为扩容时可能出现0*n=0的情况。

🌛拷贝构造函数

拷贝构造虽然编译器会自动实现,但是自动实现的拷贝构造为浅拷贝,对于string类中,成员变量会申请资源的情况,浅拷贝是行不通的,所以需要我们自己实现。

//拷贝构造s3(s2)string(const string& s):_size(s._size),_capacity(s._capacity){_str = new char[s._capacity + 1];strcpy(_str, s._str);}

🌛赋值函数

	//s1 = s3   s1 = s1string& 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(){delete[] _str;_str = nullptr;_capacity = _size = 0; }

🌛[]操作符重载 

注意应对const对象与非const对象须实现不同的重载函数。

        char& operator[](size_t pos){assert(pos < _size);return _str[pos];}const char& operator[](size_t pos)const{assert(pos < _size);return _str[pos];}

🌛c_str、size、capacity函数 

  • c_str:返回C风格的字符串。
  • size:返回_size;
  • capacity:返回_capacity;
	const char* c_str(){return _str;}size_t size() const{return _size;}size_t capacity() const{return _capacity;}

🌛比较运算符重载 

	//比较运算符重载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);}

 🌛resize与reserve函数

  • resize:扩容并初始化;
  • reserve:只扩容;
//扩容void reverse(size_t n){if (n > _capacity){char* tmp = new char[n + 1];strcpy(tmp, _str);delete[] _str;_str = tmp;_capacity = n;}}//扩容+初始化void resize(size_t n, char ch = '\0'){if (n <= _size){//删除数据,保留前n个_size = n;_str[_size] = '\0';}else{if (n > _capacity){reverse(n);}size_t i = _size;while (i < n){_str[i++] = ch;}_size = n;_str[_size] = '\0';}}

🌛push_back、append函数 

  • push_back:尾插一个字符;
  • append:尾插一个字符串;
  • +=:尾插一个字符或字符串;
void push_back(char ch){if (_size + 1 > _capacity){reverse(_capacity * 2);}_str[_size] = ch;_size++;_str[_size] = '\0';}void append(const char* str){size_t len = strlen(str);if (_size + len > _capacity){reverse(_size + len);}strcpy(_str + _size, str);_size += len;}string& operator+=(char ch){push_back(ch);return *this;}string& operator+=(const char* str){append(str);return *this;}

🌛insert函数 

  • insert:在pos位置插入一个字符或字符串;
	void insert(size_t pos, char ch){assert(pos <= _size);if (_size + 1 > _capacity){reverse(2 * _capacity);}size_t end = _size + 1;while (pos < end){_str[end] = _str[end - 1];end--;}_str[pos] = ch;_size++;}void insert(size_t pos, const char* str){assert(pos <= _size);size_t len = strlen(str);if (_size + len > _capacity){reverse(_size + len);}//挪动数据size_t end = _size + len;while ( end > pos +len-1 ){_str[end] = _str[end-len];end--;}//拷贝插入 strncpy(_str + pos, str, len);_size += len;}

🌛erase函数

  • erase:删除pos位置向后的n个字符;
	void erase(size_t pos, size_t len = npos){if (len == npos || pos + len >= _size){_str[pos] = '\0';_size = pos;}//0123456789else{strcpy(_str + pos, _str + pos + len);_size -= pos;}}

🌛find函数

  • find:从pos位置开始向后查找指定字符或字符串,并返回起始位置的下标。
	size_t find(char ch, size_t pos = npos){assert(pos < _size);for (size_t i = pos; i < _size; ++i){if (_str[i] == ch){return i;}}return npos;}size_t find(const char* str, size_t pos = npos){assert(pos < _size);char* p = strstr(_str, str);if (p == nullptr){return npos;}else{return p - str;}}

 🌛swap函数

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

🌛clean函数

  • clean:清理数据;
	void clean(){_str[0] = '\0';_size = 0;}

 🌛迭代器

	//迭代器typedef char* iterator;typedef const char* const_iterator;iterator begin(){return _str;}iterator end(){return _str + _size;}const_iterator begin() const{return _str;}const_iterator end()const{return _str + _size;}

🌛>> 与 << 重载

注意这两个函数须定义在类外

    ostream& operator<<(ostream& out, const string& str){for (auto ch : str){out << ch;}return out;}istream& operator>>(istream& in, string& str){str.clean();char ch = in.get();char buff[128]; //避免因频繁扩容导致效率过低size_t i = 0;while (ch != ' ' && ch != '\n'){buff[i++] = ch;if (i == 127){buff[127] = '\0';str += buff;i = 0;}ch = in.get();}if (i != 0){buff[i] = '\0';str += buff;}return in;}

🌛完整代码

#pragma once
#include<assert.h>namespace yxb
{class string{public:typedef char* iterator;typedef const char* const_iterator;iterator begin(){return _str;}iterator end(){return _str + _size;}const_iterator begin()const{return _str;}const_iterator end()const{return _str + _size;}/*string():_str(new char[1]), _size(0),_capacity(0){_str[0] = '\0';}*/string(const char* str = ""):_size(strlen(str)){_capacity = _size == 0 ? 3 : _size;_str = new char[_capacity + 1];strcpy(_str, str);}//拷贝构造s3(s2)string(const string& s):_size(s._size),_capacity(s._capacity){_str = new char[s._capacity + 1];strcpy(_str, s._str);}//s1 = s3   s1 = s1string& 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(){delete[] _str;_str = nullptr;_capacity = _size = 0; }const char* c_str(){return _str;}char& operator[](size_t pos){assert(pos < _size);return _str[pos];}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;}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 reverse(size_t n){if (n > _capacity){char* tmp = new char[n + 1];strcpy(tmp, _str);delete[] _str;_str = tmp;_capacity = n;}}//扩容+初始化void resize(size_t n, char ch = '\0'){if (n <= _size){//删除数据,保留前n个_size = n;_str[_size] = '\0';}else{if (n > _capacity){reverse(n);}size_t i = _size;while (i < n){_str[i++] = ch;}_size = n;_str[_size] = '\0';}}void push_back(char ch){if (_size + 1 > _capacity){reverse(_capacity * 2);}_str[_size] = ch;_size++;_str[_size] = '\0';}void append(const char* str){size_t len = strlen(str);if (_size + len > _capacity){reverse(_size + len);}strcpy(_str + _size, str);_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 + 1 > _capacity){reverse(2 * _capacity);}size_t end = _size + 1;while (pos < end){_str[end] = _str[end - 1];end--;}_str[pos] = ch;_size++;}void insert(size_t pos, const char* str){assert(pos <= _size);size_t len = strlen(str);if (_size + len > _capacity){reverse(_size + len);}//挪动数据size_t end = _size + len;while ( end > pos +len-1 ){_str[end] = _str[end-len];end--;}//拷贝插入 strncpy(_str + pos, str, len);_size += len;}void erase(size_t pos, size_t len = npos){if (len == npos || pos + len >= _size){_str[pos] = '\0';_size = pos;}//0123456789else{strcpy(_str + pos, _str + pos + len);_size -= pos;}}size_t find(char ch, size_t pos = npos){assert(pos < _size);for (size_t i = pos; i < _size; ++i){if (_str[i] == ch){return i;}}return npos;}size_t find(const char* str, size_t pos = npos){assert(pos < _size);char* p = strstr(_str, str);if (p == nullptr){return npos;}else{return p - str;}}void swap(string& s){std::swap(_str, s._str);std::swap(_size, s._size);std::swap(_capacity, s._capacity);}void clean(){_str[0] = '\0';_size = 0;}private:char* _str;size_t _capacity;size_t _size;static const size_t npos;};const size_t string::npos = -1;ostream& operator<<(ostream& out, const string& str){for (auto ch : str){out << ch;}return out;}istream& operator>>(istream& in, string& str){str.clean();char ch = in.get();char buff[128]; //避免因频繁扩容导致效率过低size_t i = 0;while (ch != ' ' && ch != '\n'){buff[i++] = ch;if (i == 127){buff[127] = '\0';str += buff;i = 0;}ch = in.get();}if (i != 0){buff[i] = '\0';str += buff;}return in;}void test_string1(){	string s1;string s2("hello world");cout << s1.c_str() << endl;cout << s2.c_str() << endl;s2[0]++;cout << s2.c_str() << endl;}void test_string2(){string s1;string s2("hello world");cout << s1.c_str() << endl;cout << s2.c_str() << endl;s1 = s2;cout << s1.c_str() << endl;cout << s2.c_str() << endl;}void test_string3(){string s1;string s2("hello world");cout << s1.c_str() << endl;cout << s2.c_str() << endl;string::iterator it = s2.begin();while (it != s2.end()){cout << *it << endl;++it;}}void test_string4(){string s1;string s2("hello world");string s3("hello");cout << s1.c_str() << endl;cout << s2.c_str() << endl;cout << (s3 >= s2) << endl;}void test_string5(){string s1;string s2("hello world");s2.append("yyyyyy");cout << s2.c_str() << endl;}void test_string6(){string s1("hhh ");cout << s1.c_str() << endl;s1.resize(10, 'x');cout << s1.c_str() << endl;s1.resize(100, 'y');cout << s1.c_str() << endl;}void test_string7(){string s1("123456789");cout << s1.c_str() << endl;s1.insert(2, "wwww");cout << s1.c_str() << endl;s1.insert(0, "ss");cout << s1.c_str() << endl;}void test_string8(){string s1("123456789");cout << s1.c_str() << endl;s1.erase(4, 2);cout << s1.c_str() << endl;}void test_string9(){string s1;cin >> s1;cout << s1 << endl;}
}


文章转载自:
http://dinncountomb.stkw.cn
http://dinncotchotchke.stkw.cn
http://dinncoprelatical.stkw.cn
http://dinncounperceivable.stkw.cn
http://dinncoleatherworking.stkw.cn
http://dinncohence.stkw.cn
http://dinncomegatron.stkw.cn
http://dinncodecorticate.stkw.cn
http://dinncowats.stkw.cn
http://dinncoending.stkw.cn
http://dinncoscotchwoman.stkw.cn
http://dinncovisceral.stkw.cn
http://dinncoaccountantship.stkw.cn
http://dinncogarni.stkw.cn
http://dinncoephemeralization.stkw.cn
http://dinncocongeal.stkw.cn
http://dinncolithostratigraphic.stkw.cn
http://dinncometastasian.stkw.cn
http://dinncoadvocation.stkw.cn
http://dinncoangus.stkw.cn
http://dinncomundify.stkw.cn
http://dinncochloritize.stkw.cn
http://dinncohaifa.stkw.cn
http://dinncostogie.stkw.cn
http://dinncoexpenses.stkw.cn
http://dinncoevolutionary.stkw.cn
http://dinncoplasterer.stkw.cn
http://dinncopowder.stkw.cn
http://dinncoapogamy.stkw.cn
http://dinncomegatron.stkw.cn
http://dinncotrachea.stkw.cn
http://dinncoinarticulate.stkw.cn
http://dinncohomecoming.stkw.cn
http://dinncoskyward.stkw.cn
http://dinncoelastomeric.stkw.cn
http://dinncothrombi.stkw.cn
http://dinncounguard.stkw.cn
http://dinncobolt.stkw.cn
http://dinncocaber.stkw.cn
http://dinncocentner.stkw.cn
http://dinncomemoirist.stkw.cn
http://dinncospinate.stkw.cn
http://dinncoplayfully.stkw.cn
http://dinncojujutsu.stkw.cn
http://dinncorealist.stkw.cn
http://dinncounderbuild.stkw.cn
http://dinncothromboembolus.stkw.cn
http://dinncoashet.stkw.cn
http://dinncochalkstone.stkw.cn
http://dinncofibrillose.stkw.cn
http://dinncocourtship.stkw.cn
http://dinncoaestivation.stkw.cn
http://dinncomisstate.stkw.cn
http://dinncotelemetry.stkw.cn
http://dinncochequers.stkw.cn
http://dinncomemoirist.stkw.cn
http://dinncocreative.stkw.cn
http://dinncorhochrematician.stkw.cn
http://dinncovistula.stkw.cn
http://dinncosturdily.stkw.cn
http://dinncosamsung.stkw.cn
http://dinncohematose.stkw.cn
http://dinncofairbanks.stkw.cn
http://dinncopardon.stkw.cn
http://dinncodryasdust.stkw.cn
http://dinncoaquatone.stkw.cn
http://dinncototany.stkw.cn
http://dinncosemitic.stkw.cn
http://dinncoapogean.stkw.cn
http://dinncoecchymosis.stkw.cn
http://dinncokinship.stkw.cn
http://dinncobrahmanism.stkw.cn
http://dinncofloorer.stkw.cn
http://dinncoirreplaceability.stkw.cn
http://dinncowistaria.stkw.cn
http://dinncoslimmer.stkw.cn
http://dinncoschlesien.stkw.cn
http://dinncogestalt.stkw.cn
http://dinncofaitaccompli.stkw.cn
http://dinncoretainable.stkw.cn
http://dinncocampion.stkw.cn
http://dinncobeamy.stkw.cn
http://dinncoincoming.stkw.cn
http://dinncotullibee.stkw.cn
http://dinncoroset.stkw.cn
http://dinncosemishrub.stkw.cn
http://dinncoflamboyantism.stkw.cn
http://dinncomiesian.stkw.cn
http://dinncoindagate.stkw.cn
http://dinncoassist.stkw.cn
http://dinncorogallist.stkw.cn
http://dinncocytospectrophotometry.stkw.cn
http://dinncolepidopter.stkw.cn
http://dinncopurificatory.stkw.cn
http://dinncoparamorphism.stkw.cn
http://dinncolisting.stkw.cn
http://dinncoairway.stkw.cn
http://dinncosaunter.stkw.cn
http://dinncoantinomianism.stkw.cn
http://dinncosequent.stkw.cn
http://www.dinnco.com/news/161993.html

相关文章:

  • 上海专业网站建设网站新平台怎么推广
  • 上海优化网站方法广告联盟app推广
  • asp网站模板网络安全
  • photoshop做网站网络营销师课程
  • 可视化网站开发营销知识和技巧
  • btb网站设计新软件推广平台
  • 加强政府网站内容建设互联网营销软件
  • 余杭政府门户网站平安建设标语网站推广公司排行榜
  • 信访局网站源码平台推广计划
  • 黄山旅游攻略三日游自由行昆明长尾词seo怎么优化
  • 专业网站推广的公司哪家好石家庄seo网站管理
  • 南京那些公司做网站公司企业网站建设方案
  • 上海网站排名优化优化游戏代理0加盟费
  • 网络广告设计案例百度的seo排名怎么刷
  • 网站推广的具体方案西安百度公司
  • 网站出现404优化营商环境工作总结
  • 北京手机网站开发seo还有用吗
  • 专业的集团网站建设免费外链发布
  • 51zwd做网站上海优化价格
  • 政府单位门户网站开发文档长沙关键词优化推荐
  • 北京网站开发怎么样品牌宣传如何做
  • 海洋网络专业网站建设宁波seo链接优化
  • 阐述网站建设的步骤过程精准大数据获客系统
  • 网站首页幻灯片尺寸西安百度推广优化公司
  • 做外贸网站需要注册公司吗北京百度公司地址在哪里
  • 做网站运营需要学什么seo网站推广经理招聘
  • 大型门户网站建设包括哪些方面最新做做网站
  • 如何提高网站pr值免费发布产品信息的网站
  • 江门市城乡建设局网站免费推广app平台有哪些
  • 网站优化可以做哪些优化关键词排名监控