1

I am not proficient in C++. I have a very short C++ script I am trying to convert to PHP which deals with vectors.

From online documentation I gather that vectors are lists of stuff, somewhat like arrays in PHP but with less features. However, I am struggling to find good documentation on vector manipulation.

For example

real32 test(std::vector<T>::iterator First, std::vector<T>::iterator Last)
{
    if(Last - First > 0) {
        // do stuff
    }
}

In this snippet I can hazard a guess at a number of outcomes to do with Last - First.

  • The difference in the number of elements between each vector
  • The difference in the sum of the elements between each vector
  • The difference between single elements in each vector (guessing from iterator - but this might be the for loop in the //do stuff part)

I am trying to RTFM but the some of the on-line resources I have stumbled across don't come across as particularly insightful (although maybe I am not able to ask/search the right question)

What is Last - First actually doing in this case?

Dumkaaa
  • 488
  • 2
  • 9
myol
  • 8,857
  • 19
  • 82
  • 143
  • 1
    Do note that two iterators may belong to two different vectors! – Ajay Jun 01 '16 at 08:50
  • @Ajay If they do then that function will exhibit undefined behavior. – Galik Jun 01 '16 at 08:56
  • @Galik, True. But such function is bound to raise runtime error. Try passing different iterators to `vector` and see it handles it well (well, at least in VC++) – Ajay Jun 01 '16 at 09:07

1 Answers1

6

The function does not take 2 vectors, but 2 iterators, which point at a particular place inside a vector. In this case, the first and one-past-the-last elements. This is a normal C++ idiom. (Note that the iterators might actually indicate some sub-part of an existing vector, doesn't actually have to be the whole thing).

Therefore the difference is the total number of elements in the vector. Usually, one would have a loop handling each element, and incrementing First until it hits Last.

1|2|3|4|5|
^First    ^Last

Note that Last does not point at 5, but one past it. Last is not a valid element of the vector, but a sentinel, indicating when to stop looping.

In this case Last-First == 5, since you would have to increment First 5 times to hit Last.

Reference documentation is not usually the best place to learn the language from. We do have the book list.

Your links:

  • isocpp.org: About the standardisation process and committee, not much actual language documentation here.

  • cplusplus.com: a non-official reference documentation site. In the past, was not very good about fixing errors. So many people prefer:

  • cppreference.com: a community-maintained wiki of reference documentation.

There is no "official" reference documentation other than The Standard, a draft of which can be found on isocpp.org, but really, don't try to read that, it is experts-only, aimed at compiler implementers. I'm not even going to link to it.

To be honest, I struggle to find a really good beginners tutorial online.

Community
  • 1
  • 1
BoBTFish
  • 19,167
  • 3
  • 49
  • 76
  • 1
    Strictly speaking we don't know that these two iterators point to the first and last elements of the vector, only that they point to a beginning and end portion of the vector. Their difference is the number of elements that lie between those two points. – Galik Jun 01 '16 at 08:51