Solved

STL map question

Posted on 2006-11-10
6
363 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
6 Comments
 
LVL 86

Expert Comment

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

Author Comment

by:ambuli
Comment Utility
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
Comment Utility

>>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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 300 total points
Comment Utility
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
Comment Utility
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
Comment Utility
>>>> 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now