auto c++

auto is not allowed here in C++11, C++17. I upgraded C++ language but I still get auto is not allowed here. How do I improve my code?

1
2
3
4
5
6
  void OrderBook::withdrawOrder(const std::string& time)
{
    orders.erase(std::remove(orders.begin(), orders.end(),
                             [&time](auto order){ return order.timestamp == time && order.username == "simuser"; }),
                 orders.end());
}.
Last edited on
What compiler are you using? Have you told the compiler to use C++17?
I am using c++11 and c++17. still the same error.
How are you using C++17?
Just saying you are isn't enough, show some evidence.

What IDE are you using (Visual Studio, Code::Blocks, something else?)
What Compiler are you using (M$, GNU, something else?)

You normally have to tell something that you want C++17 (tick a box in an IDE somewhere). You can't just install a recent compile and leave it at that.

I am using Visual Studio g++ --std=c++17 but I want working on c++11 because the other classes depend on c++11. what is alternative to auto ? thanks
Just give the actual type of variable that is in the container "orders".

You haven't given enough code (here) to identify that. (Your other thread suggests it's an OrderBookEntry).

"because the other classes depend on c++11" is a nonsense reason for not using a more up-to-date standard if it is available.
Last edited on
What is posted is kinda hard to read but I believe you're attempting to use remove_if(). I don't believe that the regular remove function allows the use of a unary predicate. That may be at least part of your issue.
As markyrocks stated using lambda requires remove_if(...). The next thing is that you pass order by copy (not all classes allow copy). The best is in many if not most cases to pass by const references.

When you pass the concrete type and avoid auto you can avoid any kind of surprise...
I add more code here


#include <map>
#include <algorithm>
#include <iostream>
#include "OrderBook.h"
#include "CSVReader.h"


OrderBook.cpp processes bids and offers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
{
    orders = CSVReader::readCSV(filename);
}

/** return vector of all known products in the dataset*/
std::vector<std::string> OrderBook::getKnownProducts()
{
    std::vector<std::string> products;

    std::map<std::string,bool> prodMap;

    for (OrderBookEntry& e : orders)
    {
        prodMap[e.product] = true;
    }
    
    // now flatten the map to a vector of strings
    for (auto const& e : prodMap)
    {
        products.push_back(e.first);
    }

    return products;
}
/** return vector of Orders according to the sent filters*/
std::vector<OrderBookEntry> OrderBook::getOrders(OrderBookType type, 
                                                 std::string product, 
                                                 std::string timestamp)
{
    std::vector<OrderBookEntry> orders_sub;
    for (OrderBookEntry& e : orders)
    {
        if (e.orderType == type && 
            e.product == product && 
            e.timestamp == timestamp )
            {
                orders_sub.push_back(e);
            }
    }
    return orders_sub;
}

void OrderBook::insertOrder(OrderBookEntry& order)
{
    orders.push_back(order);
    std::sort(orders.begin(), orders.end(), OrderBookEntry::compareByTimestamp);
}

/* R2D: Using the live order book from the exchange, decide if it should withdraw its bids at any point in time */
/* R3D: Using the live order book from the exchange, decide if it should withdraw its offers at any point in time */
void OrderBook::withdrawOrder(std::string time)
{
    for (std::size_t i = orders.size() - 1; i < orders.size(); --i)
    {
        if(orders[i].timestamp == time && orders[i].username == "simuser")
        { 
            orders.erase(orders.begin() + i);
        }
    }
}

