1.1 重载运算符特点
-
重载运算符本质上是一次函数调用
-
除了operator() 运算符调用外,其他重载运算符不能含有默认参数。
-
当重载的运算符是成员函数时,this绑定到左侧运算对象。成员运算符函数(显式)的参数数量比运算对象少一个。
运算符重载列表
该将运算符定义为成员函数还是普通成员?参考是什么,如下。
Exercise 14.1: 重载操作符与内置操作符有哪些不同?重载操作符在哪些方面与内置操作符相同?
不同点
- 我们可以直接调用重载的operator函数。
- 重载操作符函数必须是类的成员或具有至少一个类类型形参。
- 少数操作符保证操作数求值的顺序。这些操作符的重载版本不能保持求值和/或短路求值的顺序,重载它们通常是一个坏主意。
- 特别是,不保留逻辑与、逻辑或和逗号操作符的操作数求值保证。而且,重载版本的&&或||操作符不保留内置操作符的短路求值属性。两个操作数总是被求值。
相同点
- 重载操作符与相应的内置操作符具有相同的优先级和结合性。
Exercise 14.2: 为Sales_data重载的输入、输出、加法和复合赋值运算符。
#include "ex14_02.h"
Sales_data::Sales_data(std::istream &is) : Sales_data()
{
is >> *this;
}
Sales_data& Sales_data::operator+=(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
std::istream& operator>>(std::istream &is, Sales_data &item)
{
double price = 0.0;
is >> item.bookNo >> item.units_sold >> price;
if (is)
item.revenue = price * item.units_sold;
else
item = Sales_data();
return is;
}
std::ostream& operator<<(std::ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price();
return os;
}
Sales_data operator+(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum += rhs;
return sum;
}
// @Add overloaded input, output, addition, and compound-assignment operators
#ifndef CP5_CH14_EX14_02_H
#define CP5_CH14_EX14_02_H
#include <string>
#include <iostream>
class Sales_data {
friend std::istream& operator>>(std::istream&, Sales_data&); // input
friend std::ostream& operator<<(std::ostream&, const Sales_data&); // output
friend Sales_data operator+(const Sales_data&, const Sales_data&); // addition
public:
Sales_data(const std::string &s, unsigned n, double p):bookNo(s), units_sold(n), revenue(n*p){ }
Sales_data() : Sales_data("", 0, 0.0f){ }
Sales_data(const std::string &s) : Sales_data(s, 0, 0.0f){ }
Sales_data(std::istream &is);
Sales_data& operator+=(const Sales_data&); // compound-assignment
std::string isbn() const { return bookNo; }
private:
inline double avg_price() const;
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
std::istream& operator>>(std::istream&, Sales_data&);
std::ostream& operator<<(std::ostream&, const Sales_data&);
Sales_data operator+(const Sales_data&, const Sales_data&);
inline double Sales_data::avg_price() const
{
return units_sold ? revenue/units_sold : 0;
}
#endif // CP5_CH14_EX14_02_H
Exercise 14.3: string和vector都定义了重载的==,可用于比较这些类型的对象。假设svec1和svec2是保存字符串的向量,确定下面每个表达式中应用了哪个版本的==:
为什么以下语句没有调用重载的操作符== (const String &, const String &)?
因为在c++中,字符串字面值的类型是const char
[]( zero-terminated string constant 也称为零结束字符串常量)
有一个内置的操作符==通过比较两个char*的地址来比较它们。 由于数组隐式地可转换为指向其第一个元素的指针(这是C继承),因此需要使用这个操作符,比较的是内存中这些字面值的地址。
假设你的String类有一个从const char* (String::String(const char*))
的隐式转换构造函数,你可以将其中一个转换为String。另一个字符串将被隐式转换:
String("cobble") == "stone"
(除非为了提高效率,提供了operator==和const char*的重载。如果提供了,他们就会介入。)
Reference
Exercise 14.5: 在7.5.1(第291页)的练习7.40中,您编写了以下类之一的框架。决定类应该提供什么重载操作符?
Such as Book
#include <iostream>
#include <string>
class Book {
friend std::istream& operator>>(std::istream&, Book&);
friend std::ostream& operator<<(std::ostream&, const Book&);
friend bool operator==(const Book&, const Book&);
friend bool operator!=(const Book&, const Book&);
public:
Book() = default;
Book(unsigned no, std::string name, std::string author, std::string pubdate):no_(no), name_(name), author_(author), pubdate_(pubdate) { }
Book(std::istream &in) { in >> *this; }
private:
unsigned no_;
std::string name_;
std::string author_;
std::string pubdate_;
};
std::istream& operator>>(std::istream&, Book&);
std::ostream& operator<<(std::ostream&, const Book&);
bool operator==(const Book&, const Book&);
bool operator!=(const Book&, const Book&);
#include "ex14_05.h"
std::istream& operator>>(std::istream &in, Book &book)
{
in >> book.no_ >> book.name_ >> book.author_ >> book.pubdate_;
return in;
}
std::ostream& operator<<(std::ostream &out, const Book &book)
{
out << book.no_ << " " << book.name_ << " " << book.author_ << " " << book.pubdate_;
return out;
}
bool operator==(const Book &lhs, const Book &rhs)
{
return lhs.no_ == rhs.no_;
}
bool operator!=(const Book &lhs, const Book &rhs)
{
return !(lhs == rhs);
}
Test
#include "ex14_05.h"
int main()
{
Book book1(123, "CP5", "Lippman", "2012");
Book book2(123, "CP5", "Lippman", "2012");
if (book1 == book2)
std::cout << book1 << std::endl;
}
1.2 输入输出运算符
几点注意事项:
ostream
无法赋值
与iostream
标准库兼容的输入输出运算符必须是非成员函数(普通非成员函数)
如果,要定义类的自定义的IO运算符,则必须要定义其非成员函数。
IO运算符需要读写类的非共有数据成员,因此,IO运算符一般被声明为友元(P241)。
- 2.1 重载输出运算符 <<
通常,输出运算符 第一个形参是一个非常量ostream
对象的引用,第二个形参是一个待输出类类型的常量引用(不改变类内容)
Exercise 14.6: 为Sales_data类定义一个输出操作符: see Exercise 14.2.
std::ostream& operator<<(std::ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price();
return os;
}
**Exercise 14.7:**为 String
类定义一个输出操作符(P470)
#ifndef CP5_CH14_EX07_H_
#define CP5_CH14_EX07_H_
#include <memory>
#include <iostream>
class String
{
friend std::ostream& operator<<(std::ostream&, const String&);
public:
String() : String("") { }
String(const char *);
String(const String&);
String& operator=(const String&);
~String();
const char *c_str() const { return elements; }
size_t size() const { return end - elements; }
size_t length() const { return end - elements - 1; }
private:
std::pair<char*, char*> alloc_n_copy(const char*, const char*);
void range_initializer(const char*, const char*);
void free();
private:
char *elements;
char *end;
std::allocator<char> alloc;
};
std::ostream& operator<<(std::ostream&, const String&);
#include "ex14_07.h"
#include <algorithm>
#include <iostream>
std::pair<char*, char*>
String::alloc_n_copy(const char *b, const char *e)
{
auto str = alloc.allocate(e - b);
return{ str, std::uninitialized_copy(b, e, str) };
}
void String::range_initializer(const char *first, const char *last)
{
auto newstr = alloc_n_copy(first, last);
elements = newstr.first;
end = newstr.second;
}
String::String(const char *s)
{
char *sl = const_cast<char*>(s);
while (*sl)
++sl;
range_initializer(s, ++sl);
}
String::String(const String& rhs)
{
range_initializer(rhs.elements, rhs.end);
std::cout << "copy constructor" << std::endl;
}
void String::free()
{
if (elements) {
std::for_each(elements, end, [this](char &c){ alloc.destroy(&c); });
alloc.deallocate(elements, end - elements);
}
}
String::~String()
{
free();
}
String& String::operator = (const String &rhs)
{
auto newstr = alloc_n_copy(rhs.elements, rhs.end);
free();
elements = newstr.first;
end = newstr.second;
std::cout << "copy-assignment" << std::endl;
return *this;
}
std::ostream& operator<<(std::ostream &os, const String &s)
{
char *c = const_cast<char*>(s.c_str());
while (*c)
os << *c++;
return os;
}
#include "ex14_07.h"
int main()
{
String str("Hello World");
std::cout << str << std::endl;
}
- 2.2 重载输入运算符 >>
通常,第一个形参是读取流的引用,第二个形参是将要读入对象(非常量)的引用
std::istream& operator>>(std::istream &is, Sales_data &item)
{
double price = 0.0;
is >> item.bookNo >> item.units_sold >> price;
if (is)
item.revenue = price * item.units_sold;
else
item = Sales_data();
return is;
}
1.3 算术运算符和关系运算符
- 通常,算术和关系运算符定义成非成员函数,允许对左侧右侧的运算对象进行转换
- 如相等运算符与不等运算符可以把工作委托给另外一个。
- 关系运算符定义顺序关系,应该与关联容器中的关键字要求保持一致(P378)。
Exercise 14.7:如上Exercise 14.5,Book类的相等运算符。
StrVec
实例演示(你想要的全都有)( ex14_18)
#ifndef CP5_STRVEC_H_
#define CP5_STRVEC_H_
#include <memory>
#include <string>
#include <initializer_list>
#ifndef _MSC_VER
#define NOEXCEPT noexcept
#else
#define NOEXCEPT
#endif
class StrVec
{
friend bool operator==(const StrVec&, const StrVec&);
friend bool operator!=(const StrVec&, const StrVec&);
friend bool operator< (const StrVec&, const StrVec&);
friend bool operator> (const StrVec&, const StrVec&);
friend bool operator<=(const StrVec&, const StrVec&);
friend bool operator>=(const StrVec&, const StrVec&);
public:
StrVec() : elements(nullptr), first_free(nullptr), cap(nullptr) { }
StrVec(std::initializer_list<std::string>);
StrVec(const StrVec&);
StrVec& operator=(const StrVec&);
StrVec(StrVec&&) NOEXCEPT;
StrVec& operator=(StrVec&&)NOEXCEPT;
~StrVec();
void push_back(const std::string&);
size_t size() const { return first_free - elements; }
size_t capacity() const { return cap - elements; }
std::string *begin() const { return elements; }
std::string *end() const { return first_free; }
std::string& at(size_t pos) { return *(elements + pos); }
const std::string& at(size_t pos) const { return *(elements + pos); }
void reserve(size_t new_cap);
void resize(size_t count);
void resize(size_t count, const std::string&);
private:
std::pair<std::string*, std::string*> alloc_n_copy(const std::string*, const std::string*);
void free();
void chk_n_alloc() { if (size() == capacity()) reallocate(); }
void reallocate();
void alloc_n_move(size_t new_cap);
void range_initialize(const std::string*, const std::string*);
private:
std::string *elements;
std::string *first_free;
std::string *cap;
std::allocator<std::string> alloc;
};
bool operator==(const StrVec&, const StrVec&);
bool operator!=(const StrVec&, const StrVec&);
bool operator< (const StrVec&, const StrVec&);
bool operator> (const StrVec&, const StrVec&);
bool operator<=(const StrVec&, const StrVec&);
bool operator>=(const StrVec&, const StrVec&);
#endif
#include "ex13_39.h"
void StrVec::push_back(const std::string &s)
{
chk_n_alloc();
alloc.construct(first_free++, s);
}
std::pair<std::string*, std::string*>
StrVec::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 StrVec::free()
{
if (elements) {
for (auto p = first_free; p != elements;)
alloc.destroy(--p);
alloc.deallocate(elements, cap - elements);
}
}
StrVec::StrVec(const StrVec &rhs)
{
auto newdata = alloc_n_copy(rhs.begin(), rhs.end());
elements = newdata.first;
first_free = cap = newdata.second;
}
StrVec::~StrVec()
{
free();
}
StrVec& StrVec::operator = (const StrVec &rhs)
{
auto data = alloc_n_copy(rhs.begin(), rhs.end());
free();
elements = data.first;
first_free = cap = data.second;
return *this;
}
void StrVec::alloc_n_move(size_t new_cap)
{
auto newdata = alloc.allocate(new_cap);
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 + new_cap;
}
void StrVec::reallocate()
{
auto newcapacity = size() ? 2 * size() : 1;
alloc_n_move(newcapacity);
}
void StrVec::reserve(size_t new_cap)
{
if (new_cap <= capacity()) return;
alloc_n_move(new_cap);
}
void StrVec::resize(size_t count)
{
resize(count, std::string());
}
void StrVec::resize(size_t count, const std::string &s)
{
if (count > size()) {
if (count > capacity()) reserve(count * 2);
for (size_t i = size(); i != count; ++i)
alloc.construct(first_free++, s);
}
else if (count < size()) {
while (first_free != elements + count)
alloc.destroy(--first_free);
}
}
int main()
{
return 0;
}
#include "ex14_16_StrVec.h"
#include <iostream>
#include <vector>
int main()
{
StrVec vec;
vec.reserve(6);
std::cout << "capacity(reserve to 6): " << vec.capacity() << std::endl;
vec.reserve(4);
std::cout << "capacity(reserve to 4): " << vec.capacity() << std::endl;
vec.push_back("hello");
vec.push_back("world");
vec.resize(4);
for (auto i = vec.begin(); i != vec.end(); ++i)
std::cout << *i << std::endl;
std::cout << "-EOF-" << std::endl;
vec.resize(1);
for (auto i = vec.begin(); i != vec.end(); ++i)
std::cout << *i << std::endl;
std::cout << "-EOF-" << std::endl;
StrVec vec_list{ "hello", "world", "pezy" };
for (auto i = vec_list.begin(); i != vec_list.end(); ++i)
std::cout << *i << " ";
std::cout << std::endl;
// Test operator==
const StrVec const_vec_list{ "hello", "world", "pezy" };
if (vec_list == const_vec_list)
for (const auto &str : const_vec_list)
std::cout << str << " ";
std::cout << std::endl;
}
StrBlob
& StrBlobPtr
实例演示(你想要的全都有)
/*
=================================================================================
C++ Primer 5th Exercise Answer Source Code
StrBlob, StrBlobPtr, ConstStrBlobPtr
If you have questions, try to connect with me: pezy<urbancpz@gmail.com>
=================================================================================
*/
#ifndef CP5_STRBLOB_H_
#define CP5_STRBLOB_H_
#include <vector>
using std::vector;
#include <string>
using std::string;
#include <initializer_list>
using std::initializer_list;
#include <memory>
using std::make_shared; using std::shared_ptr;
#include <exception>
#ifndef _MSC_VER
#define NOEXCEPT noexcept
#else
#define NOEXCEPT
#endif
class StrBlobPtr;
class ConstStrBlobPtr;
//=================================================================================
//
// StrBlob - custom vector<string>
//
//=================================================================================
class StrBlob {
using size_type = vector<string>::size_type;
friend class ConstStrBlobPtr;
friend class StrBlobPtr;
friend bool operator==(const StrBlob&, const StrBlob&);
friend bool operator!=(const StrBlob&, const StrBlob&);
public:
StrBlob() : data(make_shared<vector<string>>()) { }
StrBlob(initializer_list<string> il) : data(make_shared<vector<string>>(il)) { }
StrBlob(const StrBlob &sb) : data(make_shared<vector<string>>(*sb.data)) { }
StrBlob& operator=(const StrBlob&);
StrBlob(StrBlob &&rhs) NOEXCEPT : data(std::move(rhs.data)) { }
StrBlob& operator=(StrBlob &&)NOEXCEPT;
StrBlobPtr begin();
StrBlobPtr end();
ConstStrBlobPtr cbegin() const;
ConstStrBlobPtr cend() const;
size_type size() const { return data->size(); }
bool empty() const { return data->empty(); }
void push_back(const string &t) { data->push_back(t); }
void push_back(string &&s) { data->push_back(std::move(s)); }
void pop_back();
string& front();
string& back();
const string& front() const;
const string& back() const;
private:
void check(size_type, const string&) const;
shared_ptr<vector<string>> data;
};
bool operator==(const StrBlob&, const StrBlob&);
bool operator!=(const StrBlob&, const StrBlob&);
inline void StrBlob::pop_back()
{
check(0, "pop_back on empty StrBlob");
data->pop_back();
}
inline string& StrBlob::front()
{
check(0, "front on empty StrBlob");
return data->front();
}
inline string& StrBlob::back()
{
check(0, "back on empty StrBlob");
return data->back();
}
inline const string& StrBlob::front() const
{
check(0, "front on empty StrBlob");
return data->front();
}
inline const string& StrBlob::back() const
{
check(0, "back on empty StrBlob");
return data->back();
}
inline void StrBlob::check(size_type i, const string &msg) const
{
if (i >= data->size()) throw std::out_of_range(msg);
}
//=================================================================================
//
// StrBlobPtr - custom iterator of StrBlob
//
//=================================================================================
class StrBlobPtr {
friend bool operator==(const StrBlobPtr&, const StrBlobPtr&);
friend bool operator!=(const StrBlobPtr&, const StrBlobPtr&);
public:
StrBlobPtr() : curr(0) { }
StrBlobPtr(StrBlob &s, size_t sz = 0) : wptr(s.data), curr(sz) { }
string& deref() const;
StrBlobPtr& incr();
private:
shared_ptr<vector<string>> check(size_t, const string&) const;
std::weak_ptr<vector<string>> wptr;
size_t curr;
};
bool operator==(const StrBlobPtr&, const StrBlobPtr&);
bool operator!=(const StrBlobPtr&, const StrBlobPtr&);
inline string& StrBlobPtr::deref() const
{
auto p = check(curr, "dereference past end");
return (*p)[curr];
}
inline StrBlobPtr& StrBlobPtr::incr()
{
check(curr, "increment past end of StrBlobPtr");
++curr;
return *this;
}
inline shared_ptr<vector<string>> StrBlobPtr::check(size_t i, const string &msg) const
{
auto ret = wptr.lock();
if (!ret) throw std::runtime_error("unbound StrBlobPtr");
if (i >= ret->size()) throw std::out_of_range(msg);
return ret;
}
//=================================================================================
//
// ConstStrBlobPtr - custom const_iterator of StrBlob
//
//=================================================================================
class ConstStrBlobPtr {
friend bool operator==(const ConstStrBlobPtr&, const ConstStrBlobPtr&);
friend bool operator!=(const ConstStrBlobPtr&, const ConstStrBlobPtr&);
public:
ConstStrBlobPtr() : curr(0) { }
ConstStrBlobPtr(const StrBlob &s, size_t sz = 0) : wptr(s.data), curr(sz) { }
const string& deref() const;
ConstStrBlobPtr& incr();
private:
std::shared_ptr<vector<string>> check(size_t, const string&) const;
std::weak_ptr<vector<string>> wptr;
size_t curr;
};
inline const string& ConstStrBlobPtr::deref() const
{
auto p = check(curr, "dereference past end");
return (*p)[curr];
}
inline ConstStrBlobPtr& ConstStrBlobPtr::incr()
{
check(curr, "increment past end of StrBlobPtr");
++curr;
return *this;
}
inline std::shared_ptr<vector<string>> ConstStrBlobPtr::check(size_t i, const string &msg) const
{
auto ret = wptr.lock();
if (!ret) throw std::runtime_error("unbound StrBlobPtr");
if (i >= ret->size()) throw std::out_of_range(msg);
return ret;
}
bool operator==(const ConstStrBlobPtr&, const ConstStrBlobPtr&);
bool operator!=(const ConstStrBlobPtr&, const ConstStrBlobPtr&);
#endif //CP5_STRBLOB_H_
#include "ex14_16_StrBlob.h"
//==================================================================
//
// operators
//
//==================================================================
bool operator==(const StrBlob &lhs, const StrBlob &rhs)
{
return *lhs.data == *rhs.data;
}
bool operator!=(const StrBlob &lhs, const StrBlob &rhs)
{
return !(lhs == rhs);
}
bool operator==(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
{
return lhs.curr == rhs.curr;
}
bool operator!=(const StrBlobPtr &lhs, const StrBlobPtr &rhs)
{
return !(lhs == rhs);
}
bool operator==(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs)
{
return lhs.curr == rhs.curr;
}
bool operator!=(const ConstStrBlobPtr &lhs, const ConstStrBlobPtr &rhs)
{
return !(lhs == rhs);
}
//==================================================================
//
// copy assignment operator and move assignment operator.
//
//==================================================================
StrBlob& StrBlob::operator=(const StrBlob &lhs)
{
data = make_shared<vector<string>>(*lhs.data);
return *this;
}
StrBlob& StrBlob::operator=(StrBlob &&rhs) NOEXCEPT
{
if (this != &rhs) {
data = std::move(rhs.data);
rhs.data = nullptr;
}
return *this;
}
//==================================================================
//
// members
//
//==================================================================
StrBlobPtr StrBlob::begin()
{
return StrBlobPtr(*this);
}
StrBlobPtr StrBlob::end()
{
return StrBlobPtr(*this, data->size());
}
ConstStrBlobPtr StrBlob::cbegin() const
{
return ConstStrBlobPtr(*this);
}
ConstStrBlobPtr StrBlob::cend() const
{
return ConstStrBlobPtr(*this, data->size());
}
#include "ex14_16_StrBlob.h"
#include <iostream>
int main()
{
StrBlob sb{ "Hello", "World", "Pezy" };
for (ConstStrBlobPtr iter = sb.cbegin(); iter != sb.cend(); iter.incr()) {
std::cout << iter.deref() << " ";
}
std::cout << std::endl;
}
String
实例演示(你想要的全都有)
#ifndef CP5_STRING_H__
#define CP5_STRING_H__
#include <memory>
#include <iostream>
#ifndef _MSC_VER
#define NOEXCEPT noexcept
#else
#define NOEXCEPT
#endif
class String
{
friend std::ostream& operator<<(std::ostream&, const String&);
friend std::istream& operator>>(std::istream&, String&);
friend bool operator==(const String&, const String&);
friend bool operator!=(const String&, const String&);
public:
String() : String("") { }
String(const char *);
String(const String&);
String& operator=(const String&);
String(String &&) NOEXCEPT;
String& operator=(String&&)NOEXCEPT;
~String();
void push_back(const char);
char* begin() const { return elements; }
char* end() const { return last_elem; }
const char *c_str() const { return elements; }
size_t size() const { return last_elem - elements; }
size_t length() const { return size(); }
size_t capacity() const { return cap - elements; }
void reserve(size_t);
void resize(size_t);
void resize(size_t, char);
private:
std::pair<char*, char*> alloc_n_copy(const char*, const char*);
void range_initializer(const char*, const char*);
void free();
void reallocate();
void alloc_n_move(size_t new_cap);
void chk_n_alloc() { if (first_free == cap) reallocate(); }
private:
char *elements;
char *last_elem;
char *first_free;
char *cap;
std::allocator<char> alloc;
};
std::ostream& operator<<(std::ostream&, const String&);
std::istream& operator>>(std::istream&, String&);
bool operator==(const String&, const String&);
bool operator!=(const String&, const String&);
#endif
#include "ex14_16_String.h"
#include <algorithm>
//===========================================================================
//
// operator - friend
//
//===========================================================================
std::ostream& operator<<(std::ostream &os, const String &lhs)
{
os << lhs.c_str();
return os;
}
std::istream& operator>>(std::istream &is, String &rhs)
{
for (char c; (c = is.get()) != '\n';) {
rhs.push_back(c);
}
return is;
}
bool operator==(const String &lhs, const String &rhs)
{
return (lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin()));
}
bool operator!=(const String &lhs, const String &rhs)
{
return !(lhs == rhs);
}
//===========================================================================
//
// Constructors
//
//===========================================================================
String::String(const char *s)
{
char *sl = const_cast<char*>(s);
while (*sl)
++sl;
range_initializer(s, ++sl);
}
//===========================================================================
//
// Big 5
//
//===========================================================================
String::String(const String& rhs)
{
range_initializer(rhs.elements, rhs.first_free);
}
String& String::operator = (const String &rhs)
{
auto newstr = alloc_n_copy(rhs.elements, rhs.first_free);
free();
elements = newstr.first;
first_free = cap = newstr.second;
last_elem = first_free - 1;
return *this;
}
String::String(String &&s) NOEXCEPT : elements(s.elements), last_elem(s.last_elem), first_free(s.first_free), cap(s.cap)
{
s.elements = s.last_elem = s.first_free = s.cap = nullptr;
}
String& String::operator = (String &&rhs) NOEXCEPT
{
if (this != &rhs) {
free();
elements = rhs.elements;
last_elem = rhs.last_elem;
first_free = rhs.first_free;
cap = rhs.cap;
rhs.elements = rhs.last_elem = rhs.first_free = rhs.cap = nullptr;
}
return *this;
}
String::~String()
{
free();
}
//===========================================================================
//
// members
//
//===========================================================================
void String::push_back(const char c)
{
chk_n_alloc();
*last_elem = c;
last_elem = first_free;
alloc.construct(first_free++, '\0');
}
void String::reallocate()
{
// \0 | -
// ^ ^
// elements first_free
// last_elem cap
auto newcapacity = size() ? 2 * (size() + 1) : 2;
alloc_n_move(newcapacity);
}
void String::alloc_n_move(size_t new_cap)
{
auto newdata = alloc.allocate(new_cap);
auto dest = newdata;
auto elem = elements;
for (size_t i = 0; i != size() + 1; ++i)
alloc.construct(dest++, std::move(*elem++));
free();
elements = newdata;
last_elem = dest - 1;
first_free = dest;
cap = elements + new_cap;
}
void String::free()
{
if (elements) {
std::for_each(elements, first_free, [this](char &c){ alloc.destroy(&c); });
alloc.deallocate(elements, cap - elements);
}
}
std::pair<char*, char*>
String::alloc_n_copy(const char *b, const char *e)
{
auto str = alloc.allocate(e - b);
return{ str, std::uninitialized_copy(b, e, str) };
}
void String::range_initializer(const char *first, const char *last)
{
auto newstr = alloc_n_copy(first, last);
elements = newstr.first;
first_free = cap = newstr.second;
last_elem = first_free - 1;
}
void String::reserve(size_t new_cap)
{
if (new_cap <= capacity()) return;
alloc_n_move(new_cap);
}
void String::resize(size_t count, char c)
{
if (count > size()) {
if (count > capacity()) reserve(count * 2);
for (size_t i = size(); i != count; ++i) {
*last_elem++ = c;
alloc.construct(first_free++, '\0');
}
}
else if (count < size()) {
while (last_elem != elements + count) {
--last_elem;
alloc.destroy(--first_free);
}
*last_elem = '\0';
}
}
void String::resize(size_t count)
{
resize(count, ' ');
}
#include <algorithm>
#include <iterator>
#include <string>
void foo(String x)
{
std::cout << x << std::endl;
}
void bar(const String& x)
{
std::cout << x.c_str() << std::endl;
}
String baz()
{
String ret("world");
return ret;
}
int main()
{
char text[] = "world";
String s0;
String s1("hello");
String s2(std::move(s0));
String s3 = s1;
String s4(text);
s2 = s1;
if (s2 == s1)
std::cout << "s2 == s1" << std::endl;
foo(s1);
bar(s1);
foo("temporary");
bar("temporary");
String s5 = baz();
std::vector<String> svec;
//svec.push_back(s0);
svec.push_back(s1);
svec.push_back(s2);
svec.push_back(s3);
svec.push_back(s4);
svec.push_back(baz());
svec.push_back("good job");
for (const auto &s : svec) {
std::cout << s << std::endl;
}
std::cout << "Input a string: ";
String s6;
std::cin >> s6;
std::cout << s6 << std::endl;
}
1.4 赋值运算符
-
赋值运算符必须定义为类的成员,复合赋值运算符通常定义为类的成员。
-
这2个运算符都必须返回左侧运算对象的引用 * this
Exercise 14.21:实例如下
栗子1:
Sales_data& Sales_data::operator+= (const Sales_data &rhs){ units_sold += rhs.units_sold; revenue += rhs.revenue; return *this; }
栗子2:
Sales_data& Sales_data::operator+=(const Sales_data &rhs) { Sales_data old_data = *this; *this = old_data + rhs; return *this; } Sales_data operator+(const Sales_data &lhs, const Sales_data &rhs) { Sales_data sum; sum.units_sold = lhs.units_sold + rhs.units_sold; sum.revenue = lhs.revenue + rhs.revenue; return sum; }
Disadvantages: +和+=都使用Sales_data的临时对象。但这是没有必要的。
Exercise 14.22:
定义赋值操作符的一个版本,可以将表示ISBN的字符串赋值给Sales_data。
#include <string>
#include <iostream>
class Sales_data {
friend std::istream& operator>>(std::istream&, Sales_data&);
friend std::ostream& operator<<(std::ostream&, const Sales_data&);
friend Sales_data operator+(const Sales_data&, const Sales_data&);
public:
Sales_data(const std::string &s, unsigned n, double p):bookNo(s), units_sold(n), revenue(n*p){ }
Sales_data() : Sales_data("", 0, 0.0f){ }
Sales_data(const std::string &s) : Sales_data(s, 0, 0.0f){ }
Sales_data(std::istream &is);
Sales_data& operator=(const std::string&);
Sales_data& operator+=(const Sales_data&);
std::string isbn() const { return bookNo; }
private:
inline double avg_price() const;
std::string bookNo;
unsigned units_sold = 0;
double revenue = 0.0;
};
std::istream& operator>>(std::istream&, Sales_data&);
std::ostream& operator<<(std::ostream&, const Sales_data&);
Sales_data operator+(const Sales_data&, const Sales_data&);
inline double Sales_data::avg_price() const
{
return units_sold ? revenue/units_sold : 0;
}
#include "ex14_22.h"
Sales_data::Sales_data(std::istream &is) : Sales_data()
{
is >> *this;
}
Sales_data& Sales_data::operator+=(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
std::istream& operator>>(std::istream &is, Sales_data &item)
{
double price = 0.0;
is >> item.bookNo >> item.units_sold >> price;
if (is)
item.revenue = price * item.units_sold;
else
item = Sales_data();
return is;
}
std::ostream& operator<<(std::ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price();
return os;
}
Sales_data operator+(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum += rhs;
return sum;
}
Sales_data& Sales_data::operator=(const std::string &isbn)
{
*this = Sales_data(isbn);
return *this;
}
#include "ex14_22.h"
int main()
{
std::string strCp5("C++ Primer 5th");
Sales_data cp5;
cp5 = strCp5;
std::cout << cp5 << std::endl;
}
Exercise 14.23: 为你的StrVec类定义一个initializer_list赋值操作符。
#include <memory>
#include <string>
#include <initializer_list>
#ifndef _MSC_VER
#define NOEXCEPT noexcept
#else
#define NOEXCEPT
#endif
class StrVec
{
friend bool operator==(const StrVec&, const StrVec&);
friend bool operator!=(const StrVec&, const StrVec&);
friend bool operator< (const StrVec&, const StrVec&);
friend bool operator> (const StrVec&, const StrVec&);
friend bool operator<=(const StrVec&, const StrVec&);
friend bool operator>=(const StrVec&, const StrVec&);
public:
StrVec() : elements(nullptr), first_free(nullptr), cap(nullptr) { }
StrVec(std::initializer_list<std::string>);
StrVec(const StrVec&);
StrVec& operator=(const StrVec&);
StrVec(StrVec&&) NOEXCEPT;
StrVec& operator=(StrVec&&)NOEXCEPT;
~StrVec();
StrVec& operator=(std::initializer_list<std::string>);
void push_back(const std::string&);
size_t size() const { return first_free - elements; }
size_t capacity() const { return cap - elements; }
std::string *begin() const { return elements; }
std::string *end() const { return first_free; }
std::string& at(size_t pos) { return *(elements + pos); }
const std::string& at(size_t pos) const { return *(elements + pos); }
void reserve(size_t new_cap);
void resize(size_t count);
void resize(size_t count, const std::string&);
private:
std::pair<std::string*, std::string*> alloc_n_copy(const std::string*, const std::string*);
void free();
void chk_n_alloc() { if (size() == capacity()) reallocate(); }
void reallocate();
void alloc_n_move(size_t new_cap);
void range_initialize(const std::string*, const std::string*);
private:
std::string *elements;
std::string *first_free;
std::string *cap;
std::allocator<std::string> alloc;
};
bool operator==(const StrVec&, const StrVec&);
bool operator!=(const StrVec&, const StrVec&);
bool operator< (const StrVec&, const StrVec&);
bool operator> (const StrVec&, const StrVec&);
bool operator<=(const StrVec&, const StrVec&);
bool operator>=(const StrVec&, const StrVec&);
#include "ex14_23.h"
#include <algorithm> // for_each, equal
void StrVec::push_back(const std::string &s)
{
chk_n_alloc();
alloc.construct(first_free++, s);
}
std::pair<std::string*, std::string*>
StrVec::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 StrVec::free()
{
if (elements) {
for_each(elements, first_free, [this](std::string &rhs){ alloc.destroy(&rhs); });
alloc.deallocate(elements, cap - elements);
}
}
void StrVec::range_initialize(const std::string *first, const std::string *last)
{
auto newdata = alloc_n_copy(first, last);
elements = newdata.first;
first_free = cap = newdata.second;
}
StrVec::StrVec(const StrVec &rhs)
{
range_initialize(rhs.begin(), rhs.end());
}
StrVec::StrVec(std::initializer_list<std::string> il)
{
range_initialize(il.begin(), il.end());
}
StrVec::~StrVec()
{
free();
}
StrVec& StrVec::operator = (const StrVec &rhs)
{
auto data = alloc_n_copy(rhs.begin(), rhs.end());
free();
elements = data.first;
first_free = cap = data.second;
return *this;
}
void StrVec::alloc_n_move(size_t new_cap)
{
auto newdata = alloc.allocate(new_cap);
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 + new_cap;
}
void StrVec::reallocate()
{
auto newcapacity = size() ? 2 * size() : 1;
alloc_n_move(newcapacity);
}
void StrVec::reserve(size_t new_cap)
{
if (new_cap <= capacity()) return;
alloc_n_move(new_cap);
}
void StrVec::resize(size_t count)
{
resize(count, std::string());
}
void StrVec::resize(size_t count, const std::string &s)
{
if (count > size()) {
if (count > capacity()) reserve(count * 2);
for (size_t i = size(); i != count; ++i)
alloc.construct(first_free++, s);
}
else if (count < size()) {
while (first_free != elements + count)
alloc.destroy(--first_free);
}
}
StrVec::StrVec(StrVec &&s) NOEXCEPT : elements(s.elements), first_free(s.first_free), cap(s.cap)
{
// leave s in a state in which it is safe to run the destructor.
s.elements = s.first_free = s.cap = nullptr;
}
StrVec& StrVec::operator = (StrVec &&rhs) NOEXCEPT
{
if (this != &rhs) {
free();
elements = rhs.elements;
first_free = rhs.first_free;
cap = rhs.cap;
rhs.elements = rhs.first_free = rhs.cap = nullptr;
}
return *this;
}
bool operator==(const StrVec &lhs, const StrVec &rhs)
{
return (lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin()));
}
bool operator!=(const StrVec &lhs, const StrVec &rhs)
{
return !(lhs == rhs);
}
bool operator<(const StrVec &lhs, const StrVec &rhs)
{
return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}
bool operator>(const StrVec &lhs, const StrVec &rhs)
{
return rhs < lhs;
}
bool operator<=(const StrVec &lhs, const StrVec &rhs)
{
return !(rhs < lhs);
}
bool operator>=(const StrVec &lhs, const StrVec &rhs)
{
return !(lhs < rhs);
}
StrVec& StrVec::operator=(std::initializer_list<std::string> il)
{
auto data = alloc_n_copy(il.begin(), il.end());
free();
elements = data.first;
first_free = cap = data.second;
return *this;
}
#include "ex14_23.h"
#include <iostream>
#include <vector>
int main()
{
StrVec vec;
vec.reserve(6);
std::cout << "capacity(reserve to 6): " << vec.capacity() << std::endl;
vec.reserve(4);
std::cout << "capacity(reserve to 4): " << vec.capacity() << std::endl;
vec.push_back("hello");
vec.push_back("world");
vec.resize(4);
for (auto i = vec.begin(); i != vec.end(); ++i)
std::cout << *i << std::endl;
std::cout << "-EOF-" << std::endl;
vec.resize(1);
for (auto i = vec.begin(); i != vec.end(); ++i)
std::cout << *i << std::endl;
std::cout << "-EOF-" << std::endl;
StrVec vec_list{ "hello", "world", "pezy" };
for (auto i = vec_list.begin(); i != vec_list.end(); ++i)
std::cout << *i << " ";
std::cout << std::endl;
// Test operator==
const StrVec const_vec_list = { "hello", "world", "pezy" };
if (vec_list == const_vec_list)
for (const auto &str : const_vec_list)
std::cout << str << " ";
std::cout << std::endl;
// Test operator<
const StrVec const_vec_list_small = { "hello", "pezy", "ok" };
std::cout << (const_vec_list_small < const_vec_list) << std::endl;
}