#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();
}
- The logout function to erase the pair from the vector works fine but for erasing the last element it gives a run time error.
- I want to reserve space for this vector type but not know the correct syntax to do the job
- Please answer according to c++ 98 standard
- I am very new to vector and stl so if there is something evern very trivial do mention.