对元素执行计算,并检索索引向量中元素的索引

Performing calculations on elements, and retrieving index the index of an element in a vector

我在这里尝试对向量中的元素进行计算和比较,求平均值、较低值、较高值和差值。我在打印向量中的元素索引时也遇到了问题。

#include <iostream>
#include <string>
#include <vector>

enum class OrderBookType{bid, ask};

class OrderBookEntry{
public:
    double price;
    double amount;
    std::string timestamp;
    std::string product;
    OrderBookType orderType;
    
    double computeAveragePrice(std::vector<OrderBookEntry>& entries);
    
    OrderBookEntry(double _price,
                   double _amount,
                   std::string _timestamp,
                   std::string _product,
                   OrderBookType _orderType):
    price(_price),
    amount(_amount),
    timestamp(_timestamp),
    product(_product),
    orderType(_orderType) {
    }
        
//*    Other kinds of functions I hope to implement for this program. */
//    double computeLowPrice(std::vector<OrderBookEntry>& entries);
//    double computeHighPrice(std::vector<OrderBookEntry>& entries);
//    double computePriceSpread(std::vector<OrderBookEntry>& entries);

};

double OrderBookEntry::computeAveragePrice(std::vector<OrderBookEntry> &entries) {
        return 5.5; // Just trying to test a for an output here
}

int main() {
    // Declaring vector entries
    std::vector<OrderBookEntry> entries;
        
    // Adding entries into entries
    entries.push_back(OrderBookEntry{
        10000,
        0.001,
        "2020/03/17 17:01:24.88492",
        "BTC/USDT",
        OrderBookType::bid
    });
    entries.push_back(OrderBookEntry{
        20000,
        0.002,
        "2020/03/17 17:01:24.88492",
        "BTC/USDT",
        OrderBookType::bid
    });

    // Prints prices of all entries
    for (OrderBookEntry& entry : entries) {
        std::cout << "The price entry " << &entry << " is " << entry.price << std::endl;
    }
    
    std::cout << "The lower price is " << entries.computeAveragePrice() << std::endl;
        
    return 0;
}

我遇到了三个问题。

首先,我无法调用 computeAveragePrice() 函数来获取它返回的值——在这种情况下,只有 5.5 的值用于测试。

上图是下面这行代码的错误信息。

std::cout << "The lower price is " << entries.computeAveragePrice() << std::endl;

其次,当我尝试打印向量中元素的索引时,打印了一个奇怪的值。

以上是我得到的输出。我期待获得元素的相应索引,但得到了这个。这是我用来遍历向量的代码。

for (OrderBookEntry& entry : entries) {
    std::cout << "The price entry " << &entry << " is " << entry.price << std::endl;
}

最后,我可以参考任何教我如何对向量中的值执行算术运算的文章,例如求平均值、比较和求差。

提前致谢!

computeAveragePriceOrderBookEntry 中的成员函数,但 entries 是 std::vector<OrderBookEntry>,而不是 OrderBookEntry,因此它没有这样的成员函数。

我建议您将 computeAveragePrice 移出 class OrderBookEntry,它只包含一个 OrderBookEntry 的信息。您可以创建另一个 class 来保存 OrderBookEntry 的集合并将其放在那里。这个 class 可以是 std::vector<OrderBookEntry>:

的简单包装

示例:

class OrderBook {   // A class to store a collection of OrderBookEntry:s
public:
    // A function to add an `OrderBookEntry`. You'll have to make this in
    // in the way you want. This is just an example:

    template<class... Args>
    decltype(auto) add(Args&&... args) {
        return entries.emplace_back(std::forward<Args>(args)...);
    }
    
    double computeAveragePrice() const;              // move it here

    // some member functions that just call the vector's member functions:
    size_t size() const { return entries.size(); }

    OrderBookEntry& operator[](size_t idx) { return entries[idx]; }
    const OrderBookEntry& operator[](size_t idx) const { return entries[idx]; }

    auto cbegin() const { return entries.cbegin(); }
    auto cend() const { return entries.cend(); }
    auto begin() const { return entries.cbegin(); }
    auto end() const { return entries.cend(); }    
    auto begin() { return entries.begin(); }
    auto end() { return entries.end(); }

private:
    std::vector<OrderBookEntry> entries;    // put the vector in here
};

double OrderBook::computeAveragePrice() const {
    // you need to #include <numeric> to use the accumulate function:
    if(entries.empty()) return NAN;   // not-a-number
    return std::accumulate(entries.begin(), entries.end(), 0.,
                           [](double tot, const OrderBookEntry& e ) {
                               return tot + e.price;
                           }) / entries.size();
}

这样,您的 main 将如下所示:

int main() {
    OrderBook entries;            // use the new collection class
        
    // Adding entries into entries
    entries.add(                  // using the add() member function
        10000,
        0.001,
        "2020/03/17 17:01:24.88492",
        "BTC/USDT",
        OrderBookType::bid
    );

    entries.add(
        20000,
        0.002,
        "2020/03/17 17:01:24.88492",
        "BTC/USDT",
        OrderBookType::bid
    );

    // Prints prices of all entries
    for (OrderBookEntry& entry : entries) {
        std::cout << "The price entry " << &entry << " is " << entry.price << '\n';
    }
    
    std::cout << "The average price is " << entries.computeAveragePrice() << '\n';
        
    return 0;
}

请注意,打印 &entry 将打印 OrderBookEntry 的地址。如果你想打印一些有意义的东西,你需要为 operator<< 添加一个重载,它需要一个 OrderBookEntry 作为输入。

示例:

std::ostream& operator<<(std::ostream& os, const OrderBookEntry& e) {
    return os << e.product;
}

然后您可以打印该条目,但不能通过获取其地址来打印:

for (const auto& entry : entries) {
    std::cout << "The price entry " << entry << " is " << entry.price << '\n';
    //                                 ^^^^^
}