4

How to get int position of this loop? Thank you.

auto a = vect.begin();
auto b = vect2.begin();
auto c = vect3.begin();
for (; a != vect.end() && b != vect2.end() && c != vect3.end(); a++, b++, c++) {

}

I need to print values of other variable, but I need to get actual unsigned int position of this vector loop.

I need to print double vector using this position of this vector.

And how to get the last index of vector.

My problem is for for loop with multiple vectors and getting index from it next to use only last of indexes.

Adam
  • 151
  • 8
  • If you mean indices, you can use [std::distance](https://en.cppreference.com/w/cpp/iterator/distance). – Rafał Górczewski Oct 18 '18 at 07:56
  • Possible duplicate of [What is the most effective way to get the index of an iterator of an std::vector?](https://stackoverflow.com/questions/2152986/what-is-the-most-effective-way-to-get-the-index-of-an-iterator-of-an-stdvector) – TamaMcGlinn Oct 18 '18 at 09:12

2 Answers2

4

It's simple: if you need indices, don't use iterators:

for (
  size_t idx = 0, idxEnd = std::min({vect.size(), vect2.size(), vect3.size()});
  idx < idxEnd;
  ++idx
)
{
  auto& obj1 = vect[idx];
  auto& obj2 = vect2[idx];
  auto& obj3 = vect3[idx];
}

(The above code initialises idxEnd once at the start of the loop, so that it's not needlessly recomputed at each iteration. It's just an optimisation).

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • I need to print double vector using this position of this vector. Is it possible by this way? Thank you. – Adam Oct 18 '18 at 07:58
  • @Adam `idx` is the index (position) of the currently iterated element in the vectors. I assume that is what you need. If not, please clarify your question to show what you're actually trying to do. – Angew is no longer proud of SO Oct 18 '18 at 08:00
  • std::min where its defined? – Adam Oct 18 '18 at 08:05
  • Sorry angew but it does not working for me min gives me error, and I dont understand where idxEnd is defined – Adam Oct 18 '18 at 08:08
  • 1
    @Adam `idxEnd` is defined in the shown snippet. Specifically in the *init-statement condition* part of the `for` expression. – eerorika Oct 18 '18 at 08:11
  • @Adam You're probably not using C++11, then (which you should be, it's 7 years old now). Or you're missing `#include `. – Angew is no longer proud of SO Oct 18 '18 at 08:13
  • @Adam Regarding your question "std::min where its defined? " - Google is your friend, and will happily point you to https://en.cppreference.com/w/cpp/algorithm/min – Angew is no longer proud of SO Oct 18 '18 at 08:15
  • 1
    @Adam It seems to me as if you're more or less blindly trying whatever you come upon here, which is not how SO can really be beneficial to you. Perhaps you might want to pick up a [good book](https://stackoverflow.com/q/388242/1782465) to get a firmer grasp on C++ basics? – Angew is no longer proud of SO Oct 18 '18 at 08:42
4

As Angew shows, a simple indexed loop may be preferable when you need indices.

However, it is possible to get the index from an iterator as well:

auto a = vect.begin();
auto b = vect2.begin();
auto c = vect3.begin();
for (/*the loop conditions*/) {
    auto index = a - vect.begin();
}

It is also possible to get the index of a forward iterator using std::distance, but it would be unwise to use it in a loop, since the complexity will be linear for non-random-access iterators.

In the case of forward iterators (and generic code that must support forward iterators), you can write a loop which has both the index variable, and the iterators.

P.S. it is potentially preferable to use pre-increment with iterators. Probably only matters in debug build.

eerorika
  • 232,697
  • 12
  • 197
  • 326