【足迹C++primer】47、Moving Objects(2)
版权声明:本文为博主原创文章。未经博主同意不得转载。 https://blog.csdn.net/cutter_point/article/details/37954805
Rvalue References and Member Functions
StrVec.h
#ifndef STRVEC_H
#define STRVEC_H
#include <iostream>
#include <memory>
#include <utility>
// simplified implementation of the memory allocation strategy for a vector-like class
class StrVec {
public:
// copy control members
StrVec():
elements(0), first_free(0), cap(0) { }
StrVec(const StrVec&); // copy constructor
StrVec &operator=(const StrVec&); // copy assignment
~StrVec(); // destructor
// additional constructor
StrVec(const std::string*, const std::string*);
void push_back(const std::string&); // copy the element
void push_back(std::string&&); //move the element
// add elements
size_t size() const { return first_free - elements; }
size_t capacity() const { return cap - elements; }
// iterator interface
std::string *begin() const { return elements; }
std::string *end() const { return first_free; }
// operator functions covered in chapter 14
std::string& operator[](std::size_t n)
{ return elements[n]; }
const std::string& operator[](std::size_t n) const
{ return elements[n]; }
private:
static std::allocator<std::string> alloc; // allocates the elements
// utility functions:
// used by members that add elements to the StrVec
void chk_n_alloc()
{ if (size() == capacity()) reallocate(); }
// used by the copy constructor, assignment operator, and destructor
std::pair<std::string*, std::string*> alloc_n_copy
(const std::string*, const std::string*);
void free(); // destroy the elements and free the space
void reallocate(); // get more space and copy the existing elements
std::string *elements; // pointer to the first element in the array
std::string *first_free; // pointer to the first free element in the array
std::string *cap; // pointer to one past the end of the array
};
#include <algorithm>
inline
StrVec::~StrVec() { free(); }
inline
std::pair<std::string*, std::string*>
StrVec::alloc_n_copy(const std::string *b, const std::string *e)
{
// allocate space to hold as many elements as are in the range
std::string *data = alloc.allocate(e - b);
// initialize and return a pair constructed from data and
// the value returned by uninitialized_copy
return std::make_pair(data, uninitialized_copy(b, e, data));
}
inline
StrVec::StrVec(const StrVec &s)
{
// call alloc_n_copy to allocate exactly as many elements as in s
std::pair<std::string*, std::string*> newdata =
alloc_n_copy(s.begin(), s.end());
elements = newdata.first;
first_free = cap = newdata.second;
}
inline
void StrVec::free()
{
// may not pass deallocate a 0 pointer; if elements is 0, there's no work to do
if (elements) {
// destroy the old elements in reverse order
for (std::string *p = first_free; p != elements; /* empty */)
alloc.destroy(--p);
alloc.deallocate(elements, cap - elements);
}
}
inline
StrVec &StrVec::operator=(const StrVec &rhs)
{
// call alloc_n_copy to allocate exactly as many elements as in rhs
std::pair<std::string*, std::string*> data =
alloc_n_copy(rhs.begin(), rhs.end());
free();
elements = data.first;
first_free = cap = data.second;
return *this;
}
inline
void StrVec::reallocate()
{
// we'll allocate space for twice as many elements as the current size
size_t newcapacity = size() ? 2 * size() : 1;
// allocate new memory
std::string *newdata = alloc.allocate(newcapacity);
// copy the data from the old memory to the new
std::string *dest = newdata; // points to the next free position in the new array
std::string *elem = elements; // points to the next element in the old array
for (size_t i = 0; i != size(); ++i)
alloc.construct(dest++, *elem++);
free(); // free the old space once we've moved the elements
// update our data structure to point to the new elements
elements = newdata;
first_free = dest;
cap = elements + newcapacity;
}
inline
StrVec::StrVec(const std::string *b, const std::string *e)
{
// call alloc_n_copy to allocate exactly as many elements as in il
std::pair<std::string*, std::string*> newdata = alloc_n_copy(b, e);
elements = newdata.first;
first_free = cap = newdata.second;
}
#endif
StrVec.cc
#include "StrVec.h"
#include <string>
#include <utility>
// errata fixed in second printing --
// StrVec's allocator should be a static member not an ordinary member
// definition for static data member
std::allocator<std::string> StrVec::alloc;
// all other StrVec members are inline and defined inside StrVec.h
inline
void StrVec::push_back(const string& s)
{
chk_n_alloc(); // ensure that there is room for another element
// construct a copy of s in the element to which first_free points
alloc.construct(first_free++, s);
}
inline
void StrVec::push_back(string&& s)
{
chk_n_alloc();
alloc.construct(first_free++, std::move(s));
}
void fun1()
{
StrVec vec;
string s="my name is cutter_point!";
vec.push_back(s); //调用const string&这个
vec.push_back("China!"); //调用string&&
}
class Foo
{
public:
Foo()=default;
Foo(const Foo&); //copy构造函数
Foo &operator=(const Foo&) &; //返回一个左值&&表示返回右值
// Foo someMem() & const; //错误,const应放第一位
Foo anotherMem() const &; //正确
};
Foo &Foo::operator=(const Foo &rhs) &
{
return *this;
}
Foo::Foo(const Foo &f)
{
}
void fun2()
{
Foo &retFoo(); //返回一个引用,是一个左值
Foo retVal(); //返回一个值,右值调用
Foo i, j; //i,j是左值
i=j; //i是左值
// retFoo()=j; //OK这个返回的是一个左值
// retVal()=j; //错误。retVal是一个右值
}
Overloading and Reference Functions
class Foo
{
public:
Foo()=default;
// Foo(const Foo&); //copy构造函数
// Foo(Foo&&);
Foo &operator=(const Foo&) &; //返回一个左值&&表示返回右值
// Foo someMem() & const; //错误。const应放第一位
Foo anotherMem() const &; //正确
Foo sorted() &&; //返回右值
Foo sorted() const &; //返回左值
private:
vector<int> data;
};
Foo Foo::sorted() &&
{
sort(data.begin(), data.end());
return *this;
}
Foo Foo::sorted() const &
{
Foo ret(*this);
sort(ret.data.begin(), ret.data.end());
return ret;
}
int main()
{
string s1="a value", s2="this is a pig.";
auto n=(s1+s2).find('e');
cout<<n<<endl;
return 0;
}
PS:如今的感觉是非常不爽。不知道学这玩意什么时候才干去找工作。不知道去那好,还是耐心点。耐着性子,仅仅要默默地向自己的目标前进就好。未来有我的机遇等着我,我如今要做的就是在机遇来的时候我能够狠狠的抓住!
!!
以免懊悔一生
【足迹C++primer】47、Moving Objects(2)的更多相关文章
- 【足迹C++primer】47、Moving Objects(1)
Moving Objects(1) * 功能:Moving Objects * 时间:2014年7月17日08:46:45 * 作者:cutter_point */ #include<iostr ...
- 【足迹C++primer】46、动态存储类
动态存储类 StrVec Class Design StrVec Class Definition class StrVec { public: //构造函数 StrVec():elements(nu ...
- 【足迹C++primer】52、,转换和继承虚函数
转换和继承,虚函数 Understanding conversions between base and derived classes is essential to understanding h ...
- 【足迹C++primer】48、函数引用操作符
函数引用操作符 struct absInt { int operator()(int val) const { cout<<val<<"<->!!!&qu ...
- [paper]MaskFusion: Real-Time Recognition, Tracking and Reconstruction of Multiple Moving Objects
Before 近期在调研关于RGBD在室内移动机器人下的语义导航的研究.目前帝国理工的Andrew Davison在这边有两个团队在研究,分别是Fusion++ 和 这篇 MaskFusion.这篇是 ...
- 【足迹C++primer】表达式求值
表达式求值 /** * 功能:表达式求值(0到9) * 时间:2014年6月15日08:02:31 * 作者:cutter_point */ #include<stdlib.h> #inc ...
- 【足迹C++primer】30、概要(泛型算法)
概要(泛型算法) 大多数算法的头文件中定义algorithm在. 标准库也是第一个文件numeric它定义了一套通用算法. #include<iostream> #include<n ...
- 【足迹C++primer】40、动态数组
动态数组 C++语言定义了第二种new表达式语法.能够分配并初始化一个对象数组.标准库中包括 一个名为allocator的类.同意我们将分配和初始化分离. 12.2.1 new和数组 void fun ...
- 【足迹C++primer】49、超载,变化,运营商
超载,变化,运营商 Conversion Operators 转换操作符 operator type() const Conversions to an array or a function typ ...
随机推荐
- bootstrap3-javascript插件- 慕课笔记
bootstrap支持的js插件 说明:文中内容系本人在某网站学习笔记,本着本站禁止推广的原则,在此就不著明学习站点的名称及地址.其实开博客的目的也就是为了方便记录学习,因为平时本地的记录太多丢三落四 ...
- AttributeError: module 'requests' has no attribute 'get'的错误疑惑
我发现文件直接用requests.get(url)会提示我AttributeError: module 'requests' has no attribute 'get' 我把问题百度了一下,解决方法 ...
- C++中的静态成员函数
1,问完成的需求: 1,统计在程序运行期间某个类的对象数目: 1,静态成员变量满足了这个需求: 2,保证程序的安全性(不能使用全局变量): 3,随时可以获取当前对象的数目: 1,有没有什么特别的地方或 ...
- 39.Binary Tree Inorder Traversal(二叉树中序遍历)
Level: Medium 题目描述: Given a binary tree, return the inorder traversal of its nodes' values. 思路分析: ...
- 简述IOC和AOP的作用
IOC: 控制反转,是一种设计模式.一层含义是控制权的转移:由传统的在程序中控制依赖转移到由容器来控制:第二层是依赖注入:将相互依赖的对象分离,在spring配置文件中描述他们的依赖关系.他们的依赖关 ...
- 深入学习Redis持久化
一.Redis高可用概述 在介绍Redis高可用之前,先说明一下在Redis的语境中高可用的含义. 我们知道,在web服务器中,高可用是指服务器可以正常访问的时间,衡量的标准是在多长时间内可以提供正常 ...
- 四、IDS4建立Authorization server和Client
一.准备 创建一个名为QuickstartIdentityServer的ASP.NET Core Web 空项目(asp.net core 2.2),端口5000创建一个名为Api的ASP.NET C ...
- PostgreSQL的学习-序列
序列对象(也叫序列生成器)都是用CREATE SEQUENCE创建的特殊的单行表.一个序列对象通常用于为行或者表生成唯一的标识符.下面序列函数,为我们从序列对象中获取最新的序列值提供了简单和并发读取安 ...
- HDU-3333 Turing Tree 分块求区间不同数和
HDU-3333 Turning Tree 题目大意:先给出n个数字.面对q个询问区间,输出这个区间不同数的和. 题解:这道题有几种解法.这里讲一下用分块解决的方法.( 离线树状数组解法看这里 Hdu ...
- Codeforces 362E 费用流
题意及思路:https://blog.csdn.net/mengxiang000000/article/details/52472696 代码: #define Hello the_cruel_wor ...