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

问题:假如有一个类似于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. Docker 搭建pxc集群 + haproxy + keepalived 高可用(一)

    一.首先需要安装好docker,安装方法可以参考之前一篇博文Centos7安装docker [root@localhost ~]# systemctl start docker [root@local ...

  2. 【JVM虚拟机】(2)---GC 算法与种类

    GC 算法与种类 对于垃圾收集(GC), 我们需要考虑三件事情:哪些内存需要回收?如何判断是垃圾对象?垃圾回收算法有哪些? 一.GC的工作区域 1.不是GC的工作区域 (1)程序计数器.虚拟机栈和本地 ...

  3. 【快速入门ORM框架之Dapper】大牛勿进系列

    前言:dapper是什么?Dapper是.NET下一个micro的ORM,它和Entity Framework或Nhibnate不同,属于轻量级的,并且是半自动的.也就是说实体类都要自己写.它没有复杂 ...

  4. 微信小程序代码构成

    一.小程序代码 app.json 是当前小程序的全局配置,包括了小程序的所有页面路径.界面表现.网络超时时间.底部tab等. { "pages":[ "pages/ind ...

  5. MongoDB exception:connection failed

    根据http://www.runoob.com/mongodb/mongodb-window-install.html的教程配置了MongoDB,Mongod.exe配置为 --port 指令表明mo ...

  6. Leetcode 137. 只出现一次的数字 II - 题解

    Leetcode 137. 只出现一次的数字 II - 题解 137. Single Number II 在线提交: https://leetcode.com/problems/single-numb ...

  7. C#版(击败100.00%的提交) - Leetcode 151. 翻转字符串里的单词 - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  8. .NET应用程序管理服务AMS设计

    AMS全称是Application Management Server即应用程序管理服:由于经常要写些一些应用服务,每次部署和维护都比较麻烦,首先要针对服务编写一个windows服务程序方便系统启动里 ...

  9. Leetcode:338. Bit位计数

    Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the ...

  10. 翻译 Asp.Net Core 2.2.0-preview1已经发布

    Asp.Net Core 2.2.0-preview1已经发布 原文地址 ASP.NET Core 2.2.0-preview1 now available 今天我们很高兴地宣布,现在可以试用ASP. ...