【问题标题】:C++ cout.endl() clear the buffer, cout.flush() dontC++ cout.endl() 清除缓冲区,cout.flush() 不清除
【发布时间】:2016-08-18 09:36:54
【问题描述】:

问题出在我正在使用的 IDE 中 - CLion 1.2.4 输出不正确 在它自己的输出窗口内,解决了。

以下代码在使用大于 ~1000 的向量时给出重复输出,例如:

bubblebubble0.265596bubble0.2655960.171889bubble0.2655960.1718890.265644 shell000

如果我在每次输出后调用 endl,一切似乎都很好:

气泡
0
0.015626
0.015628
贝壳
0
0
0

但是当我尝试使用 .flush() 清除缓冲区时,我得到了相同的重复输出。

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

using namespace std;

vector<int> type_sort(string type, vector<int>input) {
    int n = input.size();

    if (type == "bubble") {
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n - 1; j++)
                if(input[j] > input[j+1]) {
                    swap(input[j], input[j+1]);
                }
    }
    else if (type == "shell") {
        int gap, i, j, temp;
        for (gap = n/2; gap > 0; gap /= 2)
            for (i = gap; i < n; i++)
                for (j = i - gap; j >= 0 && input[j] > input[j + gap]; j -= gap) {
                    temp = input[j];
                    input[j] = input[j + gap];
                    input[j + gap] = temp;
                }
    }
    return input;
}

void print_vector(vector<int> input) {
    for(int i = 0; i < input.size(); i+= input.size() / 5) {
        cout << input[i] << ",..";
    }
    cout << endl;
}

vector<int> random_vector(int size) {
    vector<int> output;
    for(int i = 0; i < size; i++) {
        output.push_back(rand() % 20 + 1);
    }
    return output;
}

int main() {
    vector<int> numbers = random_vector(5000),
                sorted_numbers,
                reversed_numbers;
    sorted_numbers = reversed_numbers = numbers;

    sort(sorted_numbers.begin(), sorted_numbers.end());
    sort(reversed_numbers.begin(), reversed_numbers.end(), greater<int>());

    vector<vector<int>> sort_types = {numbers, sorted_numbers, reversed_numbers};
    vector<string> sort_names = {"bubble", "shell"};

    chrono::time_point<chrono::system_clock> start_time, end_time;

    for (int i = 0; i < 2; i++) {
        cout << sort_names[i];
        for (int j = 0; j < 3; j++) {
            start_time = chrono::system_clock::now();
            type_sort(sort_names[i], sort_types[j]);
            end_time = chrono::system_clock::now();

            chrono::duration<double> elapsed_seconds = end_time - start_time;
            cout << elapsed_seconds.count();
        }
        cout << endl;
    }
    return 0;
}

【问题讨论】:

  • 您在发布的代码中在哪里使用了flush()
  • "big then ~1000" - 输出以这种方式依赖于向量大小暗示一些内存损坏 - 我假设如果你注释掉对 @ 的调用987654323@它工作正常吗?如果是这样,请尝试将vector 访问从[x] 更改为.at(x),并在main() 中添加try/catch (const std::exception&amp; e) { std::cerr &lt;&lt; "caught " &lt;&lt; e.what() &lt;&lt; '\n'; } 块。
  • bkVnet,我什至尝试在内部循环的每次迭代中使用 flush(),仍然无法正常工作。托尼,谢谢你的回复,我试试看!
  • 仍然提供重复输出并且没有捕获任何错误(我已将访问方法替换为 .at() 并在循环内添加了 try/catch 块)。顺便说一句,你是对的,问题在 sort_type 函数内部。

标签: c++ buffer cout clion endl


【解决方案1】:

问题出在我使用的 IDE 中 - CLion 1.2.4 输出不正确。

但是,当我直接在资源管理器 (cmd) 中运行 .exe 时,错误消失了。

【讨论】:

  • 哇——太疯狂了。好发现!可能值得在问题是由该 IDE 引起的问题上添加更新,以便其他人立即知道此问答是否可以帮助他们。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-25
  • 1970-01-01
  • 2017-01-26
  • 2012-10-12
相关资源
最近更新 更多