Reputation: 550
I am storing data in 2 multimaps. The data is a struct the consists of 6 variables:
struct data {
int var
string var
string var
string symbol
int price
int var
};
Now I need to sort the data in the multimaps by symbol
. If the symbols are the same, then go by price
. In one the multimaps I go from price
high to low and vice-versa in the other. For example, In multimap1
, I have symbol_1 50
symbol_1 45
. In multimap2
, I have symbol_2 30
symbol_2 35
. Because I am sorting by two values, I used a struct as the key for the multimaps. The struct consists of the symbol name and price to sort the data.
If the following question is possible (which I don't think is), then I think, the second part of my question does not need to be answered. Is it possible to search throughout the two multimaps using only part of the key. For example, I want to be able to find all of the data with the same symbol, regardless of price, using the multimap.equal_range()
or any other method (or algorithm) to do this. The problem is that the key has two values, and I want to search with only 1 of the 2 values.
I don't think this is possible, so I created 2 more multimaps using a string as the symbol (to search for the symbol names) with data
as the values. Now I can find all the symbols with the same name. The problem I am now facing is that the data in the two multimaps are not linked. What I want is, if I modify the data in multimap1_search
(the multimap used for searching), then it also modifies the same data in multimap1
, or at least be able to modify the same data
values in both multimaps. Keep in mind that the the data in the 2 multimaps are not in the same order. Is this possible? Do you guys recommend using another data structure to this? I can only use the C++ STL. I don't have access to boost or any other libraries. Also, the main focus for this program is efficiency.
Also, in general, how do you modify values in multimaps. Do you remove it and insert another value? If you are modifying a lot of values, is there a better data structure to use? I am going to be doing a lot, inserting, searching, and modifying (and possibly removing).
Upvotes: 0
Views: 189
Reputation: 279255
If your multimap is sorted by symbol and then price, you can use multimap::lower_bound
and multimap::upper_bound
to iterate over the range of elements with a specified symbol. When searching in the map sorted from lowest price to highest, set the price to INT_MIN
when you call lower_bound
and INT_MAX
when you call upper_bound
.
Also, in general, how do you modify values in multimaps
To modify the value you can just assign to it. To modify the key you have to remove it and insert a new one.
Upvotes: 1