list.remove() crash.

Hi:

I have a short code below which polulats a list with 3 strings and then try
to remove them by calling list.remove(). but the program crashes at the 2nd
run in the loop (as marked below). Could you see anything I'm doing wrong?

Thanks for your input.
Polaris
-------------------------------------------------------------
#include <list>

typedef std::list <char*>::iterator LI;

int main (int argc, char* argv[])
{
        char* array[] =
        {
                { "Test1" },
                { "Test2" },
                { "Test3" }
        };

        std::list <char*> mylist;

        for (int i=0; i<3; i++)
            mylist.push_back (array[i]);

        for (LI x=mylist.begin(); x!=mylist.end(); x++)
            mylist.remove (*x);  // ******* crash at 2nd rund *****.

        return 0;
}


0
etpolaris (100)
5/14/2004 5:08:43 AM
vc.mfc 33608 articles. 0 followers. Follow

4 Replies
1048 Views

Similar Articles

[PageSpeed] 13

You are iterating through the list and deleting from it at the same time.  I
suspect this will cause undefined behaviour.  What are you trying to do?

James

"Polaris" <etpolaris@hotmail.com> wrote in message
news:uyvnAGXOEHA.2704@TK2MSFTNGP10.phx.gbl...
> Hi:
>
> I have a short code below which polulats a list with 3 strings and then
try
> to remove them by calling list.remove(). but the program crashes at the
2nd
> run in the loop (as marked below). Could you see anything I'm doing wrong?
>
> Thanks for your input.
> Polaris
> -------------------------------------------------------------
> #include <list>
>
> typedef std::list <char*>::iterator LI;
>
> int main (int argc, char* argv[])
> {
>         char* array[] =
>         {
>                 { "Test1" },
>                 { "Test2" },
>                 { "Test3" }
>         };
>
>         std::list <char*> mylist;
>
>         for (int i=0; i<3; i++)
>             mylist.push_back (array[i]);
>
>         for (LI x=mylist.begin(); x!=mylist.end(); x++)
>             mylist.remove (*x);  // ******* crash at 2nd rund *****.
>
>         return 0;
> }
>
>


0
James
5/14/2004 4:42:27 PM
Thanks for your info. I think you are right. I'm trying to remove ites with
certain names. It works after using the reverse_iterator and remove
backward.

"James Ryan" <__@_jryan@__icescape.__com> wrote in message
news:uckcHLdOEHA.808@tk2msftngp13.phx.gbl...
> You are iterating through the list and deleting from it at the same time.
I
> suspect this will cause undefined behaviour.  What are you trying to do?
>
> James
>
> "Polaris" <etpolaris@hotmail.com> wrote in message
> news:uyvnAGXOEHA.2704@TK2MSFTNGP10.phx.gbl...
> > Hi:
> >
> > I have a short code below which polulats a list with 3 strings and then
> try
> > to remove them by calling list.remove(). but the program crashes at the
> 2nd
> > run in the loop (as marked below). Could you see anything I'm doing
wrong?
> >
> > Thanks for your input.
> > Polaris
> > -------------------------------------------------------------
> > #include <list>
> >
> > typedef std::list <char*>::iterator LI;
> >
> > int main (int argc, char* argv[])
> > {
> >         char* array[] =
> >         {
> >                 { "Test1" },
> >                 { "Test2" },
> >                 { "Test3" }
> >         };
> >
> >         std::list <char*> mylist;
> >
> >         for (int i=0; i<3; i++)
> >             mylist.push_back (array[i]);
> >
> >         for (LI x=mylist.begin(); x!=mylist.end(); x++)
> >             mylist.remove (*x);  // ******* crash at 2nd rund *****.
> >
> >         return 0;
> > }
> >
> >
>
>


0
etpolaris (100)
5/14/2004 5:24:17 PM
Thanks for your info. That will not work for me because I need to remove
certain items from the list. What I' doing now is to put those items to be
removed into a seperat array and then remove() them one by one. it seems
working. but still, I think there should be better way. Thanks.

