Vector question

S

SB

I have an assignment where we are supposed to mimic a scheduler routine of
an OS. We are supposed to schedule some processes, an each process has the
following attributes...
- process id
- execution time
- memory size

All the above are int values. We have to prompt the user to input the number
of processes and then store those processes (with the 3 attributes) in a
vector. So the size of the vector is unknown until a user inputs a value for
the number of processes. My question is this...how would I store say 10
processes for example with each having the 3 attributes in parallel? What I
mean by that is, each process has an id, time and mem size, so if there are
10 processes, how would I know which execution time and mem size belonged to
which process id? Would I need to create a seperate vector for each process,
where each vector only contained one id, execution time and mem size? I'm
really hoping to do this with one vector. Can anyone help me understand how
to do this with one vector if possible?

Thanks!
 
S

SB

I forgot to add my initial idea of how I could accomplish this with one
vector for all processes... Would I have to do it under the pretense that I
know that each time I iterate through the vector (once it's already
populated with all the data for each process) that every third iteration is
one process? For example, iteration 0 I'm reading the process id for process
1, iteration 1 I'm reading the execution time for process 1, iteration 2
I'm reading the memory size for process 1 and so forth for each process
until I've gone all the way through the vector? This is only way I could see
doing it with one vector for all processes. Is there a better way?

Thanks again!
 
J

Jon Bell

I have an assignment where we are supposed to mimic a scheduler routine of
an OS. We are supposed to schedule some processes, an each process has the
following attributes...
- process id
- execution time
- memory size

All the above are int values. We have to prompt the user to input the number
of processes and then store those processes (with the 3 attributes) in a
vector. So the size of the vector is unknown until a user inputs a value for
the number of processes. My question is this...how would I store say 10
processes for example with each having the 3 attributes in parallel?

First define a struct or class data type to hold the attributes for one
process. Then create a vector of that data type. For example, using a
struct:

struct process
{
// define attributes here, as members of the struct
};

vector<process> processlist(numprocesses);

Then to get at a specific attribute for each process, refer to it as
processlist[processnumber].attributename.

You can do the same with a class instead of a struct, by making the
attributes public; or make the attributes private and define public member
functions that return the attributes.
What I
mean by that is, each process has an id, time and mem size, so if there are
10 processes, how would I know which execution time and mem size belonged to
which process id?

Using the scheme outlined above, you would have to loop through the vector
and search for the process that has the desired id, then get the
corresponding attributes from that same position in the vector. This is
the sort of thing that the standard map class is designed to do easily and
efficiently:

map<int,process> processlist;

processlist[processid].attributename = whatever;

Hers's a more complete toy example:

#include <iostream>
#include <string>
#include <map>

using namespace std;

struct process
{
string name;
int id;
};

int main ()
{
map<int,process> processlist;

processlist[29325].id = 29325;
processlist[29325].name = "Clinton";
processlist[29416].id = 29416;
processlist[29416].name = "Laurens";

// or you can declare a struct instance, fill it, and put it in the map

process newprocess;

newprocess.id = 10158;
newprocess.name = "NewYork";
processlist[newprocess.id] = newprocess;

newprocess.id = 33301;
newprocess.name = "Fort Lauderdale";
processlist[newprocess.id] = newprocess;

cout << "Name of process 29325 is " << processlist[29325].name
<< endl;

cout << "List of processes:" << endl;

// Note that if you use an iterator to traverse the map, it automatically
// comes out sorted by key!

map<int,process>::iterator it;
for (it = processlist.begin(); it != processlist.end(); ++it)
{
// the current process is at it->second, i.e. (*it).second
cout << it->second.id << "\t" << it->second.name << endl;
}

return 0;
}
 
M

marbac

SB said:
What I
mean by that is, each process has an id, time and mem size, so if there are
10 processes, how would I know which execution time and mem size belonged to
which process id? Would I need to create a seperate vector for each process,
where each vector only contained one id, execution time and mem size? I'm
really hoping to do this with one vector.

I hope that I understood your problem, but Iam not sure if I understood.

I would declare a class (or struct) process and then define an empty
vector of type process.

Example (fast hack):


#include <iostream>
#include <vector>
using namespace std;

class process {
public: // set,get and so on
process (int _id,int _t,int _mem):id(_id),t(_t),mem(_mem){};
void cout_process();
private:
int id,t,mem;
};

void process::cout_process() {
cout << "Process ID:" << this->id << endl
<< "Exec. Time:" << this->t << endl
<< "Memory:" << this->mem << endl;
}

int main () {
vector <process> container;
process test1(1,2,3),test2(4,5,6),test3(7,8,9);
container.push_back(test1);
container.push_back(test2);
container.push_back(test3);
for (int i=0;i<container.size();i++)
container.cout_process();
}
 
S

SB

Yep, I just thought of the same thing just a few minutes ago. I was going to
go with a struct, but I think I'll just a class in case I want to define
some methods. Don't know why I didn't think of that to begin with. Thanks
for help though!


