Solved

STL map question

Posted on 2006-11-10
6
366 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
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 and create keystrokes 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++.

810 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