开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
3 Star 10 Fork 6

secondtonone1/cpplearn

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
cpplearn
/
src
/
strvec.cpp
cpplearn
/
src
/
strvec.cpp
strvec.cpp 4.09 KB
一键复制 编辑 原始数据 按行查看 历史
secondtonone1 提交于 2022年03月02日 15:21 +08:00 . binary && derive
#include "../inc/strvec.h"
#include <algorithm>
#include <iostream>
#include <memory>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
std::allocator<std::string> StrVec::alloc;
void StrVec::push_back(const std::string &s)
{
chk_n_alloc();
alloc.construct(first_free++, s);
}
//重新开辟空间
void StrVec::reallocate()
{
string *newdata = nullptr;
//数组为空的情况
if (elements == nullptr || cap == nullptr || first_free == nullptr)
{
newdata = alloc.allocate(1);
// elements和first_free都指向首元素
elements = newdata;
first_free = newdata;
// cap指向数组尾元素的下一个位置。
cap = newdata + 1;
return;
}
//不为空则扩充两倍空间
newdata = alloc.allocate(size() * 2);
//新内存空闲位置
auto dest = newdata;
//旧内存有效位置
auto src = elements;
//通过移动操作将旧数据放到新内存中
for (size_t i = 0; i != size(); ++i)
{
alloc.construct(dest++, std::move(*src++));
}
//移动后旧内存数据无效,一定要删除
free();
//更新数据位置
elements = newdata;
//更新第一个空闲位置
first_free = dest;
//更新容量
cap = elements + size() * 2;
}
//释放操作
void StrVec::free()
{
//判断elements是否为空
if (elements == nullptr)
{
return;
}
auto dest = elements;
//要先遍历析构每一个对象
for (size_t i = 0; i < size(); i++)
{
// destroy会调用每一个元素的析构函数
alloc.destroy(dest++);
}
//再整体回收内存
alloc.deallocate(elements, cap - elements);
elements = nullptr;
cap = nullptr;
first_free = nullptr;
}
// copy指定范围的元素到新的内存中,返回新元素的地址和第一个空闲元素地址的pair
std::pair<std::string *, std::string *> StrVec::alloc_n_copy(
const std::string *b, const std::string *e)
{
auto newdata = alloc.allocate(e - b);
//将原数据用来初始化新空间
auto first_free = uninitialized_copy(b, e, newdata);
return {newdata, first_free};
}
//拷贝构造函数
StrVec::StrVec(const StrVec &strtmp)
{
//将形参数据拷贝给自己
auto rsp = alloc_n_copy(strtmp.begin(), strtmp.end());
//更新elements, cap,first_free
elements = rsp.first;
first_free = rsp.second;
cap = rsp.second;
}
//拷贝赋值运算符
StrVec &StrVec::operator=(const StrVec &strtmp)
{
cout << "this is copy operator = " << endl;
//防止自赋值
if (this == &strtmp)
{
return *this;
}
//将形参数据拷贝给自己
auto rsp = alloc_n_copy(strtmp.begin(), strtmp.end());
//更新elements, cap,first_free
elements = rsp.first;
first_free = rsp.second;
cap = rsp.second;
}
//析构
StrVec::~StrVec()
{
free();
}
//抛出元素
void StrVec::pop_back(std::string &s)
{
if (first_free == nullptr)
{
return;
}
if (size() == 1)
{
s = *elements;
alloc.destroy(elements);
first_free = nullptr;
elements = nullptr;
return;
}
s = *(--first_free);
alloc.destroy(first_free);
}
void test_strvec_old()
{
auto str1 = StrVec();
str1.push_back("hello zack");
StrVec str2(str1);
str2.push_back("hello rolin");
StrVec str3 = str1;
string strtmp;
str3.pop_back(strtmp);
}
void test_strvec()
{
StrVec v1, v2;
//左值赋值
v1 = v2;
//函数返回的是右值,
//右值赋值会调用移动赋值
v2 = get_vec_str();
}
//移动赋值运算符
StrVec &StrVec::operator=(StrVec &&src)
{
cout << "this is move operator = " << endl;
if (this != &src)
{
//释放自己的空间操作
this->free();
//接管源对象资源
this->elements = src.elements;
this->first_free = src.first_free;
this->cap = src.cap;
//将源对象成员赋值为空
src.elements = src.first_free = src.cap = nullptr;
}
return *this;
}
StrVec get_vec_str()
{
return StrVec();
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

cpp 学习笔记,项目源码
取消

发行版

暂无发行版

贡献者

全部

语言

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/secondtonone1/cpplearn.git
git@gitee.com:secondtonone1/cpplearn.git
secondtonone1
cpplearn
cpplearn
master
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /