在這里,我試圖對向量中的元素進行計算和比較,找到平均值、較低值、較高值和差異。我也有列印向量中元素索引的問題。
#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;
}
Lastly, I could use some reference to any article that teaches me how to perform arithmetic on values within a vector such as averages, comparisons and finding differences.
Thank you in advance!
uj5u.com熱心網友回復:
computeAveragePrice是成員函式 inOrderBookEntry但條目是 a std::vector<OrderBookEntry>,而不是 aOrderBookEntry所以它沒有這樣的成員函式。
我建議你computeAveragePrice離開OrderBookEntry這個只有一個單身資訊的班級OrderBookEntry。您可以創建另一個類來保存一個集合OrderBookEntry并將其放在那里。這個類可以是一個簡單的包裝器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';
// ^^^^^
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/336420.html
標籤:c loops class vector constructor
下一篇:使用回圈將十進制轉換為二進制
