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..

Recommended Answers

All 3 Replies

struct Properties
{
  int mint;
  int maxt;
  int medt;
  Properties( int a, b, c ) : mint(a), maxt(b), medt(c) {}
};
typedef std::map < std::string, Properties > ProcessMap;

bool is_there( const ProcessMap& pmap, const std::string& pname )
{ return pmap.find(pname) != pmap.end() ; }

void insert( ProcessMap& pmap, const std::string& pname,
             int mint, int maxt, int medt )
{ pmap[pname] = Properties(mint,maxt,medt) ; }

void incr_times( ProcessMap& pmap, const std::string& pname )
{
   Properties& p = pmap[pname] ;
   ++p.mint ;
   // etc
}

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..

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[]).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.