简体   繁体   中英

C++ Update vector elements through iterator

I am implementing in-place merge sort, so I need to update the vector container elements without creating a new container. I am iterating through the container, and do not have the vector container as parameter. I am only passing the iterator of targeted container.

My question is "how do I update or replace vector container elements only through iterator of that vector container?"

The following is part of my code.

  template<typename T>
  void it_practice(T begin, T end) {

  for(T it = begin; it != end; ++it)
  {
        if (R_half[j] >= L_half[i])
        {
              *it = L_half[i];
              i++;
        }
        else
        {
              *it = R_half[j];
              j++;
        }
  }

*it gives you a reference to the according element, and that reference can be assigned to. Iterators behave similar to pointers, keep that in mind.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM