944,164 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2228
  • C++ RSS
Nov 20th, 2007
0

Problem in using Map

Expand Post »
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..
Similar Threads
Reputation Points: 8
Solved Threads: 0
Light Poster
tonyaim83 is offline Offline
49 posts
since Aug 2007
Nov 20th, 2007
0

Re: Problem in using Map

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Nov 21st, 2007
0

Re: Problem in using Map

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..
Reputation Points: 8
Solved Threads: 0
Light Poster
tonyaim83 is offline Offline
49 posts
since Aug 2007
Nov 21st, 2007
0

Re: Problem in using Map

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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: check if a dll file is currently in use
Next Thread in C++ Forum Timeline: Network programing using ACE framework and C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC