1

I have got an object which is of type vector< map < Date, Double >> (object named simulatedPrices_). I want to do a for loop reading the map inside the vector. I tried to use below:

for (j=0; j<10000; j++) {
    map <Date, Double> & pricePathJ = *simulatedPrices_[j];
    price = pricePathJ->find(targetDate)->second;        //targetDate is certain date
    cout << j << " : " << price << endl;
}

But it seems error saying no operator "*" match these operands. Any idea on why it complain this error? Is there anyway I can do better instead of the above.

The reason I used pointer was that simulatedPrices_ was quite an large object (vector containing 10000 maps, each map with 900 dates of prices)

Thanks.

Jedidja
  • 16,610
  • 17
  • 73
  • 112
Tsui John
  • 107
  • 3
  • 21
  • Well, why did you write the `*`? What do you think it does here? What was your decision-making process when you employed it? Telling us that will enable us to help you understand why you were wrong. – Lightness Races in Orbit Apr 16 '15 at 16:10

3 Answers3

3

There are no pointers here, and applying * to something other than a pointer (or a class that overloads the operator to act like a pointer) doesn't make sense.

You want

map <Date, Double> & pricePathJ = simulatedPrices_[j];

without the *, to initialise the reference to refer to a map in the vector, if that's what you want it to refer to. Then use . rather than -> to access its members.

If you want a pointer rather than a reference for some reason, use * to declare a pointer and & to take the address of the object:

map <Date, Double> * pricePathJ = &simulatedPrices_[j];
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
2

The thing to remember about references:

  1. That they are aliases to the object they reference, so they can be treated the same way you would treat the original object, so there is no need to use the -> operator to access its members, you simply use ..
  2. you can't assign a pointer (AKA the address of some object else to them). Because they are not merely holding the address. The dereference operator can't be applied to a vector, but it could be applied to a pointer to a vector as explained here.
  3. "The definition of a reference in C++ is such that it does not need to exist. It can be implemented as a new name for an existing object (similar to rename keyword in Ada)." according to wikipedia,

For these reasons and since simulatedPrices_[j] return a reference (see std::vector::operator[]), then you can just assign it to another reference, this is why map <Date, Double> & pricePathJ = simulatedPrices_[j]; works.

Community
  • 1
  • 1
InsaneBot
  • 2,422
  • 2
  • 19
  • 31
1

It's ok without the *.

map <Date, Double> & pricePathJ = simulatedPrices_[j];

You'll get a reference of the map element.

Also, you don't have to fear the "largeness" of simulatedPrices_ since it will be referenced not copied.


When you used * the compiler searched for an operator* of map<Date, Double> and threw an error.

ArnonZ
  • 3,822
  • 4
  • 32
  • 42