Solved

STL map question

Posted on 2006-11-10
6
373 Views
Last Modified: 2013-12-14
Hi Experts,

I need to store some objects in a map.  Then if 'needed' I have to modify the objects in the map and put them back.  

Later, I am given an object which I have to compare with the objects in the map and see if it has been modified.

Can I modify the object and put it back.  Or I need to remove the object to be modified and then put the modified object back to map?  


The other question is how can I check if it is modified without doing a memberwise comparision?

typedef map<string, Test> myMap_t;


class MyMapClass{

public:
void insertToMap(string key, Test test);
bool isModifed(Test newTest);
private:
myMap_t m_myMap;
};


void MyMapClass::insertToMap( string key, Test test)
{
 m_myMap.insert(myMap_t::value_type(std::string(key), test));
}

void MyMapClass::modify(string key )
{
    // if we have an object for the key modify...
}

bool MyMapClass::isModifed(Test newTest)
{
      //return true if it is modified else false
}
0
Comment
Question by:ambuli
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 17915293
Why not just storing a 'bool m_bIsModified' member in 'Test'? That would make the whole thing quite simple.
0
 

Author Comment

by:ambuli
ID: 17915448
Hi JKR,

I was just giving a simplified version of the whole thing.  The problem is that there are two different paths that the Test objects be modified.  One is under my control and the other is not.  What happens is that a trigger is send to me from a database whenever the Test object is modified whether by my component or by the other path.  

So, I want to cashe the initial objects in the map, then when I modify the database I will also modify the object in the map.  So, if a trigger comes in I can check the objects in the map to see if it is due to the other path.

Thanks,
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 17919941

>>Can I modify the object and put it back.  Or I need to remove the object to be modified and then put the modified object back to map?  

You can modify the object and put it back.No need to remove the object to be modifed and put the modified object back.

Second point,
Make 2 cache,

(key,Value),
(keystringpath1,test object)  //one which is in your control
(test object,Keystringpath2) //one which is not n your control

Will it help ! if my understanding is correct.
Best Regards,
DeepuAbrahamK
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 300 total points
ID: 17920531
As long as you are not changing the key, you can modify the Test object directly. References are great for this.

Here's an illustration...
--------8<--------
#include <iostream>
#include <string>
#include <map>
using namespace std;

struct Test {
      string a,b;
};

ostream& operator<<(ostream& os,const Test& test) {
      return os << "a=" << test.a << ", b=" << test.b;
}

typedef map<string,Test> TestMap;

void display(const TestMap& testMap) {
      cout << "display:\n";
      for (TestMap::const_iterator i = testMap.begin();i != testMap.end();++i)
            cout << "testMap[\"" << i->first << "\"] = " << i->second << '\n';
      cout << '\n';
}

int main()
{
      Test test_one = {"good","indifferent"};
      Test test_two = {"bad","terrible"};

      TestMap testMap;
      testMap["one"] = test_one;
      testMap["two"] = test_two;

      display(testMap);

      // Altering a test in the map, using a reference
      Test& test = testMap["one"];
      test.b = "splendid";

      display(testMap);
}
--------8<--------
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 17920625
Way off topic, with apologies...

I'm often wondering how far to go with ostream& operator<<. I could have written that illustration as follows below, but it feels funny defining insertion operators for classes created from STL templates. Why? Because the template classes like map<string,Test> "come for free" and you have to roll your own ostream operator<< for them, if that's what you want to do. Why don't the template classes provide their own operator<<? ... Well I guess one size doesn't fit all, and delimiting the display of container entries with newlines will not be appropriate for all projects. When you define an insertion operator, you suppose that you know how the client wants to display your class and you pollute the global namespace with what you reckon is right.

--------8<--------
#include <iostream>
#include <string>
#include <map>
#include <algorithm>
#include <iterator>
using namespace std;

struct Test {
      string a,b;
};
typedef map<string,Test> TestMap;

ostream& operator<<(ostream& os,const Test& test) {
        return os << "a=" << test.a << ", b=" << test.b;
}

ostream& operator<<(ostream& os,TestMap::value_type v) {
      return os << "testMap[\"" << v.first << "\"] = " << v.second;
}

ostream& operator<<(ostream& os,const TestMap& testMap) {
      copy(testMap.begin(),testMap.end(),ostream_iterator<TestMap::value_type>(os,"\n"));
      return os;
}

int main()
{
      Test test_one = {"good","indifferent"};
      Test test_two = {"bad","terrible"};

      TestMap testMap;
      testMap["one"] = test_one;
      testMap["two"] = test_two;

      cout << testMap << '\n';

      // Altering a test in the map, using a reference
      Test& test = testMap["one"];
      test.b = "splendid";

      cout << testMap << '\n';
}
--------8<--------

It is nice getting rid of functions like display, though, isn't it?
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 200 total points
ID: 17929581
>>>> Can I modify the object and put it back.
>>>> References are great for this.

You simply can do:

   mymap[key] = data;

That works whether there is an existing (old) map element or not, cause mymap::operator[] creates a new data element for the given key if the element doesn't exist  and returns a reference to it. If the element (key) exists it returns a reference to the existing data object. In both cases you can assign it to a temporary reference variable as rstaveley explained above.

   mymap[key] = data;

Before using that statement you should set the proper contents to 'data' so that you don't need to make further calls to the object stored in the map. Doing so has the advantage over operating with a local reference that the object, where mymap[key] is a reference of, is complete if data is complete. That is cause mymap[key] might be uninitialized if there is no valid default constructor.

Regards, Alex

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Eclipse Neon and jdk 1.8.0 11 320
What is the Best Editor for PHP Development ? 5 88
designing in object programming 12 104
why doesn't assert terminate here? 2 9
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question