0

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?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • One is a BYTE vector and one is an int vector – user253751 May 09 '22 at 10:52
  • @user253751 So this `boost::counting_iterator` can't work with `BYTE` instead? I am using the code I have because of the samples I found here on SO. – Andrew Truckle May 09 '22 at 10:56
  • 2
    what happens if you make it work with BYTE instead? – user253751 May 09 '22 at 10:57
  • @user253751 Yes, I can use `BYTE` instead. But i am still back to square one. How do I use this `boost::counting_iterator` directly on my member variable? – Andrew Truckle May 09 '22 at 10:59
  • 1
    I am wondering if you know much about C++ – user253751 May 09 '22 at 11:04
  • @user253751 Ouch. :) – Andrew Truckle May 09 '22 at 11:20
  • 1
    it's an actual concern though, it sounds a bit like you are copy-pasting code snippets together without knowing e.g. how iterators work in C++. Is that right? There's nothing shameful about not knowing something yet, don't take it personally, you can learn it :) – user253751 May 09 '22 at 12:18
  • @user253751 Not really, I have been coding (self taught) for 20 + years. But it has been heavily MFC orientated. I have slowly added vectors and other more modern containers, so it is not all alien. It was simply the aspect of using boost (which I already had) and I was unsure how I could still use that code. I don't take it personally - no worries! :) – Andrew Truckle May 09 '22 at 12:23
  • 1
    A big big problem with "self-taught" is that you often learn the specifics and miss the more abstract patterns. An iterator is basically "a place in a range" e.g. a vector iterator can point to an item in the vector, or the end (just like a pointer can; in fact vector iterator can just be a pointer) but then you can also have a list iterator which is the list equivalent even though lists aren't vectors. And counting_iterator is a place in a fake range that contains all the numbers but doesn't actually exist in memory! – user253751 May 09 '22 at 12:29
  • 1
    std::copy(v.begin(), v.end(), blah) copies all the items between the beginning of v and the end of v (i.e. all the items in v); std::copy(counting_iterator(1), counting_iterator(100), blah) copies all the items between 1 and 100 without actually having to put them in some kind of container – user253751 May 09 '22 at 12:30

1 Answers1

2

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));
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 1
    Yes, this is how you can do it. Worth pointing out that `v.begin()` and `v.end()` *are iterators* just like counting_iterator, so you can use both of them in std::copy! – user253751 May 09 '22 at 12:20