-3

I have a situation where I have a map with one key and two values ex.

std::map<std::string, std::pair<double, double> > myMultiValueMap

depending on a condition I need to either update one value or the other.

I'd am looking for syntax to insert/ find the key and update either values in this map

yes I have used the insert on Maps before, but was not sure on multivalue map

std::map<std::string,double> myMap;
myMap.insert(std::make_pair("12345",0.00));

and find and update too

std::map<std::string,double>::iterator it =  myMap.find(("12345");
wadapav
  • 107
  • 5
  • 14
  • Have you even bothered trying to call the [`insert`](http://en.cppreference.com/w/cpp/container/map/insert) function in [`std::map`](http://en.cppreference.com/w/cpp/container/map)? – Captain Obvlious Jul 02 '14 at 13:21
  • Did you read on the interface of std::map and looked what functions you could use and tried already something, or should someone write the code for you? – PlasmaHH Jul 02 '14 at 13:22
  • There is no such thing as a multivalue `std::map`. The map has only one value as far as it is concerned, although that value may be a struct or class with more than one piece of data. You could also have a map of `vector`. – Neil Kirk Jul 02 '14 at 13:29

3 Answers3

1

std::map requires two template arguments, one key and one value type. But it is up to you to use arbitrary type for "value".

struct TestValue {
    int Value1;
    int Value2;
    int Value3;
    TestValue(int v1, int v2, int v3) : Value1(v1), Value2(v2), Value3(v3) {}
};

std::map<std::string, TestValue> myMap;
myMap["test"] = TestValue(1, 2, 3);
bkausbk
  • 2,740
  • 1
  • 36
  • 52
  • @CaptainObvlious I prefer the approach as it can become hard to remember what "first" and "second" mean deep in code.. – Neil Kirk Jul 02 '14 at 13:26
1

Inserting an item into the map:

myMultiValueMap[strKey] = make_pair(firstDouble, secondDouble);

Checking if an item exists:

if( myMultiValueMap.find(strKey) != myMultiValueMap.end() ) {
    // Item Exists
}

Updating an item:

myMultiValueMap[strKey].first = newFirstDouble;
myMultiValueMap[strKey].second = newSecondDouble;

You should use this for these to work

using namespace std;

Please take time and read the examples from cplusplus.com

kkanellis
  • 521
  • 1
  • 4
  • 11
  • Update better: `auto& ref = ....;dothings(ref);`. Unless only one should be changed... – Deduplicator Jul 02 '14 at 13:28
  • 3
    Please don't suggest using `using namespace std;`: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Deduplicator Jul 02 '14 at 13:31
  • I don't suggest it. It is just easier using it for small programs (like the op clearly wants) – kkanellis Jul 02 '14 at 13:35
  • @compileGuy You don't know the size of the program OP wants. Good questions narrow down problems into small programs with a very focused purpose so that readers don't have to slog through a couple KLOCs. That means that you generally can't infer the size of the program OP is actually working on from OP's question. – Eric Finn Jul 02 '14 at 13:42
0

I like bkausbk's answer and would have commented on the answer but don't have rep. Map sometimes has a problem with needing a default constructor. I know this from another SO post. I got it to work by setting default arguments (the post has other solutions)

So I changed the constructor in bkausbk's answer to:

TestValue(int v1 = 0, int v2 = 0, int v3 = 0) : Value1(v1), Value2(v2), Value3(v3) {}
billy bud
  • 103
  • 2
  • 13