重载运算符 ==和!=的重载

问题:假如有一个类似于vector的类,这个类只能存放string,当有2个这个类的对象时,如何比较这2个对象。

自己重载==和!=

代码(重载==,!=)

#include <iostream>
#include <memory>
#include <string> class string_vector{
friend bool operator==(const string_vector&, const string_vector&);
friend bool operator!=(const string_vector&, const string_vector&);
public:
string_vector():
elements(nullptr), first_free(nullptr), cap(nullptr){}
string_vector(const string_vector&);
string_vector& operator=(const string_vector&);
string_vector(std::initializer_list<std::string>);
~string_vector();
void push_back(const std::string&); size_t size() const {
return first_free - elements;
}; size_t capacity() const {
return cap - elements;
} void resize(size_t, std::string&);
void resize(size_t); void reserve(size_t); std::string* begin() const{return elements;}
std::string* end() const{return first_free;}
private:
static std::allocator<std::string> alloc;
//static const int a = 10;
void chk_n_alloc(){
if(size() == capacity())
reallocate();
}
std::pair<std::string*, std::string*> alloc_n_copy
(const std::string* b, const std::string* e);
void free();
void reallocate();
void reallocate(size_t);
std::string* elements;//指向第一个元素的指针
std::string* first_free;//指向最后一个元素的下一个位置的指针
std::string* cap;//指向vector空间最后一个位置的下一个位置的指针
}; //必须在类的外面再定义一次,否则后面使用alloc的地方,编译不过
std::allocator<std::string> string_vector::alloc; std::pair<std::string*, std::string*> string_vector::alloc_n_copy
(const std::string* b, const std::string* e){
auto data = alloc.allocate(e - b);
return {data, std::uninitialized_copy(b, e, data)};
} void string_vector::push_back(const std::string& s){
chk_n_alloc();
alloc.construct(first_free++, s);
} void string_vector::free(){
if(elements){
for(auto p = first_free; p != elements;)
alloc.destroy(--p);
alloc.deallocate(elements, cap - elements);
}
} string_vector::string_vector(const string_vector& s){
auto newdata = alloc_n_copy(s.begin(), s.end());
elements = newdata.first;
first_free = cap = newdata.second;
}
string_vector::string_vector(std::initializer_list<std::string> sl){
auto newdata = alloc_n_copy(sl.begin(), sl.end());
elements = newdata.first;
first_free = cap = newdata.second;
}
string_vector::~string_vector(){
free();
} string_vector& string_vector::operator=(const string_vector& rhs){
auto newdata = alloc_n_copy(rhs.begin(), rhs.end());
free();
elements = newdata.first;
first_free = cap = newdata.second;
return *this;
} void string_vector::reallocate(){
auto newcap = size() ? size() * 2 : 1;
auto newdata = alloc.allocate(newcap);
auto dest = newdata;
auto elem = elements;
for(size_t i = 0; i != size(); ++i){
alloc.construct(dest++, std::move(*elem++));
}
free();
elements = newdata;
first_free = dest;
cap = elements + newcap;
} void string_vector::reallocate(size_t sz){
auto newcap = sz * 2;
auto newdata = alloc.allocate(newcap);
auto dest = newdata;
auto elem = elements;
for(size_t i = 0; i != size(); ++i){
alloc.construct(dest++, std::move(*elem++));
}
free();
elements = newdata;
first_free = dest;
cap = elements + newcap;
}
void string_vector::reserve(size_t sz){
if(sz > capacity()){
reallocate(sz);
}
} void string_vector::resize(size_t sz){
size_t cap = capacity();
if(sz > cap){
reallocate(sz);
for(size_t i = size();i != sz; ++i){
//调用string的默认构造方法
alloc.construct(first_free++);
}
}
else if(sz < size()){
for(size_t i = sz;i != size(); ++i){
//调用string的西沟函数
alloc.destroy(--first_free);
}
}
} void string_vector::resize(size_t sz, std::string& s){
size_t cap = capacity();
if(sz > cap){
reallocate(sz);
for(size_t i = size();i != sz; ++i){
//调用string的非默认构造方法
alloc.construct(first_free++, s);
}
}
else if(sz < size()){
for(size_t i = sz;i != size(); ++i){
//调用string的西沟函数
alloc.destroy(--first_free);
}
}
} bool operator==(const string_vector& lhs, const string_vector& rhs){
if(lhs.size() == rhs.size()){
auto *p1 = lhs.elements;
auto *p2 = rhs.elements;
while(p1 != lhs.first_free){
if(*p1++ != *p2++){
return false;
}
}
return true;
}else{
return false;
}
}
bool operator!=(const string_vector& lhs, const string_vector& rhs){
return !operator==(lhs, rhs);
}
int main(){
string_vector sv1{"112"};
string_vector sv2{"11"};
if(sv1 != sv2){
std::cout << "!=" << std::endl;
}
else{
std::cout << "==" << std::endl;
}
}

github

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

