Vector insert() causes program to crash
        Posted  
        
            by wrongusername
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by wrongusername
        
        
        
        Published on 2010-05-01T04:59:53Z
        Indexed on 
            2010/05/01
            5:07 UTC
        
        
        Read the original article
        Hit count: 322
        
This is the first part of a function I have that's causing my program to crash:
vector<Student> sortGPA(vector<Student> student) {
    vector<Student> sorted;
    Student test = student[0];
    cout << "here\n";
    sorted.insert(student.begin(), student[0]);
    cout << "it failed.\n";
         ...
It crashes right at the sorted part because I can see "here" on the screen but not "it failed." The following error message comes up:
Debug Assertion Failed!
(a long path here...)
Expression: vector emplace iterator outside range
For more information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
I'm not sure what's causing the problem now, since I have a similar line of code elsewhere student.insert(student.begin() + position(temp, student), temp); that does not crash (where position returns an int and temp is another declaration of a struct Student). What can I do to resolve the problem, and how is the first insert different from the second one?
© Stack Overflow or respective owner