gpt4 book ai didi

c++ - vector 和频率搜索

转载 作者:行者123 更新时间:2023-11-28 07:34:05 25 4
gpt4 key购买 nike

我正在创建一个程序来记录现有文件中某个数字的频率。我的 vector 名称是测试,为什么它说“测试”未定义?

这可能是我遗漏的一些小东西....

#include <ostream>
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;


int main()
{
string fileName;
int aTest;

cout << "Enter a File Name:";
cin >>fileName;

ifstream inFile (fileName.c_str());
if (! inFile)
{
cout << "!!Error in opening file 'test.dat'"<< endl;
}

while( inFile >> aTest)

vector <int> test (101, 0)

test[aTest]++;

system("pause");
return 0;
}

最佳答案

您应该在 while 循环之外定义您的 vector ,并且您应该添加适当的 {} 以使您的逻辑正确。

尝试:

 vector <int> test(101, 0); //^missing semicolon
while( inFile >> aTest) {
test[aTest]++;
}

同时,不要使用 using namespace std,这被认为是不好的做法。

此外:

  #include <ostream> //^^remove one of them, don't include unnecessary headers
#include <iostream>

关于c++ - vector 和频率搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17077450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com