用C++只打印前几行数据

xienkqul  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(91)

我目前正在做一个项目,我需要绘制烛台数据。然而,我的输出给出了所有的数据,它导致我无法滚动到我的标题。有没有什么方法可以限制我的输出范围,只打印顶部的几个数据?
这些是我的密码
MerkelMain.cpp

#include "MerkelMain.h"
#include <iostream>
#include <vector>
#include "OrderBookEntry.h"
#include "CSVReader.h"
#include "Candlestick.h"

void MerkelMain::printCandlestickData()
{
    // std::cout << "MerkelMain::printCandlestickData" << std::endl;

    std::vector<OrderBookEntry> orders = orderBook.getOrders(OrderBookType::bid, "ETH/USDT");

    std::vector<Candlestick> candles = Candlestick::calculateCandlesticks(orders);

    Candlestick::printCandlestickTable(candles);
}

字符串
Candlestick.h

class Candlestick
{
public:
    //properties
    std::string time, product, type;
    double high, low, open, close;

    Candlestick(std::string time,
                std::string product, 
                std::string type, 
                double high, 
                double low, 
                double open, 
                double close);

    void printCandlestick();
    static void printCandlestickTable(std::vector<Candlestick> candles);

    static std::vector<Candlestick> calculateCandlesticks(
        std::vector<OrderBookEntry> entries
    );

};


Candlestick.cpp

#include "Candlestick.h"
#include <iostream>
#include <map>

Candlestick::Candlestick(std::string time,
                         std::string product, 
                         std::string type, 
                         double high, 
                         double low, 
                         double open, 
                         double close) : time(time),
                                         product(product),
                                         type(type),
                                         high(high),
                                         low(low),
                                         open(open),
                                         close(close) {}

// task 1
void Candlestick::printCandlestick()
{
    // std::string time, product, type;
    // double high, low, open, close;

    std::cout << time << "\t"
              << product << "\t"
              << type << "\t"
              << high << "\t"
              << low << "\t"
              << open << "\t"
              << close << "\t"
              << std::endl;
}

void Candlestick::printCandlestickTable(std::vector<Candlestick> candles)
{
   // std::cout << "Candlestick::printCandlestickTable" << std::endl;

   std::cout << "time" 
             << "\t" 
             << "product" 
             << "\t" 
             << "type" 
             << "\t"
            << "high" 
            << "\t" 
            << "low" 
            << "\t" 
            << "open" 
            << "\t" 
            << "close"
            << std::endl;

    for (Candlestick &  candle : candles) {
        candle.printCandlestick();
    }
}


这是我的输出。我看不到我的标题/标签,无法滚动到数据输出的顶部。真的很感激一些帮助,谢谢
x1c 0d1x的数据

goqiplq2

goqiplq21#

你可以使用范围打印容器中的前 n 个项目(C++20):

using namespace std::views;
const int n = 10;
for (auto& candle : candles | take(n)) {
    /* ... */
}

字符串

相关问题