使用C++仅打印前几行数据

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

我目前正在做一个项目,我需要绘制烛台数据。但是,我的输出给出了导致我无法向上滚动到标题的所有数据。有什么方法可以将我的输出限制在一个范围内,只打印前几个数据?

这些是我的代码

默克尔Main.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);
}

烛台.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
    );

};

烛台.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();
    }
}

这是我的输出。我看不到标题/标签,也无法滚动到数据输出的顶部。非常感谢一些帮助,谢谢

c++ std
1个回答
0
投票

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

using namespace std::views;
const int n = 10;
for (auto& candle : candles | take(n)) {
    /* ... */
}
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.