lundi 19 novembre 2018

Class member not updated by `std::find_if` iterator

I have a method that updates an element of a vector of structs in a class which uses std::find_if in the following way

void Book::processUpdate(const long order_id, const double price, const double size) {
    auto it = std::find_if(
        stack.begin(), stack.end(),
        boost::bind(&BookEntry::order_id, _1) == order_id
    );

    // Update price
    std::cout << "pre:  " << *it << std::endl;
    (*it).price = price;
    (*it).size  = size;
    std::cout << "post: " << *it << std::endl;

}

Where BookEntry is defined like so

struct BookEntry {
    long order_id;
    double price;
    double size;
}

And stack is of the type std::vector<BookEntry>

When I make the call

b.processUpdate(18940907988, 6436.6, 0.17089768)
stack = b.getStack();
auto it = std::find_if(
    stack.begin(), stack.end(),
    boost::bind(&BookEntry::order_id, _1) == 18940907988
);
std::cout << "last: " << *it << std::endl;

I would expect the following output

pre:  18940907988, 6436.7, 0.170895    // the initial state
post: 18940907988, 6436.6, 0.170898
last: 18940907988, 6436.6, 0.170898

However, I am getting

pre:  18940907988, 6436.7, 0.170895
post: 18940907988, 6436.6, 0.170898
last: 18940907988, 6436.7, 0.170895

I'm not sure what the reason is for this change not reflecting. Is it that (*it).price = price is not the way to update a pointer?

Please let me know if further clarification is required and thanks in advance!

Aucun commentaire:

Enregistrer un commentaire