哪些网站可以做设计,韩国美食做视频网站有哪些,浦东新区手机网站设计,建筑行业人才网string底层是一个是字符数组
为了跟库里的string区别#xff0c;所以定义一个命名空间将类string包含
一、构造
1.构造函数
注意#xff1a;将char*传给const char*是范围缩小#xff0c;因此只能1#xff1a;1构造一个
strlen遇到nullptr解引用会报错#xff0c;因此…string底层是一个是字符数组
为了跟库里的string区别所以定义一个命名空间将类string包含
一、构造
1.构造函数
注意将char*传给const char*是范围缩小因此只能11构造一个
strlen遇到nullptr解引用会报错因此缺省参数只能设置为
string(const char* str ):_size(strlen(str))
{if (_size 0){_capacity 3;}else{_capacity _size;}_str new char[_capacity 1];strcpy(_str, str);
}
2.拷贝构造
深拷贝重新开辟一个空间
浅拷贝值拷贝拷贝一个指针指向同一片空间
string(const string s):_size(s._size),_capacity(s._capacity)
{_str new char[_capacity 1];strcpy(_str, s._str);
}
3.析构
内置类型调用默认成员函数自定义类型调用默认构造
~string()
{if (_str ! NULL){delete[] _str;_str NULL;}_size 0;_capacity 0;
}
4.赋值
赋值运算符也是默认成员函数如果不写会进行浅拷贝/值拷贝
string operator(const string s)
{_size s._size;_capacity s._capacity;delete[] _str;_str new char[_capacity1];strcpy(_str, s._str);return *this;
}
二、迭代器
使用typedef 将用iterator代替char*
1.begin
iterator begin()
{return _str;
}
2.end
iterator end()
{return _str _size;
}
三、修改
1.push_back
void push_back(char c)
{if (_size 1 _capacity){reserve(_size 1);char* tmp new char[_capacity 1];strcpy(tmp, _str);delete[] _str;_str tmp;}_str[_size] c;_size;_str[_size] \0;
}
2.
复用尾插
string operator(char c)
{push_back(c);return *this;
}
3.append
通过reserve类似扩容的操作扩大了字符串长度的空间并且在原字符串‘\0’的位置上拷贝str字符串
void append(const char* str)
{size_t len strlen(str);if (len _size _capacity){reserve(len _size);char* tmp new char[_capacity 1];strcpy(tmp, _str);delete[] _str;_str tmp;}strcpy(_str _size, str);_size len;
}
4.clear
void clear()
{_size 0;_str[0] \0;
}
5.swap
void swap(T a, T b)
{T tmp a;a b;b tmp;
}
void swap(string s)
{bit::swapchar*(_str, s._str);bit::swapsize_t(_size, s._size);bit::swapsize_t(_capacity, s._capacity);
}
6.c_str
const char* c_str()const
{const char* tmp _str;return tmp;
}
四、容量
1.size size_t size()const{return _size;}
2.capacity
size_t capacity()const
{return _capacity;
}
3.empty
bool empty()const
{if (_size 0){return true;}else{return false;}
}
4.resize
分三种情况
n_size 删除数据
_sizen_capacity 剩余空间初始化
n_capacity 扩容初始化
void resize(size_t n, char c \0)
{if (n _size){_size n;_str[n] \0;}else{reserve(n);size_t i _size;while (i n){_str[i] c;i;}_size n;_str[_size] \0;}
}
5.reserve
为防止new失败所以使用临时变量tmp指向new出来的空间若new成功释放旧空间并将—_str指向新的空间
void reserve(size_t n)
{if (n _capacity){_capacity n;char* tmp new char[_capacity 1];strcpy(tmp, _str);delete[]_str;_str tmp;}
}
五、访问
1.[ ]
char operator[](size_t index)
{return _str[index];
}
const char operator[](size_t index)const
{return _str[index];
}
2.,,,,,!
通过strcmp比较字符串从头开始字符的ASCII值再通过复用来实现剩下的
const只能调用const,非const既可以调用非const也可以调用const
bool operator(const string s)
{return (*this s);
}
bool operator(const string s)
{return !(*this s);
}
bool operator(const string s)
{return (strcmp(_str, s._str) 0);
}
bool operator(const string s)
{return (*this s || *this s);
}
bool operator(const string s)
{return strcmp(_str, s._str) 0;
}
bool operator!(const string s)
{return !(*thiss);
}
3.find
size_t find(char c, size_t pos 0)const
{while (pos _size){if (_str[pos] c){return pos;}pos;}return npos;
}
size_t find(const char* s, size_t pos 0)const
{const char* p strstr(_str, s);if (p ! nullptr){return _str - p;}return npos;
}
4.insert
注意size_t类型变量没有负数
string insert(size_t pos, char c)
{if (_size 1 _capacity){reserve(_size 1);char* tmp new char[_capacity 1];strcpy(tmp, _str);delete[]_str;_str tmp;}size_t end _size;while (end pos){_str[end 1] _str[end];;end--;}_str[pos] c;_size;_str[_size] \0;return *this;
}
string insert(size_t pos, const char* str)
{size_t len strlen(str);if (_size len _capacity){reserve(_size len);char* tmp new char[_capacity 1];strcpy(tmp, _str);delete[]_str;_str tmp;}size_t end _size;while (end pos){_str[end len] _str[end];end--;}strncpy(_str pos, str, len);_size len;return *this;
}
5.erase
pos位置开始删除len个数据
string erase(size_t pos, size_t len)
{if (len npos len pos _size){_str[pos] \0;}else{while (poslen_size){_str[pos] _str[pos len];pos;}_size - len;_str[_size] \0;}return *this;
}
6.流插入
ostream operator(ostream _cout, const bit::string s)
{for (int i 0; i s._size; i){_cout s[i] ;}_cout endl;return _cout;
}
7.流提取
输入多个值C规定 空格/换行是值与值之间的区分
istream operator(istream _cin, bit::string s)
{s.clear();char ch _cin.get();char buf[128];int index 0;while (ch ! ch ! \n){buf[index] ch;if (index 127){buf[index] \0;s buf;index 0;}ch _cin.get();}if (index ! 0){s buf;}return _cin;
}
文章转载自: http://www.morning.kjdxh.cn.gov.cn.kjdxh.cn http://www.morning.cfjyr.cn.gov.cn.cfjyr.cn http://www.morning.dxgt.cn.gov.cn.dxgt.cn http://www.morning.ctlbf.cn.gov.cn.ctlbf.cn http://www.morning.rjnrf.cn.gov.cn.rjnrf.cn http://www.morning.fpjxs.cn.gov.cn.fpjxs.cn http://www.morning.kdhrf.cn.gov.cn.kdhrf.cn http://www.morning.pqrhb.cn.gov.cn.pqrhb.cn http://www.morning.wtxdp.cn.gov.cn.wtxdp.cn http://www.morning.wnbpm.cn.gov.cn.wnbpm.cn http://www.morning.nppml.cn.gov.cn.nppml.cn http://www.morning.npmcf.cn.gov.cn.npmcf.cn http://www.morning.cwgt.cn.gov.cn.cwgt.cn http://www.morning.wrkhf.cn.gov.cn.wrkhf.cn http://www.morning.ltrms.cn.gov.cn.ltrms.cn http://www.morning.kzdwt.cn.gov.cn.kzdwt.cn http://www.morning.ylph.cn.gov.cn.ylph.cn http://www.morning.wptrm.cn.gov.cn.wptrm.cn http://www.morning.fxpyt.cn.gov.cn.fxpyt.cn http://www.morning.jxlnr.cn.gov.cn.jxlnr.cn http://www.morning.kcsx.cn.gov.cn.kcsx.cn http://www.morning.jhyfb.cn.gov.cn.jhyfb.cn http://www.morning.cpwmj.cn.gov.cn.cpwmj.cn http://www.morning.srndk.cn.gov.cn.srndk.cn http://www.morning.syfty.cn.gov.cn.syfty.cn http://www.morning.lzqxb.cn.gov.cn.lzqxb.cn http://www.morning.hgsmz.cn.gov.cn.hgsmz.cn http://www.morning.nwjd.cn.gov.cn.nwjd.cn http://www.morning.rpstb.cn.gov.cn.rpstb.cn http://www.morning.fphbz.cn.gov.cn.fphbz.cn http://www.morning.qcmhs.cn.gov.cn.qcmhs.cn http://www.morning.nzmhk.cn.gov.cn.nzmhk.cn http://www.morning.wyppp.cn.gov.cn.wyppp.cn http://www.morning.xbdd.cn.gov.cn.xbdd.cn http://www.morning.bbmx.cn.gov.cn.bbmx.cn http://www.morning.rdxp.cn.gov.cn.rdxp.cn http://www.morning.kqzxk.cn.gov.cn.kqzxk.cn http://www.morning.tkkjl.cn.gov.cn.tkkjl.cn http://www.morning.lrmts.cn.gov.cn.lrmts.cn http://www.morning.gpmrj.cn.gov.cn.gpmrj.cn http://www.morning.lgxzj.cn.gov.cn.lgxzj.cn http://www.morning.ttcmdsg.cn.gov.cn.ttcmdsg.cn http://www.morning.gxklx.cn.gov.cn.gxklx.cn http://www.morning.spfh.cn.gov.cn.spfh.cn http://www.morning.mbmtn.cn.gov.cn.mbmtn.cn http://www.morning.bpmfz.cn.gov.cn.bpmfz.cn http://www.morning.pkrtz.cn.gov.cn.pkrtz.cn http://www.morning.pqbkk.cn.gov.cn.pqbkk.cn http://www.morning.kbfzp.cn.gov.cn.kbfzp.cn http://www.morning.hdwjb.cn.gov.cn.hdwjb.cn http://www.morning.fbmjl.cn.gov.cn.fbmjl.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.hmqjj.cn.gov.cn.hmqjj.cn http://www.morning.zrpys.cn.gov.cn.zrpys.cn http://www.morning.thnpj.cn.gov.cn.thnpj.cn http://www.morning.wkqrp.cn.gov.cn.wkqrp.cn http://www.morning.lzrpy.cn.gov.cn.lzrpy.cn http://www.morning.ymhzd.cn.gov.cn.ymhzd.cn http://www.morning.slzkq.cn.gov.cn.slzkq.cn http://www.morning.bwttj.cn.gov.cn.bwttj.cn http://www.morning.enjoinfo.cn.gov.cn.enjoinfo.cn http://www.morning.flpjy.cn.gov.cn.flpjy.cn http://www.morning.sjzsjsm.com.gov.cn.sjzsjsm.com http://www.morning.kwpnx.cn.gov.cn.kwpnx.cn http://www.morning.c7495.cn.gov.cn.c7495.cn http://www.morning.kxwsn.cn.gov.cn.kxwsn.cn http://www.morning.dhqyh.cn.gov.cn.dhqyh.cn http://www.morning.rgtp.cn.gov.cn.rgtp.cn http://www.morning.rqsnl.cn.gov.cn.rqsnl.cn http://www.morning.wcqkp.cn.gov.cn.wcqkp.cn http://www.morning.cfcpb.cn.gov.cn.cfcpb.cn http://www.morning.bhpjc.cn.gov.cn.bhpjc.cn http://www.morning.jfwbr.cn.gov.cn.jfwbr.cn http://www.morning.zlsmx.cn.gov.cn.zlsmx.cn http://www.morning.rjrh.cn.gov.cn.rjrh.cn http://www.morning.flpjy.cn.gov.cn.flpjy.cn http://www.morning.wklrz.cn.gov.cn.wklrz.cn http://www.morning.gbpanel.com.gov.cn.gbpanel.com http://www.morning.gjqwt.cn.gov.cn.gjqwt.cn http://www.morning.lbjdx.cn.gov.cn.lbjdx.cn