首页 技术 正文
技术 2022年11月9日
0 收藏 764 点赞 3,772 浏览 1911 个字

今天看了primer C++的 “动态内存管理类”章节,里面的例子是模拟实现std::vector<std::string>的功能。

照抄之后发现编译不通过,有个库函数调用错误,就参考着自己写了一份简单的int版。

实现思路:

1.初始化时,容器容量为1。

2.往容器添加数据时,如果容器没有放满,就直接放进去。

3.往容器添加数据时,如果容器已经满了:

  3.1 申请新的更大的存储空间(2倍)

  3.2 把原来的数据复制到新的存储空间

  3.3 删除旧的存储空间

  3.4放入数据

代码如下:

// 13.5动态内存管理类VecInt.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"
#include "iostream"
using namespace std;class VecInt
{ //容器变量
private:
int* arr;
int capacity;
int size;public: //构造函数
VecInt()
:arr(nullptr), capacity(), size()
{
reallocate();
} ~VecInt(){ delete arr; } //拷贝构造函数
VecInt(const VecInt &s); //拷贝赋值函数
VecInt& VecInt::operator=(const VecInt &rhs); //重新分配内存
void reallocate(); //放入数据
void push_back(const int s); //STL标准兼容
int *begin() const { return arr; }
int *end() const { return &arr[size]; }
int& operator[](int i){ return arr[i]; }};void VecInt::push_back(const int s)
{
//检查空间是否足够
if (size == capacity)
reallocate(); arr[size++] = s;
}//重新分配内存
void VecInt::reallocate()
{
int newCapacity = capacity ? * capacity : ; int * newArr = new int[newCapacity]; //arr为nullptr也能正常运行
memcpy(newArr, arr, capacity*sizeof(int));
delete arr; arr = newArr;
capacity = newCapacity;}VecInt::VecInt(const VecInt &s)
{
cout << "VecInt 拷贝构造函数" << endl;
capacity = s.capacity;
arr = new int[capacity];
size = s.size; //arr为nullptr也能正常运行
memcpy(arr, s.arr, s.capacity*sizeof(int));}VecInt& VecInt::operator=(const VecInt &s)
{
cout << "VecInt 拷贝赋值函数" << endl;
capacity = s.capacity;
arr = new int[capacity];
size = s.size; //arr为nullptr也能正常运行
memcpy(arr, s.arr, s.capacity*sizeof(int));
return *this;
}int _tmain(int argc, _TCHAR* argv[])
{
VecInt a; //输入数据
for (int i = ; i <= ; i++)
{
a.push_back(i);
} cout << "a:" << endl;
for (auto v : a)
{
cout << v << " ";
if (v % == )
cout << endl;
}
cout << endl; cout << "b :" << endl;
VecInt b(a); cout << "c :" << endl;
VecInt c;
c = a; a[] = ;
b[] = ;
c[] = ; //打印数据
cout << "a:" << endl;
for (auto v : a)
{
cout << v << " ";
if (v % == )
cout << endl;
}
cout << endl; cout << "b:" << endl;
for (auto v : b)
{
cout << v << " ";
if (v % == )
cout << endl;
}
cout << endl; cout << "c:" << endl;
for (auto v : c)
{
cout << v << " ";
if (v % == )
cout << endl;
}
cout << endl; cout << "" << endl;
{
VecInt temp;
for (int i = ; i < ; i++)
{
temp.push_back(i);
}
} cout << "" << endl; return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294