c ++字符串指针数组的初始化

时间:2013-01-23 08:33:58

标签: c++

我想初始化一个从构造函数中获得的std::string指针大小的数组。另外,我想对两个int数组做同样的事情,但下面的代码不能编译:

class MyQuickInitArray
{
public:
    MyQuickInitArray(int size)
    {
        if(size <= 0)
        {
            throw new std::exception;
        }

        _size = size;
        _counter = 0;
        A = new std::string[size];
        B = new int[size];
        C = new int[size];
    }

    std::string& operator[](int j) {
        if(j > _size)
        {
            throw std::out_of_range("out of range");
        }

        if(((B[j]<0) || (B[j]>=_counter)) || (C[B[j]]!=j))
        {
            // A[j] points to junk
            _counter++;
            A[j] = new std::string;
            B[j] = _counter-1;
            C[_counter-1] = j;
            return A[j];
        }

        // the cell was instantiated before
        return A[j];
    }

    ~MyQuickInitArray(){};


private:
    std::string* A[];
    int B[];
    int C[];
    int _size;
    int _counter;
};

如何正确声明我从ctor获得的大小数组?

修改

发生的错误是:

incompatible types in assignment of ‘std::string* {aka std::basic_string<char>*}’ to ‘std::string* [0] 

对于int数组:

incompatible types in assignment of ‘int*’ to ‘int [0]’

2 个答案:

答案 0 :(得分:3)

它们不是在C ++中声明静态数组的有效方法,需要在编译时知道数组大小。下面的代码无法在没有特殊扩展的情况下在标准C ++中编译。

std::string* A[];
int B[];
int C[];

如果你只是玩指针很好。 但是,您最好考虑使用std::vector

#include <vector>
std::vector<std::string> A;
std::vector<int> B;
std::vector<int> C;

我可能会将您的代码改写为:

class MyQuickInitArray
{
public:
    MyQuickInitArray(int size)
      : A(size),
        B(size),
        C(size),
        size_(size),
        counter_(0)
    {     
    }

    std::string operator[](int j) const 
    {
        return A.at(j);
    }

private:
    std::vector<std::string> A;
    std::vector<int> B;
    std::vector<int> C;
    int size_;
    int counter_;
};

答案 1 :(得分:0)

我认为您希望使用std::vector<std::string>std::vector<int>代替您的数组成员。整个代码会更简单。

但是如果你真的想使用数组,那么你可以试试(不是我推荐的):

class MyQuickInitArray
{
public:
    MyQuickInitArray(int size):_size(0),_counter(0),A(nullptr),B(nullptr),C(nullptr)
    {
        if(size <= 0)
        {
            throw new std::exception;
        }

        _size = size;

        typedef std::string* pSTR;
        A = new pSTR[size];
        B = new int[size];
        C = new int[size];
    }

    std::string& operator[](int j) {
        if(j > _size)
        {
            throw std::out_of_range("out of range");
        }

        if(((B[j]<0) || (B[j]>=_counter)) || (C[B[j]]!=j))
        {
            // A[j] points to junk
            _counter++;
            A[j] = new std::string;
            B[j] = _counter-1;
            C[_counter-1] = j;
            return *A[j];
        }

        // the cell was instantiated before
        return *A[j];
    }

            ~MyQuickInitArray(){
               for(int j=0; j<_size; ++j)
                   if(((B[j]<0) || (B[j]>=_counter)) || (C[B[j]]!=j))
                      delete A[j];                       
               delete[]A; delete[]B; delete[]C;};
            }


private:
    std::string** A;
    int *B;
    int *C;
    int _size;
    int _counter;
};
相关问题