Andrew Truckle
Andrew Truckle

Reputation: 19117

Using boost::counting_iterator with an existing vector?

At the moment I am doing this:

const int n = 13;

std::vector<int> v(boost::counting_iterator<int>(0), boost::counting_iterator<int>(n + 1));
std::copy(v.begin(), v.end(), back_inserter(m_vecAssignmentIndex));

m_vecAssignmentIndex is defined liek this:

ByteVector m_vecAssignmentIndex;

And, ByteVector:

using ByteVector = std::vector<BYTE>;

Is it possible to assign directly to m_vecAssignmentIndex and avoid std::copy?


Update

So, code like this is OK:

std::vector<BYTE> v2(boost::counting_iterator<BYTE>(0), boost::counting_iterator<BYTE>(n + 1));
std::vector<int> v(boost::counting_iterator<int>(0), boost::counting_iterator<int>(n + 1));
std::copy(v.begin(), v.end(), back_inserter(m_vecAssignmentSortedIndex));

Thus, I can directly increment BYTE values. So how can I avoid the requirement for the temp vector?

Upvotes: 0

Views: 118

Answers (1)

Andrew Truckle
Andrew Truckle

Reputation: 19117

I have now found the samples in the official docs:

int N = 7;
std::vector<int> numbers;
typedef std::vector<int>::iterator n_iter;
std::copy(boost::counting_iterator<int>(0),
         boost::counting_iterator<int>(N),
         std::back_inserter(numbers));

std::vector<std::vector<int>::iterator> pointers;
std::copy(boost::make_counting_iterator(numbers.begin()),
          boost::make_counting_iterator(numbers.end()),
          std::back_inserter(pointers));

std::cout << "indirectly printing out the numbers from 0 to "
          << N << std::endl;
std::copy(boost::make_indirect_iterator(pointers.begin()),
          boost::make_indirect_iterator(pointers.end()),
          std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;

So:

std::copy(boost::counting_iterator<BYTE>(0),
    boost::counting_iterator<BYTE>(n), std::back_inserter(m_vecAssignmentSortedIndex));

Upvotes: 2

Related Questions