Jon Bell said:
I have an assignment where we are supposed to mimic a scheduler routine of
an OS. We are supposed to schedule some processes, an each process has the
following attributes...
- process id
- execution time
- memory size

All the above are int values. We have to prompt the user to input the number
of processes and then store those processes (with the 3 attributes) in a
vector. So the size of the vector is unknown until a user inputs a value for
the number of processes. My question is this...how would I store say 10
processes for example with each having the 3 attributes in parallel?

First define a struct or class data type to hold the attributes for one
process. Then create a vector of that data type. For example, using a
struct:

struct process
{
// define attributes here, as members of the struct
};

vector<process> processlist(numprocesses);

Then to get at a specific attribute for each process, refer to it as
processlist[processnumber].attributename.

You can do the same with a class instead of a struct, by making the
attributes public; or make the attributes private and define public member
functions that return the attributes.
What I
mean by that is, each process has an id, time and mem size, so if there are
10 processes, how would I know which execution time and mem size belonged to
which process id?

Using the scheme outlined above, you would have to loop through the vector
and search for the process that has the desired id, then get the
corresponding attributes from that same position in the vector. This is
the sort of thing that the standard map class is designed to do easily and
efficiently:

map<int,process> processlist;

processlist[processid].attributename = whatever;

Hers's a more complete toy example:

#include <iostream>
#include <string>
#include <map>

using namespace std;

struct process
{
string name;
int id;
};

int main ()
{
map<int,process> processlist;

processlist[29325].id = 29325;
processlist[29325].name = "Clinton";
processlist[29416].id = 29416;
processlist[29416].name = "Laurens";

// or you can declare a struct instance, fill it, and put it in the map

process newprocess;

newprocess.id = 10158;
newprocess.name = "NewYork";
processlist[newprocess.id] = newprocess;

newprocess.id = 33301;
newprocess.name = "Fort Lauderdale";
processlist[newprocess.id] = newprocess;

cout << "Name of process 29325 is " << processlist[29325].name
<< endl;

cout << "List of processes:" << endl;

// Note that if you use an iterator to traverse the map, it automatically
// comes out sorted by key!

map<int,process>::iterator it;
for (it = processlist.begin(); it != processlist.end(); ++it)
{
// the current process is at it->second, i.e. (*it).second
cout << it->second.id << "\t" << it->second.name << endl;
}

return 0;
}
 
R

rossum

Yep, I just thought of the same thing just a few minutes ago. I was going to
go with a struct, but I think I'll just a class in case I want to define
some methods. Don't know why I didn't think of that to begin with. Thanks
for help though!

You can define methods on a struct if you want to. The only real
difference is that struct defaults to public and class defaults to
private. There is no diference on methods.

rossum
Jon Bell said:
I have an assignment where we are supposed to mimic a scheduler routine of
an OS. We are supposed to schedule some processes, an each process has the
following attributes...
- process id
- execution time
- memory size

All the above are int values. We have to prompt the user to input the number
of processes and then store those processes (with the 3 attributes) in a
vector. So the size of the vector is unknown until a user inputs a value for
the number of processes. My question is this...how would I store say 10
processes for example with each having the 3 attributes in parallel?

First define a struct or class data type to hold the attributes for one
process. Then create a vector of that data type. For example, using a
struct:

struct process
{
// define attributes here, as members of the struct
};

vector<process> processlist(numprocesses);

Then to get at a specific attribute for each process, refer to it as
processlist[processnumber].attributename.

You can do the same with a class instead of a struct, by making the
attributes public; or make the attributes private and define public member
functions that return the attributes.
What I
mean by that is, each process has an id, time and mem size, so if there are
10 processes, how would I know which execution time and mem size belonged to
which process id?

Using the scheme outlined above, you would have to loop through the vector
and search for the process that has the desired id, then get the
corresponding attributes from that same position in the vector. This is
the sort of thing that the standard map class is designed to do easily and
efficiently:

map<int,process> processlist;

processlist[processid].attributename = whatever;

Hers's a more complete toy example:

#include <iostream>
#include <string>
#include <map>

using namespace std;

struct process
{
string name;
int id;
};

int main ()
{
map<int,process> processlist;

processlist[29325].id = 29325;
processlist[29325].name = "Clinton";
processlist[29416].id = 29416;
processlist[29416].name = "Laurens";

// or you can declare a struct instance, fill it, and put it in the map

process newprocess;

newprocess.id = 10158;
newprocess.name = "NewYork";
processlist[newprocess.id] = newprocess;

newprocess.id = 33301;
newprocess.name = "Fort Lauderdale";
processlist[newprocess.id] = newprocess;

cout << "Name of process 29325 is " << processlist[29325].name
<< endl;

cout << "List of processes:" << endl;

// Note that if you use an iterator to traverse the map, it automatically
// comes out sorted by key!

map<int,process>::iterator it;
for (it = processlist.begin(); it != processlist.end(); ++it)
{
// the current process is at it->second, i.e. (*it).second
cout << it->second.id << "\t" << it->second.name << endl;
}

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top