Joh Daslatk
Joh Daslatk

Reputation: 3

Alternative to std::next

I'm working on a C++ code where the compiler version supports the std::next() command, but I'm migrating this code to work on a system where the compiler does not support C++11, and I need to migrate a part of the code that uses std::next, what is the best alternative for this?

The code that I need to migrate is:

uint32_t getUint32(uint32_t position, vector<uint8_t> data)
{
    auto it = next(data.cbegin(), position);
    ...
}

Upvotes: 0

Views: 417

Answers (2)

Ron
Ron

Reputation: 15501

As suggested in the comments, you can implement your own next function by mimicking the possible implementation:*

template<class T>
T mynext(T it, typename std::iterator_traits<T>::difference_type n = 1) {
    std::advance(it, n);
    return it;
}

* Everything in this code is C++03 compliant. If you go with this implementation you should properly attribute the cppreference source.

Upvotes: 1

juanchopanza
juanchopanza

Reputation: 227390

You can use std::advance (namespace std assumed):

vector<uint8_t>::const_iterator it = data.begin();
advance(it, position);

Upvotes: 1

Related Questions