使用strcpy将char数组的元素复制到另一个数组

Using strcpy to copy elements of an char array to another array

本文关键字:数组 复制 另一个 元素 char 使用 strcpy      更新时间:2023-10-16

所以我构建了一个拼写检查器,并将其作为我的TrieNode类:

class TrieNode{
public:
bool isWord;
char word[100][20];
TrieNode* letter[alphabetSize];

我在Trie类中有一个插入方法,它是:

void insert(TrieNode* root, char* wordInsert);

我能够将char*单词的字母插入到我的trie 中

这就是我的插入函数:

void Trie::insert(TrieNode* root, char* wordInsert) {
TrieNode* currentNode = root;
int wordLength = strlen(wordInsert);
for (int i = 0; i < wordLength; i++) {
//strcpy(currentNode->word[i], currentNode->letter);
int index = wordInsert[i]- 'a';
if(!currentNode->letter[index]){
currentNode->letter[index] = new TrieNode();
}
currentNode = currentNode->letter[index];
}
currentNode->isWord = true;
}

现在,我想将当前->letter[I]插入到TrieNode类中名为word 的另一个char*数组中

我试着做

strcpy(currentNode->word, currentNode->letter[i])

但我得到一个错误说:

没有用于调用"strcpy"的匹配函数

我如何才能将字母数组中的元素放入名为word的数组中,该数组也在我的TrieNode类中

首先,如果您标记了问题c++,为什么要使用char *strcpy?最好使用std::string来保存字符串,可能使用std::vectorstd::array来保存数组。此外,您可能应该考虑使用std::unique_ptr,而不是使用new分配的普通指针。

其次,TrieNode本身不是字符串。它内部有一些字符串,但您没有指定要复制这些内部字符串中的哪一个。