Reputation: 1595
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.
Upvotes: 3
Views: 736
Reputation: 171117
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.
Upvotes: 4