标题:这个Resize()成员函数该怎么定义才能调整向量的个数啊
取消只看楼主
深藏依旧
Rank: 2
等 级:论坛游民
帖 子:45
专家分:93
注 册:2012-12-8
结帖率:100%
已结贴  问题点数:18 回复次数:0 
这个Resize()成员函数该怎么定义才能调整向量的个数啊
#include<iostream>
using namespace std;
class vector
{
private:
    int* buffer;
    int _size;
public:
    vector(int size = 10) : _size(size)
    {
        buffer = new int[size];
    }
    ~vector()
    {
        delete[] buffer;
    }
    vector(const vector& other)
        : _size(other._size)
    {
        buffer = new int[other._size];
        for (int i = 0; i < _size; i++)
            buffer[i] = other.buffer[i];
    }
    Resize()//成员函数
    {
        
    }

    int& operator[] (int index)
    {
        if (index >= _size)
        {
            int* data = new int[index + 10];
            for (int i = 0; i < _size; i++)
                data[i] = buffer[i];
            delete[] buffer;

            buffer = data;
            _size = index + 10;
        }

        return buffer[index];
    }
};
void main()
{
    vector a(10);
    for(int i=0;i<10;i++)a[i]=i+1;
    a.Resize(20);
    for(i=0;i<20;i++)cout<<a[i]<<endl;

}
搜索更多相关主题的帖子: private include public 
2012-12-08 16:55



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-390185-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.175976 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved