c++ - C vector 错误 'char(*)[]: unknown size' - vector 删除迭代器超出范围

标签 c++ c arrays pointers vector

很抱歉,我是 C 语言新手。我之前学过一点 C++,可能会混淆 C 和 C++。但我正在尝试编写一个C程序。我在 C++ 中唯一可以应用的是 vector 。这对我来说是一个练习。请告诉我我犯过的任何错误。非常感谢。

我正在开发一个程序来计算文本文件中单词的频率,并输出单词列表以及文本文件中相应的频率。程序中止并显示错误消息:

C2036: 'char(*)[]: unknown size'.

它指向 vector 文件中的以下行:

1475:   _Move_unchecked(_VIPTR(_Where) + 1, this->_Mylast(), _VIPTR(_Where));

1476:   _Destroy(this->_Mylast() - 1, this->_Mylast());

1478:   --this->_Mylast();

我能理解的是消息

line 1474: _DEBUG_ERROR("vector erase iterator outside range");

但是我不知道这个问题。我确实在寻找类似的问题,但我发现的解决方案是特定于海报代码的。请帮忙。谢谢。

#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <ctype.h>


void main(int argc, char *argv[])
{
char *str;
int wordLocation, insertLocation, wordLength;
wordLocation = insertLocation = wordLength = 0;
char *wordCheck;

std::vector<char[]> word;
std::vector<int> frequency;
std::vector<char[]> tempWord;

int tempFreq;

FILE *file;
file = fopen(argv[1], "r");

if (file)
{
    //Read file test
    /*while (fscanf(file, "%s", str)!= EOF)
    {
        printf("s%\n",str);
    }
    */

    while (fscanf(file, "%s",word.end()) != EOF) 
    {
        wordCheck = word[wordLocation];
        wordLength = strlen(wordCheck);

        for (int i = 0; i < wordLength; i++, wordCheck++)
        {
            // Check and remove punctuation
            if (ispunct(*wordCheck))
            {
                *wordCheck = ' ';
            }

            // Check uppercase and transform to lowercase
            if (*wordCheck >= 'A' && *wordCheck <= 'Z')
            {
                *wordCheck = putchar(tolower(*wordCheck));
            }
        }

        // Start to compare word in vector word
        if (int(word.size()) == 1) 
        {
            //only contain 1 input
            frequency[wordLocation] = 1;
        }
        else
        {
            for (int i = 0; i <= wordLocation; i++)
            {
                // Look for the same word 
                if (word[i] == word[wordLocation])
                {
                    frequency[i]++;
                    word.erase(word.end()-1);
                    break;
                }
                // Add new word at the end
                else if (i == wordLocation && word[i] != word[wordLocation])
                {
                    frequency[wordLocation] = 1;
                }
            }
        }
        wordLocation++;
    }
    // Word Sorting
    for (int i = 0; i <= wordLocation; i++) 
    {
        for (int k = 0; k <= wordLocation - 1; k++)
        {
            if (frequency[i] < frequency[i + 1])
            {
                //Swap element

                tempWord.insert(tempWord.begin(),word[i]);
                tempFreq = frequency[i];

                word.erase(word.begin());
                frequency.erase(frequency.begin());
                word.insert(word.begin() + i + 1, tempWord.front());
                frequency.insert(frequency.begin() + i + 1,tempFreq);

                tempWord.pop_back();
            }
        }
    }
    // After sorting, write into the file
    fclose(file);

    remove("output.txt");

    file = fopen("output.txt", "a");
    file = fopen("output.txt", "w");

    for (int i = 0; i <= wordLocation; i++)
    {
        fprintf(file, "%s %d \n", word[i], frequency[i]);
    }
    printf("Create output.txt successed.");
}
else
{
    printf("Read file fail. Please check the input path.\n");
}

}

最佳答案

C2036: 'char(*)[]: unknown size'.

这是因为

std::vector<char[]> tempWord;

这是错误的(您必须指定大小或使用指针),并且在 C++ 中使用 char 数组是个坏主意。

使用 std::stringstd::array 代替。

std::vector<std::string> tempWord;
<小时/>

void main 导致未定义的行为

入口点必须声明为

int main(void)
{
    return 0; // Optimal
}

或者使用参数

int main(int argc, char *argv[])
{
    return 0; // Optimal
}
<小时/>

此外,您正在使用未初始化的指针并尝试在那里复制数据 -> UB

char *str;
while (fscanf(file, "%s",str) != EOF)

由于您是初学者,我建议您使用自动数组。

char str[1024];
<小时/>

line 1474: _DEBUG_ERROR("vector erase iterator outside range");

您正在索引 std::vector,但它没有任何推送值 -> UB

word[wordLocation];

首先你必须将一些字符串插入容器

std::string userInput;
std::cin >> userInput;        // User will enter some string
word.push_back(userInput);    // Push that input into the vector

关于c++ - C vector 错误 'char(*)[]: unknown size' - vector 删除迭代器超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45769371/

相关文章:

c - 套接字编程(sys/socket.h)

ios - Swift 4 可变对象的可变集合?

c++ - CUDA 流和上下文

c++ - 如何混合使用 boost::bind 和 C 函数指针来实现回调

c - 在c中为字符串分配内存?

Java - 对于通用数据类型 Stack<Item>,new Stack<?>[N] 是否等同于 new Stack[N]?

java - 在二维数组中累积行

c++ - 空类和派生虚拟类的大小

php - 流程经理

c - 如何让用户在 C 中回家(平台无关的方式)?