"Kelly Herald" <k@nospam.no> wrote in message
news:%23WG1SWgOEHA.1456@TK2MSFTNGP09.phx.gbl...
> How about this:
>
> If you are just wanting to empty the whole list without doing anything
with each element:
>
>     mylist.clear();
>
> or
>
>     while (!mylist.empty())
>         mylist.pop_back();
>
> or if you need to do something with each element before removing from the
list:
>
>     while (!mylist.empty())
>     {
>         LI = mylist.begin();
>         // do something with LI
>         mylist.remove(*LI);
>     }
>
>
> "Polaris" <etpolaris@hotmail.com> wrote in message
news:uyvnAGXOEHA.2704@TK2MSFTNGP10.phx.gbl...
> > Hi:
> >
> > I have a short code below which polulats a list with 3 strings and then
try
> > to remove them by calling list.remove(). but the program crashes at the
2nd
> > run in the loop (as marked below). Could you see anything I'm doing
wrong?
> >
> > Thanks for your input.
> > Polaris
> > -------------------------------------------------------------
> > #include <list>
> >
> > typedef std::list <char*>::iterator LI;
> >
> > int main (int argc, char* argv[])
> > {
> >         char* array[] =
> >         {
> >                 { "Test1" },
> >                 { "Test2" },
> >                 { "Test3" }
> >         };
> >
> >         std::list <char*> mylist;
> >
> >         for (int i=0; i<3; i++)
> >             mylist.push_back (array[i]);
> >
> >         for (LI x=mylist.begin(); x!=mylist.end(); x++)
> >             mylist.remove (*x);  // ******* crash at 2nd rund *****.
> >
> >         return 0;
> > }
> >
> >


0
etpolaris (100)
5/15/2004 12:36:11 AM
In article <ehIQgShOEHA.2844@tk2msftngp13.phx.gbl>, 
etpolaris@hotmail.com says...
> Thanks for your info. That will not work for me because I need to remove
> certain items from the list. What I' doing now is to put those items to be
> removed into a seperat array and then remove() them one by one. it seems
> working. but still, I think there should be better way. Thanks.

There is.  Look up std::list::remove_if.  Just for example, to remove 
every item less than 100 from a list of ints:

std::list<int> integers;

integers.remove_if(std::bind2nd(std::less<int>(), 100));

-- 
    Later,
    Jerry.

The universe is a figment of its own imagination.
0
jcoffin1 (194)
5/16/2004 7:25:53 AM
Reply:

Similar Artilces:

Collapsed list view report control
I'm try to set up an EXCEL like window, which apparently one can do with a REPORT control. I actually get it work, but all the columns are scrunched to the left when the window comes up, forcing the user to move the partions. I'm using Visual C++ 7.0. Here' the code. Thanks, Ihor P.S. Is there an easier way of inserting a line of data?? ----------------------------------------------------------------------- CRect rect(16 ,60,450 ,250); m_pendlistctrl.Create(WS_VISIBLE|WS_CHILD|LVS_REPORT,rect, this,IDD_WIRE_DIALOG); //m_pendlistctrl is a CListCtrl m_...

Income in Payee List?
Why does my income (paychecks, child support received, etc.) show up in my Payee list. Are they supposed to? Thanks -- Lori In microsoft.public.money, Lori wrote: >Why does my income (paychecks, child support received, etc.) show up in my >Payee list. Are they supposed to? Thanks It's up to you. If you want to rename or move those "payees" to something more meaningful to you, there are buttons to make those choices. Just select the odd payee, and choose Rename, Move or Delete. Move or Delete will let you combine payees. I'm not sure I understand what you mean by...

