I have the following loop in my code:
for (const auto& item : vec) {...}
In order to find the position of item
in vec
, I do:
size_t pos = ((size_t)&item-(size_t)vec.data())/sizeof(item);
But it feels kinda "C" (in opposed to "C++").
I know that I can achieve this purpose if I change my loop-format to either one of the following:
for (size_t i = 0; i < vec.size(); i++) {...}
for (auto it = vec.begin(); it != vec.end(); it++) {...}
But I would like to refrain from doing this.
Bottom line questions:
- Is my method guaranteed by the language standard?
- Is there a more "C++" way to do this without changing the loop-format?
Thank you.