Results 1 to 14 of 14

Thread: erase in vector

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2004
    Posts
    222

    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?

  2. #2
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    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]

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2004
    Posts
    222

    erase in vector

    Here are the files
    Attached Files Attached Files
    Last edited by purusingh1; Jun 14th, 2004 at 11:49 PM.

  4. #4
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    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;
            }
    Last edited by Darkwraith; Jun 16th, 2004 at 04:54 PM.
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  5. #5
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    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)
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  6. #6
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    Why not use the string class?
    "Can't" and "shouldn't" are two totally separate things.

    All questions should be answered. All answers should be true. That is why I post.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2004
    Posts
    222

    erase in vector

    Thanks
    I have changed the program but still I am getting the same problem.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Mar 2004
    Posts
    222

    erase in vector

    No answer ???????

  9. #9
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    try
    v.erase(v.end()-1, v.end());
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  10. #10
    Member
    Join Date
    Oct 2003
    Location
    England
    Posts
    40
    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
    Long live me and my global empire of toothpicks

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Mar 2004
    Posts
    222

    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.

  12. #12
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    Can you post the project again and an example of what you are doing?
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Mar 2004
    Posts
    222
    //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 ¨„¨„¨„¨„¨„¨„¨„¨„

  14. #14
    Frenzied Member Technocrat's Avatar
    Join Date
    Jan 2000
    Location
    I live in the 1s and 0s of everyones data streams
    Posts
    1,024
    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
    MSVS 6, .NET & .NET 2003 Pro
    I HATE MSDN with .NET & .NET 2003!!!

    Check out my sites:
    http://www.filthyhands.com
    http://www.techno-coding.com


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width