List Validation
I was wondering if there was anyway you can have a drop down list in one workbook that comes from a different workbook. Example: I have a workbook saved with a list of names and I want to have that list of names come up in a form that I have saved in another workbook. http://www.contextures.com/xlDataVal05.html note that the other workbook needs to be open -- Regards, Peo Sjoblom Excel 95 - Excel 2007 Northwest Excel Solutions www.nwexcelsolutions.com "It is a good thing to follow the first law of holes; if you are in one stop digging." Lord Healey "Renee" <...

Excel 97 crashes #2
Hi I'm working with Windows XP and Excel 97. When I enter the menu command "tools -> options" Excel crashes immediately. I tried to solve this problem with the following run-commands, but it fails: ....\EXCEL.EXE" /unregserver ....\EXCEL.EXE" /regserver Any ideas how to get this work? Tom "Tom" <tstruebi@gmx.net> wrote in message news:umnSeNOXEHA.4032@TK2MSFTNGP11.phx.gbl... > I'm working with Windows XP and Excel 97. When I enter the menu command > "tools -> options" Excel crashes immediately. Hi Tom, See the follo...

how do I increase drop list font and box size I created
Font is small and you have to scroll through list due to box size If the dropdown was created using Data Validation.............Font size is set at 8pt Dropdown box height cannot be changed. For workarounds like using a ComboBox and using Zoom see Debra Dalgleish's site on DV Lists. http://www.contextures.on.ca/tiptech.html Gord Dibben MS Excel MVP On Fri, 4 Apr 2008 11:36:02 -0700, cdietz1 <cdietz1@discussions.microsoft.com> wrote: >Font is small and you have to scroll through list due to box size ...

