# Thread: Vector of Structs Question

1. ## Vector of Structs Question

Say i have a vector of structs
Code:
```struct Crap{
int number;
int name;
}```
and in main i have this vector

Code:
` vector <Crap> ca`
Now ive called this to a function that wants to look through this vector of structs. Say i got to the 3rd vector which lets say had
a number of 10 and a name of john. How do i delete this vector.
I've seen pop_back() and erase() but i havent seen it used for a vector of structs and the syntax is diff because you have to do things like
ca.name to access specific parts of the stuct of elements.
I'm doing this for a program im writing but b/c of my code i hope this example explains what i mean.

2. You can use erase on a vector. It takes an iterator. If all you have is an index, use erase(begin() + index); to erase the proper entry.

3. Right ive tried that. This is what i tried.

Code:
`procVector.erase(procVector.begin() + j);`
procVector is my vector of course and J is the position it is in when i searched the loop.... but this is the error message i get.

[/code]
d:\Poly\CS1124\hw01\hw01\hw01.cpp(89): error C2663: 'std::vector<struct Process,class std::allocator<struct Process> >::erase' : 2 overloads have no legal conversion for 'this' pointer
[/code]

If i did erase will it erase everything for that vector like name and # cuz thats what i want...

4. It will only erase the element at the index j. So if you had ten structs in your vector, j is 4, and the one at index 4 had the name and number you want to get rid of then erase(begin() + j) will only erase that one and you will have 9 elements in your vector (everything after 4 will move down one space).

As for your error, you might want to show a little more code - the declaration of procVector, the function definition (is it a const function?), the declaration of j, etc.

5. okay i didnt want 2 cuz now i might have to explain the problem but here is my code.... the important parts.. FYI the vector fills correctly from the stream b/c i tested that.

My Struct
Code:
```struct Process //Structure will hold all process information from the procces file
{

int procId; // process ID ... 1000.. 1001 etc
string name;  // holds the name of the process
int cpuTime; // Total CPU time needed for process
int timeBurst; // Burst for each process

};```
and here is my function... i apologize if its distorted .. its hard to make it look normal here.
Code:
```void procSched(const vector<Process>& procVector)
{
// totProc is the total number of Proccess that need to run

while (!procVector.empty()) // Continue to process until vector is empty
{
for (size_t j = 0; j < procVector.size(); j++) // Loop through the vecotr
{  // doo scheduling things here.
//output the process information
cout << procVector[j].procId << ' ' << procVector[j].name << ' '
<< procVector[j].cpuTime  << ' ' << procVector[j].timeBurst << endl;

if (procVector[j].cpuTime <= 0) // If time is below 0, the process is exhausted
{

procVector.erase(procVector.begin() + j);
}
}
}
}```
Its the erase section at the end as u can c that i want out... i want to eliminate that vector incluing the time, the burst, the name etc.

6. Your procVector variable is const (you have a const reference parameter to your function). You cannot erase from a const vector.

If you want the function to be allowed to erase from the vector, just remove the const and make it a plain reference.

If I understand you correctly everything else looks fine. The wording of your question is wrong, though. You want to erase an element in the vector. You don't want to erase a vector like you've been saying.

7. Yea thx i just realized that ... i was going by what i wrote on paper first and just copying.. this is just a first rough run through but nice catch.