1

because of not enough reputation I can't comment a post on the the thread:

How to get position of a certain element in strings vector, to use it as an index in ints vector?

I want to know something regarding this answer:


Original answer on thread:

To get a position of an element in a vector knowing an iterator pointing to the element, simply subtract v.begin() from the iterator:

ptrdiff_t pos = find(Names.begin(), Names.end(), old_name_) - Names.begin();

Now you need to check pos against Names.size() to see if it is out of bounds or not:

if(pos >= Names.size()) {
    //old_name_ not found
}

vector iterators behave in ways similar to array pointers; most of what you know about pointer arithmetic can be applied to vector iterators as well.

Starting with C++11 you can use std::distance in place of subtraction for both iterators and pointers:

ptrdiff_t pos = distance(Names.begin(), find(Names.begin(), Names.end(), old_name_));

Questions:

  1. How does this exactly work for the compiler? What happens in the background?

I know, that im passing two iterators to the find method, which seems logic: I pass the start and the end position. In between, I'm searching for the element. std::find() returns an iterator of the first element satisfying the value passed as third parameter to the std::find() method. So is std::distance doing nothing else than incrementing in a for loop something like a counter until it reaches the "end-point" iterator?

  1. I could also write:

    int index = distance(Names.begin(), find(Names.begin(), Names.end(), old_name_));
    

Why is it better to use ptrdiff_t than an integer? Someone commented in the original post that it lets you store the distance between any pair of iterators into the same container, even in situations when the result is negative. But I do not really get the point of it. Maybe you could explain that a bit further. (:

  1. I'm coming from C#, where you can use something like (demonstrated on a string):

    string test = "Hello World";
    int index = test.IndexOf("d");
    

This seems a lot easier compared to the methods used above in C++. Is there something equivalent in C++?

Thank you very much for your help!

Best regards

Chris Dunaway
  • 10,273
  • 2
  • 32
  • 45
Gino Pensuni
  • 304
  • 3
  • 14
  • Need extra indentation there, fixed. – Jarod42 Jan 16 '19 at 14:19
  • 1) Documentation explains the behavior of [`std::distance`](https://en.cppreference.com/w/cpp/iterator/distance) quite well, in my opinion. 2) Documentation, also explains the [`ptrdiff_t`](https://en.cppreference.com/w/cpp/types/ptrdiff_t), and it's difference from `int`. 3) "Easier" is opinionated concept. `std::string` has [`std::string::find`](https://en.cppreference.com/w/cpp/string/basic_string/find), however. – Algirdas Preidžius Jan 16 '19 at 14:20
  • Thank you @Jarod42 :D you are my hero :D – Gino Pensuni Jan 16 '19 at 14:20
  • @AlgirdasPreidžius isn't your first sentence also an opinionated concept, that the documentation explains it quite well? (; – Gino Pensuni Jan 16 '19 at 14:22
  • @HardCodedCoder Since you didn't come up with questions, about what is unclear in the wording, presented in the documentation, I'll continue to assume that it explains it well. – Algirdas Preidžius Jan 16 '19 at 14:24
  • 1
    You can still use in C++ : std::string text = "Hello world"; auto index = text.find("d"); It's as easy as in C# – Gojita Jan 16 '19 at 14:37

0 Answers0