0
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() 
{
    int a, b;
    cin>>a;
    vector<int> numbers;
    for(int i=0; i<a; i++)
    {
        cin>>b;
        numbers.push_back(b);
    }

    int c,d,e;
    cin>>c;
    numbers.erase(numbers.begin()+c-1);
    cin>>d>>e;
    numbers.erase(numbers.begin()+d-1, numbers.end()+e);

    cout<<numbers.size();
    for(int x : numbers)
        cout<<x<<" ";

    return 0;
}

Hello everyone, I am learning c++ and writing a very simple program, but this code is giving "Segmentation Fault" as error. I really could not figure out why this is happening.

Thank you.

KNF TV
  • 49
  • 4

1 Answers1

1

This line

numbers.erase(numbers.begin()+d-1, numbers.end()+e);

cannot be correct. Incrementing the end iterator never gets you a valid iterator. It is not quite clear what you want to do, but if you want to erase elements in the range of indices [d,e) then that would be

numbers.erase(numbers.begin()+d, numbers.begin()+e);

Note: No +1 needed on the first, because the first is inclusive. And you get an iterator to the e-th element by incrementing the begin iterator not the end iterator (well... I assume the common 0-based counting, ie the "first" element is the 0th element ;).

Also, as mentioned in comments, you should check if the user entered values are in range, before calling erase. erase does no bounds-checking. If you pass invalid iterators your get undefined behavior.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Thank you everyone for your valuable comments. Don't worry i am not building any kind of application right now. just started with competitive programming and was trying to learn c++. I feel so dumb that i could not figure this small mistake. lol. – KNF TV Apr 22 '20 at 13:19
  • 3
    @KNFTV I did not worry (your comment is only addressed at me btw). However, what really does make me worry a lot is "started with competivite programming". You won't learn anything useful from participating in programming contest (I mean not anything apart from bad practices and stuff that is only good for programming contests but nothing else). If you do want to learn C++, you should grab a [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and stay away from any online tutorials/contests, there is just too much low quality stuff out there – 463035818_is_not_an_ai Apr 22 '20 at 13:24
  • @KNFTV in case you refer to checking the input for being valid, this is something you should always do, wether for an application or in your exercises. Using invalid iterators is no fun – 463035818_is_not_an_ai Apr 22 '20 at 13:26
  • 1
    I have to strongly agree with @idclev463035818 Please consider following his advice – nada Apr 22 '20 at 14:12
  • Hey thanks for your input, i really appreciate that. I have just skimmed through c++ primer and accelerated c++ (phew this was little confusing for me) and was also looking forward to other books as well (so thanks for the resource list, i was confused regarding this as i want to learn more about c++). I started "competitive programming" to get hands on experience and also i was suggested that it would improve my programming skills in general with respect to c++ or data structures or algorithms. And yes i would definitely improve in the iterators part as well. Thanking You again. – KNF TV Apr 22 '20 at 14:44
  • @KNFTV " i was suggested that it would improve my programming skills in general with respect to c++ or data structures or algorithms" the thing is, it doesnt. Coding contest typically train a very narrow set of skills and very special usage of algorithms and data-structures. It does not hurt to have seen it, but it does hurt to believe it would be good training for real-world coding – 463035818_is_not_an_ai Apr 22 '20 at 14:46