#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
vector < pair <string,int> > ivec;
//ivec.reserve((pair <string,int>)(10);
void logout(int id)
{
vector < pair<string,int> > :: iterator iter=ivec.begin();
while(iter!=ivec.end())
{
if(iter->second==id){
ivec.erase(iter);
}
++iter;
}
}
void login(char str[],int id)
{
pair<string,int> temp;
temp.first=(string)str,temp.second=id;
ivec.push_back(temp);
}
int main(void)
{
int ch;
pair<string,int> temp;
temp.first="server",temp.second=0;
ivec.push_back(make_pair("server",0));
while(true)
{
char name[10];
int id;
cin>>name;
cin>>id;
cout<<"login"<<endl;
cin>>ch;
if(ch==1)
{
login(name,id);
}
else
{
break;
}
}
cout<<ivec.size();
logout(6);
cout<<ivec.size();
}
erasing from a vector invalidates the iterator, but erase returns a correct one too.
Do this:
if(iter->second==id){
iter = ivec.erase(iter);
}
else
++iter;
And for the other question.
vector has a reserve function which reserves space. it does not change the size, just the capacity.
vector<pair<string, int> > ivec;
int main()
{
ivec.reserve(100);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With