首页 > 解决方案 > 将数据推送到循环中的向量时出现问题

问题描述

这是我的txt文件,其中第一行是dataSets. 第 2 行(size1 变量)等于第 3 行(A1 数组)中的元素数,第 4 行(size2 变量)等于第 5 行(A2 数组)中的元素数。我的 readFile 函数必须覆盖上面列出的变量和数组而不覆盖dataSets变量。

我的countElementsInArray功能是计算数组中数字的出现次数A2array A1。我的程序适用于一个数据集(txt 中的 5 行),但是当涉及到例如 3 个数据集(13 行)时,我的 readFile 函数在将结果推回名为计数的向量时停止。我的问题是为什么它不正确以及如何替换它?

文本文件:

3
4
-5 -1 0 8
7
7 9 2 0 -7 2 -5
4
1 2 3 4
2
1 1
5
0 0 0 0 0
1
3

Result for above input should be:
Dataset1:
0 0 0 1 0 0 1
Dataset2:
1 1
Dataset3:
0
int main() {

    int dataSets = 0, size1 = 0, size2 = 0;
    std::fstream file;
    openFile(file);

    int *A1 = new int[size1];
    int *A2 = new int[size2];

    readFile(file, dataSets, size1, size2, A1, A2);
}

void readFile(std::fstream &file, int &dataSets, int &size1, int &size2, int *&A1, int *&A2) {

    std::vector<int> counts;

    file >> dataSets;

    for (size_t i = 0; i < dataSets; i++) {
        file >> size1;

        for (size_t j = 0; j < size1; j++)
            file >> A1[j];

        file >> size2;

        for (size_t j = 0; j < size2; j++)
            file >> A2[j];

        for (size_t j = 0; j < size2; j++) {
            int searchValue = A2[j];
            counts.push_back(countElementsInArray(A1, size1, searchValue));
        }

        int numberOfDataSet = i + 1;
        std::cout << "Dataset" << numberOfDataSet << ":" << std::endl;
        for (size_t j = 0; j < size2; j++) {
            std::cout << counts[j] << " ";
        }

        std::cout << std::endl;
    }

}



int countElementsInArray(int *A, int size, int searchValue) {

    int count = 0, first = 0, last = size;
    int i = 1;
    int j = 1;
    while (first <= last) {
        int mid = first + (last - 1) / 2;

        if (A[mid] == searchValue) {
            count++;
            do {
                if (A[mid + i] == searchValue && A[mid - j] == searchValue) {
                    count += 2;
                    i++;
                    j++;
                } else if (A[mid + i] == searchValue) {
                    count++;
                    i++;
                } else if (A[mid - j] == searchValue) {
                    count++;
                    j++;
                }

                return count;
            } while (A[mid + i] == searchValue || A[mid - j] == searchValue);
        } else if (searchValue < A[mid])
            last = mid - 1;

        else if (searchValue > A[mid])
            first = mid + 1;
    }
    return count;
}

标签: c++arraysfile

解决方案


一个突出的问题是

int *A1 = new int[size1];
int *A2 = new int[size2]; 

等于size1,本质size20你有一个大小固定的数组0,它不能保存任何值,所以这个

file >> A1[j];

还有这个

file >> A2[j];

都是未定义的行为

改变size1并只是size为了10测试它,产生了一个结果。

样本:

https://wandbox.org/permlink/ra2AE4uff74NXAXp

当前面的代码发生丑陋的崩溃时:

https://wandbox.org/permlink/tHGK8unYj0ww9KSe

我认为从这里您可以更正程序的其余部分。

更换

for (size_t j = 0; j < size2; j++) {
     std::cout << counts[j] << " ";
}

和:

for(auto& i : counts)
   std::cout << i;

将向您显示我收集的数组中的所有元素不正确,您必须调整文件中的数据收集以使大小正确,并使用正确的大小在函数内初始化 A1 和 A2。

请注意,openFile();仍然缺少定义,我假设它只是打开文件。


推荐阅读