-
erase in vector
<code>
vector<myrec> v
class myrec
{
int age;
char* name;
};
void sdelete()
{
if(v.size()>1)
system("CLS");
v.erase(v.begin());
}
void edelete()
{
if(v.size()>1)
system("CLS");
v.erase(v.end()-1);
}
</code>
When I use edelete() it works fine but when I use sdelete()
the last name element of the last element of the vector changes.
What did I do wrong here?
-
What do you mean 'the last element of the vector changes'?
Could you post all of your code?
Also, the if(size>1) applies only to the system("cls") command (why is that there anyway). I assume it is a check before the erase? In that case put braces around the statement, otherwise if only applies to the single statement following it.
Finally, the correct tag to post code is [code]
-
1 Attachment(s)
erase in vector
-
I am taking a look at your code, but I am finding other weird things as I look through it. Because I cannot see any drivers in your zip, I cannot say if these are causing your bugs. However, I am going down the bugs as I find them.
Code:
myrec(int age,char* name)
{
this->name=new char[strlen(name)+1];
this->age=age;
strcpy(this->name,name);
}
~myrec()
{
delete name;
}
Your destructor needs to be changed to:
Code:
~myrec()
{
delete [] name;
}
-
This is not so much as a bug than as being inefficient. Change this to:
Code:
system("CLS");
int ch;
while (ch!=10)
{
system("CLS");
...
}
Code:
int ch;
do {
system("CLS");
...
} while (ch!=10)
-
Why not use the string class?
-
erase in vector
Thanks
I have changed the program but still I am getting the same problem.
-
erase in vector
-
try
v.erase(v.end()-1, v.end());
-
Code:
void sdelete()
{
if (v.size()>1)
{
system("CLS");
v.erase(v.begin());
}
}
In this case it deletes all but the very first element. If you want it to delete all elements then you need to check that its higher than 0 not 1.
In thecode you have above as soon as the list is empty you are telling it to erase nothing which could cause an error ;)
-
erase in vector
Thanks
I have not handled the exception there yet.
if I insert four record and I have deleted the first or second record. it delets ok but the string stored after that record changes to strange symbols.
-
Can you post the project again and an example of what you are doing?
-
//myrec.h
class myrec
{
public:
myrec()
{
}
myrec(int age,char* name)
{
this->name=new char[strlen(name)+1];
this->age=age;
strcpy(this->name,name);
}
myrec(const myrec& myreccopy)
{
this->name=new char[strlen(myreccopy.name)+1];
this->age=myreccopy.age;
strcpy(this->name,myreccopy.name);
}
myrec(myrec *myreccopy)
{
this->name=new char[strlen(myreccopy->name)+1];
this->age=myreccopy->age;
strcpy(this->name,myreccopy->name);
}
~myrec()
{
delete [] name;
}
int GetAge()
{
return age;
}
char *GetName()
{
return name;
}
private:
int age;
char* name;
} ;
//MAIN.CPP
#include <string.h>
#include <vector>
#include <iostream>
#include <conio.h>
#include "myrec.h"
using namespace std;
void adisp();
void disp();
void sinsert();
void einsert();
void ainsert();
void sdelete();
void edelete();
void adelete();
void search();
vector<myrec> v;
int main(int argc, char **argv)
{
system("CLS");
int ch;
do
{
system("CLS");
cout<<"1....show at given pos."<<endl;
cout<<"2....show rec."<<endl;
cout<<"3....insert at start"<<endl;
cout<<"4....insert at given position"<<endl;
cout<<"5....insert at end"<<endl;
cout<<"6....delete at start"<<endl;
cout<<"7....delete at end"<<endl;
cout<<"8....delete at given position"<<endl;
cout<<"9....search"<<endl;
cout<<"10...Exit"<<endl;
cout<<"ENTER A CHOICE..?:"<<endl;
cin>>ch;
switch (ch)
{
case 1:
adisp();
getch();
break;
case 2:
//cin.get();
disp();
getch();
break;
case 3:
sinsert();
break;
case 4:
ainsert();
break;
case 5:
einsert();
break;
case 6:
sdelete();
break;
case 7:
edelete();
break;
case 8:
adelete();
getch();
break;
case 9:
search();
break;
default:
cout<<"dfdsf";
return 0;
}
}while (ch!=10);
return 0;
}
void adisp()
{
if (v.size()<1)
cout<<"No record in vector"<<endl;
else
{
system("CLS");
int i;
cout<<"Entered the number Which you want to see"<<endl;
cin>>i;
system("CLS");
cout<<v[i-1].GetAge()<<" "<<v[i-1].GetName()<<endl;
}
}
void disp()
{
system("CLS");
vector<myrec>::iterator vi=v.begin();
for (;vi<v.end();vi++)
cout<<(*vi).GetAge()<<" "<<(*vi).GetName()<<endl;
}
void sinsert()
{
system("CLS");
int ag;
char p[100];
try
{
cin>>ag>>p;
myrec r(ag,p);
// itoa(ag,p,sizeof(ag));
// MessageBox(NULL,p,"DSF",MB_OK);
v.insert(v.begin(),r);
}
catch(...)
{
exception *e;
MessageBox(NULL,e->what() ,"DSF",MB_OK);
}
}
void einsert()
{
system("CLS");
int ag;
char p[100];
cin>>ag>>p;
myrec r(ag,p);
v.insert(v.end(),r);
}
void ainsert()
{
system("CLS");
int i;
int ag;
char p[100];
cout<<"Entered the number where to put the values"<<endl;
cin>>i;
cin>>ag>>p;
myrec r(ag,p);
v.insert(v.begin()+i-1,r);
}
void sdelete()
{
system("CLS");
if(v.size()>1)
v.erase(v.begin());
}
void edelete()
{
system("CLS");
if(v.size()>1)
v.erase(v.end()-1);
}
void adelete()
{
system("CLS");
int i;
cout<<"Entered the number where to put the values"<<endl;
cin>>i;
v.erase(v.begin()+i-1);
}
void search()
{
}
//
3 1 ONE
5 2 TWO
5 3 THREE
5 4 FOUR
NOW IF I PRESS 2 IT WILL DISPLAY
1 ONE
2 TWO
3 THREE
4 FOUR
NOW IF I DELETE AS FIRST PRESSING 6
AND PRESS 2 TO DISPLAY
IT WILL DISPLAY
2 TWO
3 THREE
4 ¨„¨„¨„¨„¨„¨„¨„¨„
-
I have been really busy so I havent been able to really loook close at it, but I think I have a direction to point you in. It looks like the problem is with your char pointers. When you loose an element your pointers become invalid. Perhaps you could rethink that part, maybe use strings.
Or you could always use std vectors or maps ;)