3

I've a map that stores, string and a set of doubles as follows.

typedef std::map<std::string, std::set<double> > exprType;
typedef std::map<std::string, std::set<double> >::iterator exprIter;

exprType exPricesData;

std::vector<double> lastprice;
std::string exchange;

I need a way to insert prices for a given key and wrote the following piece of code.

  std::set<double> prices;
  double temp =  lastprice[0]; // An array of values comes as a parameter
  prices.insert(temp); // Creating a temp. set

  std::pair<exprIter,bool> locIter = exPricesData.insert(
            std::pair<std::string, std::set<double> >(exchange, prices));

   for ( int i = 1; i < lastprice.size() ; ++i )
   {
        locIter.first->second.insert(lastprice[i]);
   }

I'm wondering, is there a way to improve especially, the first part, which creates a temp set.

nsivakr
  • 1,565
  • 2
  • 25
  • 46
  • 1
    I removed my answer because I misread your code. You can insert an empty set `std::pair locIter = exPricesData.insert(std::pair >(exchange, std::set()));` – stefaanv Apr 17 '13 at 17:34

1 Answers1

4

Do you mean something like this?

std::set<double> &prices = exPricesData[exchange];  //returns existing value, or inserts a new one if key is not yet in map

prices.insert(lastprice.begin(), lastprice.end());

Oh, and be careful when using a std::set<double>. If the numbers are the result of computation, numerical inaccuracies can result in distinct members of the set where you don't expect them.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455