C++ クラスの割り当てに取り組んでおり、ifstream を使用して while ループでベクトルにデータを渡すのに問題があります。
これは私がそれをやった方法であり、うまくいきますが、データファイルが100未満の整数であることに依存しています:
void Frequency()
{
ifstream frequency("test.dat");
if (! frequency)
{
cout << "**Error opening File**";
}
else
{
int data;
vector <int> numbers(101, 0);
while(frequency >> data)
{
numbers[data-1] += 1;
}
for(int i = 100; i >= 1; i--) //
{
if (numbers[i] != 0)
{
cout << setw(3) << i+1 <<": " << numbers[i] << endl;
}
}
}
}
特定の数値の頻度を降順で返します。
これは、コーディングしているというよりも、自分のやり方を打ち破っているように感じますが (私のインストラクターは「これが簡単な方法です!」と主張していますが、私は簡単ではなく、正しくしたいのです。私は次のようにしていました:
void Frequency()
{
ifstream frequency("test.dat");
if (! frequency)
{
cout << "**Error opening File**";
}
else
{
int size = 0;
int x; //actually a useless variable, only exists so the program can iterate to find the size
while (frequency >> x) //gives us the size of the data file
{
size++;
}
vector <int> numbers(size, 0);
int data;
int a = 0;
while (frequency >> data) //inputs the data into the vector
{
numbers[a] = data;
a++;
}
for (int a = 0; a < size; a++)
{
frequency >> numbers[a];
}
for(int i = 0; i < size; i++) //displays each subvector and it's value (for testing)
{
cout << "numbers[" << i << "]: " << numbers[i] << endl;
}
}
}
しかし、すべてのベクトルが 0 として返されます。データが正しく渡されない理由がわかる人はいますか?
これは参照用に渡しているデータ ファイルです。75 85 90 100
60 90 100 85 75 35 60 90 100 90 90 90 60 50 70 85 75 90 90 70
編集:いくつかのコメントされたものを修正しました。MAPでやってみます。今私を混乱させている最大のこと(私がやったように、データファイルがベクトルに渡されない理由です)