【问题标题】:issue when pushing data to vector in loop将数据推送到循环中的向量时出现问题
【发布时间】:2020-06-22 21:10:51
【问题描述】:

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

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

txt:

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;
}

【问题讨论】:

  • minimal reproducible example帮助你会更容易。
  • @anastaciu man 这是用最简单的方式描述的
  • 这意味着代码可以复制到编译器并且可以重现您描述的问题,创建重现问题的代码示例可能并不容易,我发现这样做,很多次您都可以自己跟踪问题。
  • 删除 countElementsInArray 功能没有帮助,godbolt.org/z/pfFERP
  • 你否认自己。 “要处理的代码越多,人们发现你的问题的可能性就越小。”

标签: c++ arrays file


【解决方案1】:

一个突出的问题是

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

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

file &gt;&gt; A1[j];

还有这个

file &gt;&gt; A2[j];

都是undefined behaviour

size1size 更改为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();的定义还没有,我假设只是为了打开文件。

【讨论】:

  • 这很奇怪,因为我现在明白我的错误,但我的 IDE 显示了相同的结果,尽管我将 10 分配给 size1 和 size2
  • 你的IDE发生了什么,有错误吗?结果有问题吗?
  • 我调试了它,一切都很顺利,但是当向量试图 push_back 时它不会发生。在线编译器做得好正常吗?
  • 像这样打印向量,它仍然是未定义的行为,因为您正在访问超出范围的向量,为避免这种情况,最好使用 for each loop 我将其添加到答案中。跨度>
  • 好的,谢谢,但是当我打印它时,您能解释一下向量超出范围是什么意思吗?我没有打印超出矢量大小的任何内容。
【解决方案2】:

@anastaciu 我稍微更改了代码,现在我可以在调试器中看到程序正在将计数传递给向量计数,但我仍然无法显示它

void fread(std::fstream &file, std::vector<int> &A1, std::vector<int> &A2) {

    std::vector<int> counts;
    int dataSets, size1, size2, x, y;
    file >> dataSets;
    for (size_t i = 0; i < dataSets; i++) {
        file >> size1;

        for (size_t j = 0; j < size1; j++) {
            file >> x;
            A1.push_back(x);
        }

        file >> size2;

        for (size_t j = 0; j < size2; j++) {
            file >> y;
            A2.push_back(y);
        }

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

        int numberOfDataSet = i + 1;
        std::cout << "Dataset" << numberOfDataSet << ":" << std::endl;
        for(auto& k : counts)
            std::cout << k;
        std::cout << std::endl;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-18
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多