-4

I'm a newbie in C++ STL. I have some problem in the below code with respect to the output.Why is this not providing the expected output?

#include<iostream>
#include<algorithm>
#include<vector>
#include<iterator>
using namespace std;
void show(vector<int> vect)
{
    for(int i=0;i<vect.size();i++)
    {
        cout<<vect[i]<<" ";
    }
}
int main()
{

    int arr[5]={ 1, 2, 3, 4, 5 };
    vector<int> vect1(arr,arr+5);
    vector<int>::iterator it;
    it=vect1.begin();
    advance(it,3);
    copy(vect1.begin(),vect1.end(),inserter(vect1,it));
    show(vect1);
    return 0;

}

Expected output:1 2 3 1 2 3 4 5 4 5
Actual output:1 2 3 1 0 3 4 5 4 5

Can anyone help me in finding out where it went wrong?

  • 2
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/) – NathanOliver Aug 19 '19 at 12:27
  • 2
    Also, when posting a [mcve] here, make sure it compiles. – Ted Lyngmo Aug 19 '19 at 12:30
  • @M B Nandhini After inserting a data in a vector its iterators become invalid. – Vlad from Moscow Aug 19 '19 at 12:30
  • 2
    the first number you print is the 4th element via `cout<<*it;`. I would expect a 4 as first output. Please turn your code into a mcve, by adding the missing includes – 463035818_is_not_an_ai Aug 19 '19 at 12:31
  • 1
    You may also want to take a look at the documentation for [std::copy](https://en.cppreference.com/w/cpp/algorithm/copy) which states "Copies all elements in the range [first, last) starting from first and proceeding to last - 1. The behavior is undefined if d_first is within the range [first, last). In this case, std::copy_backward may be used instead." – pstrjds Aug 19 '19 at 12:44
  • @formerlyknownas_463035818 Thanks for pointing it out.`cout<<*it;` was by mistake.Other than that the code compiles fine but the output is weird. – M B Nandhini Aug 19 '19 at 15:19
  • no the code you posted does not compile fine. Especially in the presence of a `using namespace std;` (which apparently you have in code you dont show) it is important to present the full code, because `inserter` or `cout`, etc could be anything – 463035818_is_not_an_ai Aug 19 '19 at 15:23
  • @MBNandhini [`std::inserter()`](https://en.cppreference.com/w/cpp/iterator/inserter) calls the container's `insert()` method. For [`std::vector::insert()`](https://en.cppreference.com/w/cpp/container/vector/insert), "*The behavior is undefined if `first` and `last` are iterators into `*this`.*". IOW, you can't legally insert into the same container that you are iterating through. Just because your code may compile does not mean it does the right thing. Undefined behavior is always wrong – Remy Lebeau Aug 19 '19 at 15:48
  • @RemyLebeau Thats correct,I have came across the same in documentation provided by _pstrjds_. Thanks everyone. – M B Nandhini Aug 19 '19 at 16:19
  • @MBNandhini part of the undefined behavior is due to iterator invalidation if pushing a new value into the vector reallocates its internal array. If you `reserve()` the vector beforehand, you can avoid that reallocation. Otherwise, use a `for` loop instead of `std::copy()` using indexes on each iteration. That would work too, and doesn't require `reserve()` – Remy Lebeau Aug 19 '19 at 19:02

1 Answers1

1

Replace the copy with this:

vect1.insert(it, std::begin(arr), std::end(arr));

where the arguments to insert are:
it is the position you want to insert into vector vect1;
std::begin(arr) is the start position of range to insert;
std::end(arr) is the end position of range to insert.

jignatius
  • 6,304
  • 2
  • 15
  • 30