Max Limit for Values in List Boxes and Combo Boxes in Outlook forms
Folks, a user is setting up a form. She wants to have several combo boxes and list boxes with between 75 and 300 (yes, 300 -- that's not a typo!) possible values in the combo box or list box. Before she starts work on this, she would like to know if there is a limit on the number of possible values for a combo box or a multi-select list box. Is there a limit? Is it defined by the number of characters in the values box, or the number of actual values? What is the limit? Any help here would be appreciated. Thanks. David consulttech2004@hotmail.com ...

Stop advancing in form with the "next" arrow at end of "list"
I have an editable form for users where they can query by date, etc. However, the next entry arrow allows them to advance beyond the result. For example, if their query resulted in 1 of 4, they can still click to 5, 6, and so on which only adds blank lines to the database table. How do I set this to stop at the last entry of their original query? You can cancel adding new records like this: Private Sub Form_BeforeInsert(Cancel As Integer) Cancel = True End Sub Jeanette Cunningham MS Access MVP -- Melbourne Victoria Australia "Duchess" <Duchess@discuss...

Outlook not removing deleted messages from server
I have a gmail account and Outlook 2007 is configured to leave the messages on the POP3 server, but to remove the messages from the server when deleted from the "deleted items" folder. So, I downloaded all of my email (approx 17,000 messages), deleted some messages I don't want any more and emptied the "deleted items" folder, but the messages are not removed from the server. I've tried it several times in several different ways, always with the same result. I can delete the local copy on my PC, but the server copy is never removed. "sparx&qu...

HELP! When I removed Office XP did I lose by inbox file?
I didn't archive my inbox and personal folders before removing Office XP. Now I have reinstalled Office XP and thought I could find my old data files. but all of the import and repair tools work with PST which I don't have. I remember that previously there were seperate inbox files and can not find them now. Thank you for your input. Ron Be sure to show hidden and system files on your computer and then do a search for *.pst. This should get you started... "Ron" <anonymous@discussions.microsoft.com> wrote in message news:093b01c3ba63$265a8120$a401280a@phx.gbl....

list.remove() crash.
Hi: I have a short code below which polulats a list with 3 strings and then try to remove them by calling list.remove(). but the program crashes at the 2nd run in the loop (as marked below). Could you see anything I'm doing wrong? Thanks for your input. Polaris ------------------------------------------------------------- #include <list> typedef std::list <char*>::iterator LI; int main (int argc, char* argv[]) { char* array[] = { { "Test1" }, { "Test2" }, { "Test3" } }; ...

Removing last 5.5 server
We migrated from NT 4.0 and Exchange 5.5 to Win 2003 and Exchange 2003. The ADC was installed to move the mailboxes from the old server to the new one. While the new Exchange server was installed into the new AD domain it was also installed into the old Exchange 5.5 "site". In system manager we have one Administrative Group which shows both servers. If I select the properties from this group it shows the old domain and administrator account. Is this correct? Do I need to be concerned if I remove the old Exchange server? I have followed all of the other instructions from the MS...

Intercompany AP Edit List
I entered an Intercompany AP invoice. I distributed as follows: Originating Company AP $300 Originating Company Expense $150 Destination Company Expense $150 When I print the Payables Transaction Edit List, it shows a completely different account for the Destination Company entry. It is an account in my Originating Company that has nothing to do with the invoice or the vendor. But when I post it, the General Ledger posting journal looks good. Can someone please let me know if this is a known flaw in GP 9.0 or have I set something up wrong? Thanks for any help! ...

create price list from excel sheet
I have been using Excel 2003 to manage a list of approximately 200 items. This is a very simple list comprising of four or five fields one of which is the price. I prefer to use Excel to manage this data because it then becomes very simple for me to use formulas in order to calculate prices after changes or additions to the items. I would like to produce a simple A5 flyer listing all the items together with their prices. What will be the best way to do this? I have explored the possibility of exporting the Excel sheet into something like Microsoft publisher but I am looking for a fa...

How to remove the last separator from toolbar
Hi, I need to add/remove some buttons from the toolbar depends on user settings. But after I remove the buttons, there is a separator at the end of the toolbar. Is there any way I can remove the separator? Thanks, Lucy This might seem like a stupid question, but how are you removing a button from CToolbar? AliR. <lucyh3h@yahoo.com> wrote in message news:1169232478.123890.276230@l53g2000cwa.googlegroups.com... > Hi, > > I need to add/remove some buttons from the toolbar depends on user > settings. But after I remove the buttons, there is a separator at the > end of ...

msn sync crashes Money 2004
This is a multi-part message in MIME format. ------=_NextPart_000_0134_01C3D949.E61EBD00 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Every time I try to sync Money2004 standard with msn money Money crashes = with a mscofd.dll error. I have tried uninstalling and reinstalling = Money to no avail. Synchronization with my brokerage and credit card = accounts works fine. Any suggestions. Pierre Aterianus ------=_NextPart_000_0134_01C3D949.E61EBD00 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: q...

Rounding up price lists
Can you round to the nearest Dollar? Yes, if the Price Method is one that caculates the selling price (such as % of List, markup from list, etc). Click on Options, select the Round Option and set to multiple of and enter 1.00 in the Round Amount field. -- Richard L. Whaley Author / Consultant / MVP Documentation for Software Users Get our Free Tips and Tricks Newsletter and check out our books at http://www.AccoladePublications.com "tstrop" wrote: > Can you round to the nearest Dollar? ...

Data List #2
In relation to an earlier question, I am looking for a way of creatin box which displays the contents of a list, which when clicked on can b copied to a current cell, but other items added to this list. For example, in one spreadsheet a list of; John Paul Peter In another spreadsheet, a view of this, so that the values can easil be copied over, but also the abaility to add another name. Something like.. [Players] | John | | Paul | | Peter | ... [ADD NEW] Any ideas? Many thanks, Mic -- Message posted from http://www.ExcelForum.com Mich Lookup data validation. It is very straigh...

How do I find duplicate rows in a list in Excel, and not delete it
I have a long list of data in Excel that is 3 columns wide. I need to find and save only the duplicate rows but don't want to delete them. Instead, I could delete the unique rows and keep the duplicate rows. In the customer assistance, I found out how to delete duplicate rows and save the unique rows, but this is exactly opposite of what I want to do. This is one option: =COUNTIF($A$4:$A$18,A4)>1 copy down, and use a Autofilter to find all True HTH Ola Sandstr´┐Żm Picture encl.: http://www.excelforum.com/attachment.php?attachmentid=3498&stc=1 +---------------------------...

Removing lines and/or borders
how do I remove a line or border that only appears on normal view not print preview? If this is not part of your print preview, it is simply the gridlines outline the cells in your working window. If you want to turn these off as well, select Options from the Tools menu, select the View tab, then uncheck the Gridlines checkbox. -- Thanks, Deb "mystical_ways" wrote: > how do I remove a line or border that only appears on normal view not print > preview? ...

Create a textbox that displays a list of possible items as we type
Hi All, Is it possible to display a list once user starts typing a word in vba? For eg, I have a userform connected to access database(I can already pull info from access, based on what user inputs in ID field), So Once a user starts typing in a text box, lets say User Name textbox, a list of users is displayed (something like google search, we start writing in the search bar and it suggests us what we might want based on what text we input) SO if I type in "A" in User Name text box, I get all the names starting with "A" such as: Name: [ A.... ] ...

add to existing distribution list
Trying to add to an existing distribution list; (from MS Access to Outlook 2003) ; code below can add, but always creates new DL (see below); skipping through Access records into general top level contacts was easy, DL work has been very frustrating. Code below works (it adds to a list) but it needs to refer to a known list. How do I do this successfully ? I've played with : objDistList = myFolder.Items.Add(Outlook.OlItemType.olDistributionListItem) in other code but get "object variable not set" even though I can see it has been ! Dim objOutlook ...

minipulate the unwanted senders list by means of VB in Outlook
Hi, using outlook 2002 sp3 is there a way to minipulate the unwanted senders list (add entries) by means of VB ? Best rgds, Ton On Mon, 30 May 2005 20:45:03 -1000, tondewilde <ton@winemaker.nl> wrote: Maybe. The Junk Senders list is contained in a text file called "Junk Senders.txt". If you can manipulate text files in your code you can do it. > Hi, > using outlook 2002 sp3 > is there a way to minipulate the unwanted senders list (add entries) by > means of VB ? > Best rgds, > Ton > > -- -Ben- Ben M. Schorr, MVP-OneNote/Outlook Operations...

Exchange 2007 lets phishing emails in via Safe Lists?
I've tried to post this a few times, but something in must be triggering a filter software as it hasn't shown up yet after 12 hrs and the web post shows up there but not in the NNTP. Here goes again, with a few more potential keywords changed: In Exchange 2007 with safe-list aggregation enabled, it appears that certain phishing messages get through we/of any phishing score. The culprit is that the recipient has the address in the safe-senders list (@phishingSite.com, for example). The user will put that there so that OL loads the images -- but a few phishing messages have now...

Server list blank when launching GP Utilities
We just installed GP 9.0 on a new workstation. When we try to launch GP Utilities, we do not see our ODBC connection in the list of server names. The list is blank. We did create the System DSN correctly. Any ideas? Are you logged in as an administrator of that machine? Usually when you create a system DSN as a non-administrative user, for some strange reason Windows will let you continue with the procedure, but will not save the connection. Best regards, -- MG.- Mariano Gomez, MIS, MCP, PMP Maximum Global Business, LLC http://www.maximumglobalbusiness.com The Dynamics GP Blogster a...

Task list sorting?
I have entered a DUE DATE for several tasks. List sorts by this just fine. If I enter a new task with a due date - in the past - seems like it should sort to the top - but no - it sorts to just AFTER any others with dates. How do I get a true sort by any due date? Thx. JDS As a very general answer: click the column header to sort. I have no idea what you are looking at by the way. Which version? Which folder? Which view? I hope this helps you at least a little bit! Judy Gleeson MVP Outlook Outlook trainer and author of Productiv_IT with Outlook read my articles here: www.judygl...