C++ cout.endl() 清除缓冲区,cout.flush() 不

标签 c++ buffer cout clion endl

Problem was in IDE I am using - CLion 1.2.4 gives incorrect output inside its own output window, solved.

以下代码在处理大于 ~1000 的 vector 时提供重复输出,例如:

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

最佳答案

问题出在我使用的 IDE 中 - CLion 1.2.4 给出了错误的输出。

enter image description here

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

enter image description here

关于C++ cout.endl() 清除缓冲区,cout.flush() 不,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36833060/

相关文章:

c++ - 如何通过使用 C、C++ 在 Linux 上指定其名称以编程方式检查特定守护进程是否正在运行?

c++ - 使用 CMake 为 iOS 编译

ios - 具有输入和输出一维数据缓冲区的 Metal 计算着色器?

C 标准 I/O 与 UNIX I/O 基础

c++ - std::cout 未显示在屏幕上

c++ - 如何用time_t改变日期和时间的格式?

c++ - 交换功能的好处?

c - 最佳缓冲区大小

c++ - 使用 MS 编译器的 std::cout 非常慢

c++ - 二叉树 - 仅打印左分支 - 使用后序遍历 - C++