S
S
sabn1k2016-03-01 20:03:30
C++ / C#
sabn1k, 2016-03-01 20:03:30

By what principle are numbers displayed (back_inserter, front_inserter)?

I just can’t understand why 3 2 1 7 8 9 is displayed with back_inserter, and 7 8 9 1 2 3 with front_inserter? Do I understand correctly that the LIFO principle applies here? Then why does front_inserter print 1 2 3 at the end, and not 3 2 1, as in the first option?

int main()
{
  setlocale(LC_ALL, "Russian");
  deque<int> d1, d2;

  int arr1[] = { 1, 2, 3 };
  int arr2[] = { 7, 8, 9 };

  for (int j = 0; j < 3; j++)
  {
    d1.push_back(arr1[j]);
    d2.push_back(arr2[j]);
  }
  copy(d1.begin(), d1.end(), back_inserter(d2)); // или front_inserter(d2)
  for (int j = 0; j < d2.size(); j++)
    cout << d2[j] << ' ';
  return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MiiNiPaa, 2016-03-01
@sabn1k

You are confused about what is output. back_inserter will have 7 8 9 1 2 3, front_inserter will have 3 2 1 7 8 9 back_inserter
inserts each element at the end: 7 8 9 → 7 8 9 1 → 7 8 9 1 2 → 7 8 9 1 2 3
start: 7 8 9 → 1 7 8 9 → 2 1 7 8 9 → 3 2 1 7 8 9

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question