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

做网站三剑客稿定设计网站官网入口

做网站三剑客,稿定设计网站官网入口,厦门微信网站建,织梦做的网站打开空白大家看了这么久的string了&#xff0c;可能对string的简单实现有兴趣&#xff0c;所以我实现了一个string类的常用接口&#xff0c;大家可以自行查看&#xff1a; 我是分多文件写的&#xff1a; string.h: #pragma once #define _CRT_SECURE_NO_WARNINGS 1 #include <ios…

大家看了这么久的string了,可能对string的简单实现有兴趣,所以我实现了一个string类的常用接口,大家可以自行查看:

我是分多文件写的:

string.h:

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
#include <string.h>
#include <assert.h>namespace cx
{class string{public://构造函数:string(const char* str = "");//默认值声明时写--string()string(const string& s);	string(size_t n, char c);string(const char* s, size_t n);string(const string& str, size_t pos, size_t len = npos);//运算符重载构造string& operator= (const char* s);	string& operator= (char c);	//string& operator=(const string& s);string& operator=(const string s);//析构函数~string();//迭代器部分:typedef char* iterator;typedef const char* const_iterator;iterator begin(){return _str;}const_iterator begin() const{return _str;}iterator end(){return _str+_size;}const_iterator end() const{return _str + _size;}iterator rbegin(){return end();}const_iterator rbegin() const{return end();}iterator rend(){return begin();}const_iterator rend() const{return begin();}//capacity:inline size_t size()const{return _size;}inline size_t length()const{return _size;}inline size_t capacity()const{return _capacity;}inline bool empty()const{return _size == 0;}void reserve(size_t n = 0);inline size_t max_size() const{return -1;}void clear();		//modify:string& operator+= (const string& str);string & operator+= (const char* s);string& operator+= (char c);void push_back(char c);string& append(const string& str);string& append(const string& str, size_t subpos, size_t sublen);string & append(const char* s);string& append(const char* s, size_t n);string& append(size_t n, char c);string& erase(size_t pos = 0, size_t len = npos);void swap(string& str);void pop_back();string& replace(size_t pos, size_t len, const string& str);string& replace(size_t pos, size_t len, const char* s);//      const char* c_str()const;//access:char& operator[] (size_t pos);const char& operator[] (size_t pos) const;char& at(size_t pos);const char& at(size_t pos) const;//String operations :size_t find(const string& str, size_t pos = 0) const;size_t find(const char* s, size_t pos = 0) const;size_t find(const char* s, size_t pos, size_t n) const;size_t find(char c, size_t pos = 0) const;string substr(size_t pos = 0, size_t len = npos) const;//Non - member function overloads:bool operator<(const string& s);bool operator<=(const string& s);bool operator>(const string& s);bool operator>=(const string& s);bool operator==(const string& s);bool operator!=(const string& s);//友元部分:friend ostream& operator<<(ostream& _cout, const cx::string& s);	friend istream& operator>>(istream& _cin, cx::string& s);private:const static size_t npos=-1;//特殊用法//static size_t npos;char* _str=nullptr;//缺省值size_t _size=0;//缺省值size_t _capacity=0;//缺省值};
};
//static正常用法
//size_t npos = -1;

string.cpp:

#include "string.h"
//构造函数:
cx::string::string(const char* str)//默认值处理空串
{//不适合列表初始化,原因在于strlen时间复杂度O(N)_size = strlen(str);_capacity = _size;char* tmp = new char[_size + 1];//new不用检查strcpy(tmp, str);tmp[_size] = '\0';delete[] _str;_str = tmp;
}
//传统写法:
//cx::string::string(const string& s)
//{
//	char* tmp = new char[s.size() + 1];
//	strcpy(tmp, s._str);
//	tmp[s.size()] = '\0';
//	delete[] _str;
//	_str = tmp;
//	_capacity = s._capacity;
//	_size = s._size;
//}
//现代写法;
cx::string::string(const string& s)
{cx::string tmp(s._str);//拷贝一份临时变量swap(tmp);//将*this和tmp交换//注意点,this初始化情况,我们在private处给缺省值
}
cx::string::string(size_t n, char c)
{char* tmp = new char[n + 1];//strcpy(_str, 0);_size = n;_capacity = n;for (int i = 0; i < n; i++){tmp[i] = c;}tmp[_size] = '\0';delete[] _str;_str = tmp;
}
cx::string::string(const char* s, size_t n)
{char* tmp = new char[n + 1];strncpy(tmp, s, n);tmp[n] = '\0';delete[] _str;_str = tmp;_size = _capacity = n;
}
cx::string::string(const string& str, size_t pos, size_t len)
{if (len == npos || pos + len > str._size){char* tmp = new char[str.size() - pos + 1];_capacity = str.size() - pos;strncpy(tmp, str._str + pos, len);delete[] _str;_str = tmp;}else{char* tmp = new char[len + 1];_capacity = len;strncpy(tmp, str._str + pos, len);delete[] _str;_str = tmp;}_size = len;_str[_size] = '\0';
}
//运算符重载构造
cx::string& cx::string::operator= (const char* s)
{_capacity = _size = strlen(s);char* tmp = new char[_size + 1];strcpy(tmp, s);tmp[_size] = '\0';delete[] _str;_str = tmp;return *this;
}
cx::string& cx::string::operator= (char c)
{char* tmp = new char[2];tmp[0] = c;_size = _capacity = 1;tmp[_size] = '\0';delete[] _str;_str = tmp;return *this;
}
//传统写法:
//cx::string& cx::string::operator=(const string& s)
//{
//	if (this != &s)
//	{
//		char* tmp = new char[s._capacity + 1];
//		strcpy(tmp, s._str);
//		tmp[s._capacity] = '\0';
//		delete[] _str;
//		_str = tmp;
//		_size = s._size;
//		_capacity = s._capacity;
//	}
//	return *this;
//}
//现代写法:
cx::string& cx::string::operator=(string s)
{swap(s);//将this和s交换return *this;
}
//析构函数
cx::string::~string()
{delete[] _str;_str = nullptr;_size = _capacity = 0;
}//capacity:
void cx::string::reserve(size_t n)
{if (n > _capacity){char* tmp = new char[n + 1];strcpy(tmp, _str);		delete[] _str;_str = tmp;_capacity = n;}
}
void cx::string::clear()
{erase();
}
void cx::string::swap(string& str)
{std::swap(_str, str._str);std::swap(_size,str._size);std::swap(_capacity , str._capacity);
}
void cx::string::pop_back()
{erase(_size - 1, 1);
}
cx::string& cx::string::replace(size_t pos, size_t len, const string& str)
{//检查if (pos + len > _size){reserve(pos + len);strncpy(_str + pos, str._str, len);_size = pos + len;_str[_size] = '\0';}else{strncpy(_str + pos, str._str, len);_size = pos + len;}return *this;
}
cx::string& cx::string::replace(size_t pos, size_t len, const char* s)
{//检查if (pos + len > _size){reserve(pos + len);strncpy(_str + pos, s, len);_size = pos + len;_str[_size] = '\0';}else{strncpy(_str + pos, s, len);_size = pos + len;}return *this;
}//modify:
cx::string& cx::string::operator+= (const cx::string& str)
{append(str);return *this;
}
cx::string& cx::string::operator+= (const char* s)
{append(s);return *this;
}
cx::string& cx::string::operator+= (char c)
{push_back(c);return *this;
}
void cx::string::push_back(char c)
{if (_capacity == _size){size_t newcapacity = _capacity == 0 ? 4 : 2 * _capacity;reserve(newcapacity);}_str[_size++] = c;_str[_size] = '\0';
}
cx::string& cx::string::append(const string& str)
{if (str._size + _size > _capacity){reserve(str._size + _size);}strcpy(_str + _size, str._str);_size += str._size;_str[_size] = '\0';return *this;
}
cx::string& cx::string::append(const char* s)
{size_t len = strlen(s);if (len + _size > _capacity){reserve(_size + len);}strcpy(_str + _size, s);_size += len;_str[_size] = '\0';return *this;
}
cx::string& cx::string::append(size_t n, char c)
{//检查if (n + _size > _capacity){reserve(n + _size);}for (size_t i = _size; i < _size + n; i++){_str[i] = c;}_size += n;_str[_size] = '\0';return *this;
}
cx::string& cx::string::append(const char* s, size_t n)
{//检查if (n + _size > _capacity){reserve(n + _size);}strncpy(_str+_size, s, n);_size += n;_str[_size] = '\0';return *this;
}
cx::string& cx::string::append(const string& str, size_t subpos, size_t sublen)
{//检查if (sublen + _size > _capacity){reserve(sublen + _size);}strncpy(_str+_size, str._str+subpos, sublen);_size += sublen;_str[_size] = '\0';return *this;
}
cx::string& cx::string::erase(size_t pos, size_t len)
{	assert(pos <= _size);if (len == npos || len + pos > _size){_str[pos] = '\0';_size = pos;}else{strcpy(_str + pos, _str + pos + len);_size -= len;_str[_size] = '\0';}return *this;
}//access:
char& cx::string::operator[] (size_t pos)
{assert(pos <= _size);return *(_str + pos);//等价于_str[pos]
}
const char& cx::string::operator[] (size_t pos) const
{assert(pos <= _size);return *(_str + pos);//等价于_str[pos]
}
char& cx::string::at(size_t pos)
{assert(pos < _size);return _str[pos];
}
const char& cx::string::at(size_t pos) const
{assert(pos < _size);return _str[pos];
}//String operations :
size_t cx::string::find(const string& str, size_t pos ) const
{char* tmp = new char[str._size + 1];strcpy(tmp, str._str);tmp[str._size ] = '\0';const char* ptr = strstr(_str + pos, tmp);if (ptr == nullptr){delete[] tmp;tmp = nullptr;return npos;}else{delete[] tmp;tmp = nullptr;return ptr - _str;}
}
size_t cx::string::find(const char* s, size_t pos ) const
{const char* ptr = strstr(_str + pos, s);if (ptr == nullptr){return npos;}else{return ptr - _str;}
}
size_t cx::string::find(const char* s, size_t pos, size_t n) const
{char* tmp = new char[n];strcpy(tmp, s);tmp[n] = '\0';const char* ptr = strstr(_str + pos, tmp);if (ptr == nullptr){delete[] tmp;tmp = nullptr;return npos;}else{delete[] tmp;tmp = nullptr;return ptr - _str;}
}
size_t cx::string::find(char c, size_t pos ) const
{for (size_t i = pos; i < _size; i++){if (_str[i] == c)return i;}return npos;
}
cx::string cx::string::substr(size_t pos , size_t len) const
{//检查:assert(pos < _size);size_t end = pos + len;if (len == npos || pos + len >= _size){end = _size;}string str;str.reserve(end - pos);for (size_t i = pos; i < end; i++){str += _str[i];}return str;
}//Non - member function overloads:
bool cx::string::operator<(const string& s)
{size_t n = _size > s._size ? s._size : _size;for (size_t i = 0; i < n; i++){if (_str[i] >= s._str[i])return false;}if (n == s._size)return false;elsereturn true;
}
bool cx::string::operator<=(const string& s)
{return !(*this > s);
}
bool cx::string::operator>(const string& s)
{return !((*this == s) || (*this < s));
}
bool cx::string::operator>=(const string& s)
{return !(*this < s);
}
bool cx::string::operator==(const string& s)
{if (_size != s._size)return false;for (size_t i = 0; i < _size; i++){if (_str[i] != s._str[i])return false;}return true;
}
bool cx::string::operator!=(const string& s)
{return !(*this == s);
}//友元:
// 写法一:
//namespace cx
//{
//	ostream& operator<<(ostream& _cout, const cx::string& s)
//	{
//		for (auto e : s)
//		{
//			_cout << e;
//		}
//		return _cout;
//	}
//}
//写法二:
ostream& cx::operator<<(ostream& _cout, const cx::string& s)
{for (auto e : s){_cout << e;}return _cout;
}
istream& cx::operator>>(istream& _cin, cx::string& s)
{s.clear();//清除内容char ch = _cin.get();//建立一个缓冲数组char buff[64];size_t i = 0;while((ch != ' ') && (ch != '\n')){//读入buff中buff[i++] = ch;if (65 == i){buff[i] = '\0';//注意:置零s += buff;//利用operator+=数组i = 0;}ch = _cin.get();}if (i > 0){buff[i] = '\0';s += buff;}return _cin;
}

test.cpp:

#include "string.h"
void test_string()
{cx::string s0("Initial string");std::cout << s0 << std::endl;cx::string s1;std::cout << s1 << std::endl;cx::string s2(s0);std::cout << s2 << std::endl;cx::string s3(s0, 8, 3);std::cout << s3 << std::endl;cx::string s4("A character sequence");std::cout << s4 << std::endl;cx::string s5("Another character sequence", 12);std::cout << s5 << std::endl;cx::string s6(10, 'x');std::cout << s6 << std::endl;cx::string s7(10, 42);std::cout << s7 << std::endl;
}
void test_operator()
{cx::string str("Test string");for (int i = 0; i < str.size(); ++i){std::cout << str[i] << std::endl;}for (int i = 0; i < str.length(); ++i){std::cout << str[i] << std::endl;}
}
void test_capacity()
{cx::string str("Test string");std::cout << str.max_size() << std::endl;std::cout << str.capacity() << std::endl;
}
void test_reserve()
{cx::string str("Test string");std::cout << str.empty() << std::endl;std::cout << str.capacity() << std::endl;str.reserve(50);std::cout << str.capacity() << std::endl;
}
void _string2()
{cx::string s0("Initial string");cout << s0 << std::endl;cx::string s1;cout << s1 << std::endl;cx::string s2(s0);cout << s2 << std::endl;cx::string s3(s0, 8, 3);cout << s3 << std::endl;cx::string s4("A character sequence");cout << s4 << std::endl;cx::string s5("Another character sequence", 12);cout << s5 << std::endl;cx::string s6(10, 'x');cout << s6 << std::endl;cx::string s7(10, 42);cout << s7 << std::endl;
}
void test_s()
{cx::string s0("Initial string");cx::string s3(s0, 8, 3);cout << s3 << std::endl;
}
void test_opertaor()
{cx::string s1 = "hello world";cx::string s2 = "c";cx::string s3 = s1;cout << s1 << std::endl;cout << s2 << std::endl;cout << s3 << std::endl;
}
void test_o()
{cx::string str1, str2, str3;str1 = "Test string: ";   // c-stringstr2 = 'x';               // single character//str3 = str1 + str2;       // stringcout << str1 << std::endl;cout << str2 << std::endl;
}
void test_push()
{cx::string s1 = "hello worl";s1.push_back('d');cout << s1 << std::endl;
}
void test_append()
{cx::string str;cx::string str2 = "Writing ";cx::string str3 = "print 10 and then 5 more";str.append(str2);// "Writing "cout << str << std::endl;str.append(str3, 6, 3);// "10 "cout << str << std::endl;str.append("dots are cool", 5);// "dots "cout << str << std::endl;str.append("here: ");// "here: "cout << str << std::endl;str.append(10u, '.'); // ".........."cout << str << std::endl;
}
void test_oper()
{cx::string name("John");cx::string family("Smith");name += " K. ";cout << name << std::endl;name += family;cout << name << std::endl;name += '\n';cout << name << std::endl;
}
void erase_test()
{cx::string s1 = "hello world";cout << s1.size() << std::endl;cout << s1.capacity() << std::endl;cout << s1 << endl;s1.clear();cout << s1.size() << std::endl;cout << s1.capacity() << std::endl;cout << s1 << endl;
}
void test_cin()
{cx::string s1, s2;cin >> s1;cout << s1 << endl;cin >> s2;s1 += s2;cout << s1 << endl;
}
void test_stringc()
{cx::string s1 = "helloworld";cx::string s2(s1);cout << s2 << endl;
}
void test_op()
{cx::string s1 = "hello world",s2;s2 = s1;cout << s2 << endl;
}
int main()
{test_stringc();return 0;
}

最后,感谢大家的支持!!!

http://www.yayakq.cn/news/346945/

相关文章:

  • 医疗网站建设网站东莞网站提升排名
  • 全网营销型网站 新闻休闲会所网站建设
  • led灯网站策划书尚义网站建设
  • jsp网站建设技术案例罗湖附近公司做网站建设哪家技术好
  • 普通网站与营销型网站有什么区别自适应网站优点缺点
  • 做网站是什么意思网络搭建是什么工作
  • 哪里有做商城的网站装修公司做网站有用吗
  • 没有做等保的网站不能上线对吗山西移动网站设计
  • 当当网网站建设策划书上海品划做网站
  • 门户网站建设情况总结wordpress搭建内网导航
  • 怎么让网站绑定域名访问不了中山网站建设文化方案
  • 重庆做网站开发的集中河南广告制作公司网站
  • 兰州新区建设银行网站小程序开发教程和源码
  • 有关网站设计与制作的论文建筑设计加盟分公司
  • 问卷调查网站赚钱程序员知识网站需要多少钱
  • 自己的网站怎么做网盘深圳公众号制作
  • 电商设计网站培训网站建设需要的条件
  • 河北婚庆网站建设定制如何网站后台清理缓存
  • 以前的网站忘了怎么办啊软件技术论文题目
  • 网站直播怎么做做兼职的网站贴吧
  • 设计网站免费素材外贸网站建设需要什么
  • 这样做自己的网站亿速云官网
  • 网站开发主要内容和要求公司微网站怎么做的好
  • 互联网招聘网站排行wordpress设置禁止蜘蛛抓取
  • 自建免费网站哪个好自己做网站用软件
  • 网站备案必须在公司注册地搜索引擎竞价推广的优势
  • 手机界面设计网站汕头保洁公司
  • 淘宝标题优化网站湖北省建设银行网站
  • 辖网站建设 网站设计矿泉水网站模板
  • 南海区建设局网站首页如何在百度上推广业务