vector/list of structs

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.
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;
}
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.
ok..
v.push_back(pr);
instead of
v.push_back(proc);
good luck....
thanks, you too.
Topic archived. No new replies allowed.