Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 844501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:17:44+00:00 2026-05-15T06:17:44+00:00

#include stdafx.h int _tmain(int argc, _TCHAR* argv[]) { string s = Haven’t got an

  • 0
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
    string s = "Haven't got an idea why.";
    auto beg =  s.begin();
    auto end = s.end();
    while (beg < end)
    {
        cout << *beg << '\n';
        if (*beg == 'a')
        {//whithout if construct it works perfectly
            beg = s.erase(beg);
        }
        ++beg;
    }
    return 0;
}

Why if I erase one or more chars from this string this code breaks? I suppose it has something to do with returned iterator after erase operation being created at higher address than end iterator but I’m not sure and it surely isn’t right behaviour. Or is it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:17:45+00:00Added an answer on May 15, 2026 at 6:17 am

    There are several problems with this code.

    1. Don’t cache the value of s.end(); it changes as you delete elements.
    2. Don’t use beg < end. The idiomatic approach is to write beg != end. If you try to iterate past end, the result is undefined, and a debug version of the string library may deliberately crash your process, so it is meaningless to use <.
    3. The iterator returned from s.erase(beg) might be s.end(), in which case ++beg takes you past the end.

    Here’s a (I think) correct version:

    int _tmain(int argc, _TCHAR* argv[])
    {
        string s = "Haven't got an idea why.";
        for (auto beg = s.begin(); beg != s.end();)
        {
            cout << *beg << '\n';
            if (*beg == 'a')
            {//whithout if construct it works perfectly
                beg = s.erase(beg);
            }
            else
            {
                ++beg;
            }
        }
    }
    

    EDIT: I suggest accepting FredOverflow’s answer. It is simpler and faster than the above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

project1.cpp #include stdafx.h #include Bicycle.cpp using namespace std; int _tmain(int argc, _TCHAR* argv[]) {
#include stdafx.h #include string.h #include stdio.h void CharReadWrite(FILE *fin); FILE *fptr2; int _tmain(int argc,
The following code #include stdafx.h #include <string> #include <set> #include <boost/algorithm/string/trim.hpp> int _tmain(int argc,
I have this code: #include stdafx.h #include <windows.h> #include <conio.h> int _tmain(int argc, _TCHAR*
// Huffman Tree.cpp #include stdafx.h #include <iostream> #include <string>//Necessary to do any string comparisons
#include <iostream> #include <string> #include <fstream> using namespace std ; string strWord( int index
Hey guys. Check out this piece of sample code. #include stdafx.h #include<conio.h> #include<string.h> class
This is my sample program #include stdafx.h class B { public: int i,j; };
#include stdafx.h #include <iostream> #include <string> using namespace std; const char* funA() { return
#include stdafx.h #include <iostream> #include <vector> #include <string> class Helper { public: Helper() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.