Answer the question
In order to leave comments, you need to log in
How to remove an element from a vector and then add some new ones?
There is this code:
vector<Query> queries;
...
channels.push_back(Query(i));
...
queries.erase(remove_if(queries.begin(), queries.end(), IsEnded), queries.end());
bool IsEnded(const Query &aVar)
{
bool ok = false;
if (aVar.id==-1)
ok = true;
return ok;
}
void servBegin(Query &que) {
cout << " #" << que.id << endl;
}
for(i=0;i<n;i++) {
if(channels[i].isFree() && queries[k-1].chn_num < L) {
channels[i].servBegin(queries[k-1]);
}
}
Answer the question
In order to leave comments, you need to log in
You may have two problems.
1. The "assign" or "move" operation is written incorrectly.
2. I forgot that in std:: vector during the operation "add" or "remove" the physical movement of the object is possible and references to it are no longer valid.
Well, IsEnded is better to write like this.
bool IsEnded(const Query &aVar) { return (aVar.id == NO_ID); }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question