Problem in using Map

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2007
Posts: 49
Reputation: tonyaim83 is an unknown quantity at this point 
Solved Threads: 0
tonyaim83 tonyaim83 is offline Offline
Light Poster

Problem in using Map

 
0
  #1
Nov 20th, 2007
Hi
My problem statement is as follows :-
I have set of processes all having unique name say PROG,PROG1,PROG2 etc.
Now with each of these processes different integer values are being associated say min_time, max_time,median_time.

What i want is to create a map of all these processes and associate the respective property with each of them. So for that what i did is

/...
struct Properties
{
int MinTime;
int MaxTime;
int MedianTime;

};
typedef std::map < std::string,int,Properties > ProcessMap;
ProcessMap pmap;
.../

Now how to write a code to insert the name of the process if it is not present in the list and then add all the values to it.(Provided that all values are being taken as an input from user already). Also i need to increment or decrement the existing values of MinTime, MaxTime and MedianTime for a particular process so how to do that also..

Kindly help..
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Problem in using Map

 
0
  #2
Nov 20th, 2007
  1. struct Properties
  2. {
  3. int mint;
  4. int maxt;
  5. int medt;
  6. Properties( int a, b, c ) : mint(a), maxt(b), medt(c) {}
  7. };
  8. typedef std::map < std::string, Properties > ProcessMap;
  9.  
  10. bool is_there( const ProcessMap& pmap, const std::string& pname )
  11. { return pmap.find(pname) != pmap.end() ; }
  12.  
  13. void insert( ProcessMap& pmap, const std::string& pname,
  14. int mint, int maxt, int medt )
  15. { pmap[pname] = Properties(mint,maxt,medt) ; }
  16.  
  17. void incr_times( ProcessMap& pmap, const std::string& pname )
  18. {
  19. Properties& p = pmap[pname] ;
  20. ++p.mint ;
  21. // etc
  22. }
Last edited by vijayan121; Nov 20th, 2007 at 10:06 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 49
Reputation: tonyaim83 is an unknown quantity at this point 
Solved Threads: 0
tonyaim83 tonyaim83 is offline Offline
Light Poster

Re: Problem in using Map

 
0
  #3
Nov 21st, 2007
While using the code provided by you i'm facing one compilation problem
My code is as follows

struct Properties
{
int Min;
int Max;
Properties( int a,int b): Min(a),Max(b)
{ }
};
typedef std::map < std::string, Properties > ProcessMap;
ProcessMap pmap;

void insert( ProcessMap& pmap, const std::string& pname,int mint, int maxt)
{
pmap[pname] = Properties(mint,maxt) ;
}
On the line in Bold i m getting compilation error as "no matching function for call to `NewProperties::NewProperties()'" What can be the reason for this..
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Problem in using Map

 
0
  #4
Nov 21st, 2007
i'm sorry; it was a mistake made by me. to use the [] operator on a map, a default constructor is needed for the data.
struct Properties
{
    int Min;
    int Max;
    Properties( int a = 0, int b = 0 ): Min(a),Max(b){}
};
another way is to use the insert method of the map (instead of the operator[]).
Last edited by vijayan121; Nov 21st, 2007 at 9:50 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1669 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC