Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::map insertion without creating a temporary value

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.

like image 898
nsivakr Avatar asked Jul 03 '26 10:07

nsivakr


1 Answers

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.

like image 73
Angew is no longer proud of SO Avatar answered Jul 05 '26 22:07

Angew is no longer proud of SO