C矢量误差';字符(*)[]:未知大小';-范围外的向量擦除迭代器 很抱歉,我是C的新手。我以前学过C++,我可能会混淆C和C++。但是我正在尝试写一个C程序。在C++中我唯一可以应用的就是向量。这是我的练习。请告诉我我犯了什么错误。非常感谢。
我正在编写一个程序来计算文本文件中单词的频率,并在文本文件中输出单词列表和相应的频率。程序中止,并显示错误消息:C矢量误差';字符(*)[]:未知大小';-范围外的向量擦除迭代器 很抱歉,我是C的新手。我以前学过C++,我可能会混淆C和C++。但是我正在尝试写一个C程序。在C++中我唯一可以应用的就是向量。这是我的练习。请告诉我我犯了什么错误。非常感谢。,c++,c,arrays,pointers,vector,C++,C,Arrays,Pointers,Vector,我正在编写一个程序来计算文本文件中单词的频率,并在文本文件中输出单词列表和相应的频率。程序中止,并显示错误消息: C2036: 'char(*)[]: unknown size'. 它指向矢量文件中的以下行: 1475: _Move_unchecked(_VIPTR(_Where) + 1, this->_Mylast(), _VIPTR(_Where)); 1476: _Destroy(this->_Mylast() - 1, this->_Mylast());
C2036: 'char(*)[]: unknown size'.
它指向矢量文件中的以下行:
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");
}
#包括
#包括
#包括
#包括
void main(int argc,char*argv[])
{
char*str;
int-wordLocation,insertLocation,wordLength;
wordLocation=insertLocation=wordLength=0;
字符*字检查;
向量词;
向量频率;
向量时间词;
int tempFreq;
文件*文件;
file=fopen(argv[1],“r”);
如果(文件)
{
//读取文件测试
/*while(fscanf(文件“%s”,str)!=EOF)
{
printf(“s%\n”,str);
}
*/
而(fscanf(文件“%s”,word.end())!=EOF)
{
wordCheck=单词[wordLocation];
wordLength=strlen(字检查);
for(int i=0;i='A'&&*wordCheck
C2036:'字符(*)[]:未知大小'
这是因为
std::vector<char[]> tempWord;
void main
导致未定义的行为
入口点必须声明为
int main(void)
{
return 0; // Optimal
}
还是用args
int main(int argc, char *argv[])
{
return 0; // Optimal
}
此外,您正在使用未初始化的指针,并试图将数据复制到该指针
char *str;
while (fscanf(file, "%s",str) != EOF)
我建议你使用自动数组,因为你是初学者
char str[1024];
第1474行:_DEBUG_ERROR(“向量擦除迭代器超出范围”)
您正在索引一个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++是不同的语言,不能编写C程序,在C++中使用一点C++。
C.中没有<代码> STD::vector < /代码>如果你想使用<代码> STD::vector < /C> >,你必须用C++编写。如果你想用C编写,你不能使用<代码> STD::向量< /> > < /P>
<>不要使用C++编译器编译C程序,这只会增加你的混淆。将程序解压缩到最小,但仍然崩溃。这不是C,而是C++,不同的语言。Jonathan Leffler注意到栈溢出主动建议同时标记两种语言。如果你想写一个C程序,YO你的第一个错误是你写了一个C++程序,你应该删除C++标签,最好用C编译器写一个“C程序”。谢谢你的回复。很抱歉我以前是C。我以前学过C++,我可能会混淆C和C++。但是我正在写C程序。我唯一可以应用C++的是向量。这是我的练习。请告诉我我犯了什么错误。非常感谢。每个人都是初学者一次,犯了和你一样的错误:我建议您分部分执行,而不是使用ie 400行编写源代码,然后对其进行调试,首先尝试从文件中运行扫描,如果您获得了扫描,然后移动到下一个操作,依此类推on@FilipKo“ICR:呃,嗯,不是真的……-当我学会C时,没有C++……-我指的是比喻:-)事实上,代码已经成功构建,但无法运行。因此我尝试调试。我在修改中犯了一些错误,因此出现了这种情况:'(.我想我误导了练习问题:允许使用std::vector,但不允许“使用命名空间std”。如果我包含任何要使用的东西,那就不再是C程序了吗?谢谢大家。谢谢你的提醒。使用std::vector是问题的建议。如果我想在C中存储“char array”列表,你有什么建议存储它吗?但是整个“string”的大小依赖于文本文件的用户输入。我认为使用Strut,但我怀疑在某些特定的情况下,输入的可变大小可能导致程序崩溃。如果您的练习提到了使用名称空间STD <代码>,它们具体允许<代码> STD::vector < /代码>,它们可能试图教你C++,而不是C。如果有疑问,请问你。它们不允许“使用名称空间std”,除了“std::vector”看,我已经尽力解释了,但可能还不够硬。让我再尝试一下,然后再放弃。如果他们说了什么——关于允许或不允许使用<代码>使用命名空间STD< /COD>或<代码> STD::向量< /代码>,很有可能,你正在参加C++课程。如果有疑问,请问一下你的TA。谢谢。
std::string userInput;
std::cin >> userInput; // User will enter some string
word.push_back(userInput); // Push that input into the vector