std::vector<OrderBookEntry> OrderBook::matchAsksToBids(std::string product, std::string timestamp)
{
// asks = orderbook.asks
    std::vector<OrderBookEntry> asks = getOrders(OrderBookType::ask, 
                                                 product, 
                                                 timestamp);
// bids = orderbook.bids
    std::vector<OrderBookEntry> bids = getOrders(OrderBookType::bid, 
                                                 product, 
                                                 timestamp);

    // sales = []
    std::vector<OrderBookEntry> sales; 

    // I put in a little check to ensure we have bids and asks
    // to process.
    if (asks.size() == 0 || bids.size() == 0)
    {
        std::cout << " OrderBook::matchAsksToBids no bids or asks" << std::endl;
        return sales;
    }

    // sort asks lowest first
    std::sort(asks.begin(), asks.end(), OrderBookEntry::compareByPriceAsc);
    // sort bids highest first
    std::sort(bids.begin(), bids.end(), OrderBookEntry::compareByPriceDesc);
    // for ask in asks:
    std::cout << "max ask " << asks[asks.size()-1].price << std::endl;
    std::cout << "min ask " << asks[0].price << std::endl;
    std::cout << "max bid " << bids[0].price << std::endl;
    std::cout << "min bid " << bids[bids.size()-1].price << std::endl;
    
    for (OrderBookEntry& ask : asks)
    {
    //     for bid in bids:
        for (OrderBookEntry& bid : bids)
        {
    //         if bid.price >= ask.price # we have a match
            if (bid.price >= ask.price)
            {
    //             sale = new order()
    //             sale.price = ask.price
            OrderBookEntry sale{ask.price, 0, timestamp, 
                product, 
                OrderBookType::asksale};

                if (bid.username == "simuser")
                {
                    sale.username = "simuser";
                    sale.orderType = OrderBookType::bidsale;
                }
                if (ask.username == "simuser")
                {
                    sale.username = "simuser";
                    sale.orderType =  OrderBookType::asksale;
                }
            
    //             # now work out how much was sold and 
    //             # create new bids and asks covering 
    //             # anything that was not sold
    //             if bid.amount == ask.amount: # bid completely clears ask
                if (bid.amount == ask.amount)
                {
    //                 sale.amount = ask.amount
                    sale.amount = ask.amount;
    //                 sales.append(sale)
                    sales.push_back(sale);
    //                 bid.amount = 0 # make sure the bid is not processed again
                    bid.amount = 0;
    //                 # can do no more with this ask
    //                 # go onto the next ask
    //                 break
                    break;
                }
    //           if bid.amount > ask.amount:  # ask is completely gone slice the bid
                if (bid.amount > ask.amount)
                {
    //                 sale.amount = ask.amount
                    sale.amount = ask.amount;
    //                 sales.append(sale)
                    sales.push_back(sale);
    //                 # we adjust the bid in place
    //                 # so it can be used to process the next ask
    //                 bid.amount = bid.amount - ask.amount
                    bid.amount =  bid.amount - ask.amount;
    //                 # ask is completely gone, so go to next ask                
    //                 break
                    break;
                }


    //             if bid.amount < ask.amount # bid is completely gone, slice the ask
                if (bid.amount < ask.amount && 
                   bid.amount > 0)
                {
    //                 sale.amount = bid.amount
                    sale.amount = bid.amount;
    //                 sales.append(sale)
                    sales.push_back(sale);
    //                 # update the ask
    //                 # and allow further bids to process the remaining amount
    //                 ask.amount = ask.amount - bid.amount
                    ask.amount = ask.amount - bid.amount;
    //                 bid.amount = 0 # make sure the bid is not processed again
                    bid.amount = 0;
    //                 # some ask remains so go to the next bid
    //                 continue
                    continue;
                }
            }
        }
    }
    return sales;             
}/
Last edited on
Please code code tags!
getKnownProducts)() can be simplified using a std::set - as you are only making sure the product is unique (not tried):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::vector<std::string> OrderBook::getKnownProducts()
{
	std::set<std::string> prodSet;

	for (const OrderBookEntry& e : orders)
		prodSet.insert(e.product);

	std::vector<std::string> products;
	products.reserve(prodSet.size());

	// now flatten the set to a vector of strings
	for (const auto& e : prodSet)
		products.push_back(e);

	return products;
}


for getOrders(), the parameters should be passed by const ref and not by value to avoid unnecessary copies.

insertOrder(). shouldn't order be passed by const ref?

withdrawOrder(). Pass time by const ref. Also, to erase the orders, first use std::remove_if(), and then erase. Something like (not tried):

1
2
3
4
5
6
void OrderBook::withdrawOrder(const std::string& time)
{
	const auto del {std::remove_if(orders.begin(), orders.end(), [&](const auto& ord) {return ord.timestamp == time && ord.username == "simuser"; })};

	orders.erase(del, orders.end());
}


getOrders() also could be (not tried):

1
2
3
4
5
6
7
std::vector<OrderBookEntry> OrderBook::getOrders(const OrderBookType& type, const std::string& product, const std::string& timestamp)
{
	std::vector<OrderBookEntry> orders_sub;

	std::copy_if(orders.begin(), orders.end(), std::back_inserter(orders_sub), [&](const auto& ord) { return ord.orderType == type && ord.product == product && ord.timestamp == timestamp; });
	return orders_sub;
}

Last edited on
@seeplus thanks.
Topic archived. No new replies allowed.