对矩阵进行排序后出现分段错误

问题描述 投票:0回答:1

我实现了这个程序,但出现了分段错误,我不知道为什么,有人可以解释一下吗。

问题: 如果矩阵的最大元素更大,我们称矩阵的一行比另一行更好 从第二阶的最大元素开始。创建通用函数“SortByOrderGoodness” 作为参数作为唯一参数组织一些矩阵(可以是驼背的) 作为任意类型元素的向量的向量,仅假设它们是可能的 按大小比较该函数应该转换该矩阵,以便它按以下顺序排序 行按升序排列的“优点”,即排序后,其第一行是 是前面描述的意义上的“最好”,然后是下一个“善良”顺序,依此类推 到最后一行,这应该是“最糟糕的”。如果两行具有相同的“优点”,那么 根据字典顺序,较高的顺序(i

your text
后来出现)应该排在第一位。 该函数不返回任何结果,而仅修改作为参数传递给它的矩阵。 请求的函数必须借助头库中的“sort”函数来实现 “算法”,应将适当的标准函数传递给该算法,该函数应执行为 名为“Criterion”的命名函数。此外,该标准函数不得使用任何 循环(但可以调用同一库中的其他函数)。 演示测试程序中的书面函数,其中整数矩阵的元素呈驼峰状 数字是逐行输入的,行尾标记不是数字(例如“”)。 当紧接在行首输入数字以外的内容时,输入结束。完成后 输入,程序应该按照描述对矩阵进行排序,并在之后打印其元素 完成排序。最后,打印后,程序应该要求输入一些元素 整数序列,之后测试该序列是否是使用二分搜索过程输入的 显示为先前输入的矩阵的行之一。根据测试结果,程序应该 如果搜索成功,则打印句子“The searched sequence is located in 𝑖. order (after 排序)”,其中 𝑖 是与输入序列相同的矩阵行(完成后 排序),即失败时出现“在矩阵中找不到所请求的序列”的句子 搜索。如果有多行与输入的序列相同,则应报告第一行。为了 搜索,仅使用库中带有“algorithm”标头的“lower_bound”函数(i 没有其他)。用户和程序之间的对话应如下所示: 输入元素( 表示行尾,* 在行首表示条目结束):

2 5 1 6 7 *
9 8 9 *
3 3 2 3 *
4 5 1 1 7 3 2 *
*

排序后的矩阵:

9 8 9
4 5 1 1 7 3 2
2 5 1 6 7
3 3 2 3

输入所需序列的元素(*表示行尾):

2 5 1 6 7 *

请求的序列在第3行(排序后) 这是我的代码:

#include <algorithm>
#include <iostream>
#include <vector>

// Criterion function
bool Criterion(const std::vector<int>& a, const std::vector<int>& b) {
    int max_a = *std::max_element(a.begin(), a.end());
    int max_b = *std::max_element(b.begin(), b.end());

    if (max_a != max_b)
        return max_a > max_b;
    else
        return a > b;
}

// SortByOrderGoodness function
void SortByOrderGoodness(std::vector<std::vector<int>>& matrix) {
    std::sort(matrix.begin(), matrix.end(), Criterion);
}

int main() {
    std::vector<std::vector<int>> matrix;
    std::string line;
    char ch;

    // Input matrix
    std::cout << "Enter the elements (* for the end of the line, * at the beginning of the line for the end of the entry):\n";
    while (true) {
        std::vector<int> row;
        int num;
        while (std::cin >> num) {
            row.push_back(num);
        }
        if (row.empty()) break;
        matrix.push_back(row);
        std::cin.clear(); // clear the error state
        std::cin >> ch; // read the '*'
    }

    // Sort matrix
    SortByOrderGoodness(matrix);

    // Print sorted matrix
    std::cout << "Matrix after sorting:\n";
    for (const auto& r : matrix) {
        for (const auto& n : r) {
            std::cout << n << ' ';
        }
        std::cout << '\n';
    }

    // Search for sequence
    std::cout << "Enter the elements of the required sequence (* for the end of the line): ";
    std::vector<int> sequence;
    int num;
    while (std::cin >> num) {
        sequence.push_back(num);
    }

    auto it = std::lower_bound(matrix.begin(), matrix.end(), sequence, Criterion);
    if (it != matrix.end() && *it == sequence) {
        std::cout << "The requested sequence is in the " << std::distance(matrix.begin(), it) + 1 << "th row (after sorting)\n";
    } else {
        std::cout << "The requested sequence is not found in the matrix\n";
    }

    return 0;
}

当我必须输入我需要查找的序列时,我的程序就结束了。 我无法使用 sstream :( 有什么方法可以处理这个问题吗?

c++ segmentation-fault
1个回答
0
投票

错误就在这里

    if (row.empty()) break;
    matrix.push_back(row);
    std::cin.clear();  // clear the error state
    std::cin >> ch;    // read the '*'

您没有清除

std::cin
状态,并且
'*'
在循环
std::cin
处保持在
break
状态。因此
sequence
无法被填满,为空。使用空的
Criterion()
std::lower_bound()
进一步调用
sequence
会导致分段错误。

您可能需要在

std::cin
:
 之前清除 
break

状态及其缓冲区
    if (!row.empty()) matrix.push_back(row);
    std::cin.clear();  // clear the error state
    std::cin >> ch;    // read the '*'
    if (row.empty()) break;
© www.soinside.com 2019 - 2024. All rights reserved.