vector/list of structs
Dec 12, 2010 at 4:21pm UTC
Hello,
I'm trying to create vector of structs and I'm having problems updating it. Let me give you an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
struct process{
string pid;
string type;
int duration;
int timer;
void some_fun (int p){ p++;}
}pr;
int main(){
vector <process> v;
//how to push_back some values to struct variables on the top?
}//end of main
Thanks in advance.
Dec 12, 2010 at 6:36pm UTC
Hi,
I hope you can help..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
typedef int DATATYPE;
struct VECTOR {
DATATYPE *pArray;
size_t count;
size_t capacity;
}
VECTOR *CreateVector(size_t capacity)
{
VECTOR *hVector;
if ((hVector = (HVECTOR) malloc(sizeof (VECTOR))) == NULL)
return NULL;
hVector->capacity = capacity ? capacity : DEFAULT_CAPACITY;
if ((hVector->pArray = (DATATYPE *) malloc(sizeof (DATATYPE) * hVector->capacity)) == NULL)
{
free(hVector);
return NULL;
}
hVector->count = 0;
return hVector;
}
Dec 12, 2010 at 6:43pm UTC
Thank you firix,
I found some way of doing it.
1 2 3 4 5 6 7 8 9 10 11
int main(){
vector <process> v;
string p= "example" ;
v.push_back(proc);
v[0].pid = p;
cout<< "results: " << v[0].pid << endl;
Thank you very much.
Dec 12, 2010 at 6:44pm UTC
ok..
Dec 12, 2010 at 6:44pm UTC
v.push_back(pr);
instead of
v.push_back(proc);
Dec 12, 2010 at 7:03pm UTC
good luck....
Dec 12, 2010 at 7:04pm UTC
thanks, you too.
Topic archived. No new replies allowed.