c++ - arrayList<string> 问题

标签 c++ string

这是 arrayList 类:

template<class T>
class arrayList: public linearList<T> 
{

public:
    // constructor, copy constructor and destructor
    arrayList(int initialCapacity = 10);
    arrayList(const arrayList<T>&);
    ~arrayList() {
        delete[] element;
    }

void insert(int theIndex, const T& theElement);
protected:
    T* position;
}; // end of iterator class

protected:
    // additional members of arrayList
    void checkIndex(int theIndex) const;
    // throw illegalIndex if theIndex invalid
    T* element; // 1D array to hold list elements
    int arrayLength; // capacity of the 1D array
    int listSize; // number of elements in list
};

dict2 读取文件并将单词存储在 arrayList 中。我用 rrayList<char[10]> ,但是如何从文件中将这些输入到 arrayList 中呢?上面的 main() 函数中指出了错误。

主要是:

在main函数中,有如下内容。

arrayList<char[10]> *dict1 = new arrayList<char[10]> (1000);

int k = 0;
while (getline(fin, str)) {
    dict1->insert(k, str.c_str()); // error here
    k++;
}

reverseArray(dict2); // error here

编辑

我应该使用 arrayList<string>在这种情况下。

最佳答案

您的insert 函数声明为

void insert(int theIndex, const T& theElement); 

Tchar[10],因此您需要将 char[10] 传递给它,而不是 const字符*

数组既不可赋值也不可复制构造,因此如果您希望容器能够处理它们,则需要专门针对值类型为数组的情况编写代码:它们通常不能在与处理标量对象的方式相同。

关于c++ - arrayList<string> 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4867912/

相关文章:

c++ - 在第二个 vector 中查找 vector 元素

c++ - 用于访问管理的中间人对象?

python - 提取以符号开头和结尾的子字符串并在Python中替换为变量

python - 如何使用列表(或元组)作为字符串格式化值

c++ - 在不依赖位置的情况下使参数成为强制性的

c++ - C++ 中的类型转换

c++ - 比较模板类型和具体类型的实例

java - String.format/Formatter 中的错误?

Python如何根据列表中的项目从字符串中剥离字符串

python - 重复存储在变量中的字符串 x 次