c/c++ 重载运算符 ==和!=的重载的更多相关文章

  1. 【STL】重载运算符

    重载运算符 为什么要重载运算符: C++中预定义的运算符的操作对象只能是基本数据类型.但实际上,对于许多用户自定义类型(例如结构体),也需要类似的运算操作.这时就必须在C++中重新定义这些运算符,赋予 ...

  2. 【noi 2.6_9290】&【poj 2680】Computer Transformation(DP+高精度+重载运算符)

    题意:给一个初始值1,每步操作将1替换为01,将0替换为10.问N步操作后有多少对连续的0. 解法:f[i]表示第i步后的答案.可以直接打表发现规律--奇数步后,f[i]=f[i-1]*2-1;偶数步 ...

  3. c++的重载运算符

    c++中允许重载运算符: 这是我辛苦的结果 #include"iostream"using namespace std;class aaa{ int x;public: aaa() ...

  4. C# 重载运算符

    如果你想让自己定义的类型可以用运算符进行运算,那么可以通过重载运算符来实现: 示例: class Salary { public int RMB { get; set; } public static ...

  5. c++中有些重载运算符为什么要返回引用

    事实上,我们的重载运算符返回void.返回对象本身.返回对象引用都是可以的,并不是说一定要返回一个引用,只不过在不同的情况下需要不同的返回值. 那么什么情况下要返回对象的引用呢? 原因有两个: 允许进 ...

  6. C++ Primer : : 第十四章 : 重载运算符与类型转换之类型转换运算符和重载匹配

    类型转换运算符 class SmallInt { public: SmallInt(int i = 0) : val(i) { if (i < 0 || i > 255) throw st ...

  7. C++ Primer : 第十四章 : 重载运算与类型转换之重载运算符

    重载前须知 重载运算符是特殊的函数,它们的名字由operator和其后要重载的运算符号共同组成. 因为重载运算符时函数, 因此它包含返回值.参数列表和函数体. 对于重载运算符是成员函数时, 它的第一个 ...

  8. Swift开发第六篇——操作运算符也可以重载& func 的参数修饰

    本篇分为两部分: 1.Swift 中重载操作运算符的使用 2.Swfit 中 func 的参数修饰 1.Swift 中重载操作运算符的使用 与别的语言不同,Swift 支持运算符的重载,运算符指的是“ ...

  9. C#基础知识系列一(goto、i++、三元运算符、ref和out、String和string、重载运算符)

    前言 这两天在网上看到的总结很多,尤其是博客园中的,很多很多,也给了我很多的启发,当然自己也总结过,而且有很多人也给与我一些意见和看法.不管怎样,自己还是先把所谓的基础知识加强巩固下吧. 2014年的 ...

随机推荐

  1. Go JSON 转化规则

    Go语言内置encoding/json包支持JSON序列化和反序列化,有如下转换规则 基本的数据结构映射关系 bool, for JSON booleans float64, for JSON num ...

  2. Python内置函数(2)——all

    英文文档: all(iterable) Return True if all elements of the iterable are true (or if the iterable is empt ...

  3. Python内置函数(40)——map

    英文文档: map(function, iterable, ...) Return an iterator that applies function to every item of iterabl ...

  4. 关于pycharm安装出现的interpreter field is empty,无法创建项目存储位置

    关于pycharm安装出现的interpreter field is empty(解释器为空) 关于pycharm安装出现的interpreter field is empty,无法创建项目存储的位置 ...

  5. linux-php5.6-安装sftp扩展

    一. 更新gc库,添加libssh2库 yum -y install libstdc libgomp cpp gcc libgfortran libssh2 libssh2-devel gcc-gfo ...

  6. IntelliJ IDEA上操作GitHub

    IntelliJ IDEA集成了对GitHub的支持,使上传代码到GitHub和从GitHub下载代码更加方便快捷. 一. 分享代码到GitHub 1.首先需要在IntelliJ配置Git,如果没有正 ...

  7. ARP欺骗攻击

    一.ARP攻击概述 ARP攻击主要是存在于局域网中,通过伪造IP地址和MAC地址实现ARP欺骗,能够在网络中产生大量的ARP通信量使网络阻塞,攻击者只要持续不断的发出伪造的ARP响应包就能更改目标主机 ...

  8. Linux svn checkout时候总报设备上没有空间

    详细报错信息:svn: 不能打开文件“weibosearch2.0.0/.svn/lock”: 设备上没有空间 但是df -h查看磁盘[root@picdata-1-2 data]# df -h文件系 ...

  9. 4.镜像管理【Docker每天5分钟】

    Docker给PaaS世界带来的“降维打击”,其实是提供了一种非常便利的打包机制.该机制打包了应用运行所需要的整个操作系统,从而保证了本地环境和云端环境的高度一致,避免了用户通过“试错”来匹配不同运行 ...

  10. [十二]基础数据类型之String

    在正式介绍String之前,我们先介绍下CharSequence char + sequence 就是字符的序列的意思 Java中万事万物都是对象类型 而对于字符的序列,也就是多个char, 这么一种 ...