2

The title says it all. I see that there is a difference between using vec.begin() and &vec[0] as former returns a std::vector<T>::iterator and latter returns a pointer to an object of type T. The book I am currently reading (The C++ Primer, 5th Edition) suggests that you use the std::begin() function in the iterator header to get the first element of an array. However, since this function also just returns a pointer, wouldn't be using the variable name as a pointer to the first element be quicker / more efficient?

Edit This question is not a duplicate as marked. I am not asking for the difference between the container member methods and the ones from the C-Library. Let me clarify my problem:

int numbers[] = {0, 1, 2};
// is there any major difference between
int first = *numbers;
// and
int first1 = *std::begin(numbers);
Niklas Vest
  • 872
  • 6
  • 20
  • If you want to get the first element of a container, some of them offer a `front()` methods. – François Andrieux Jun 26 '17 at 16:47
  • 2
    To address your question about _efficiency_ at the end (which IMO isn't specifically discussed in the duplicate): no, just using a pointer is neither quicker nor more efficient. The function is inlined, and no compiler will generate anything other than a raw pointer access (unless you explicitly disable normal optimization and force it to emit a call instead). – Useless Jun 26 '17 at 16:48
  • @πάνταῥεῖ Well, OP's question seems more focused on the difference between iterators and pointers... – Bob__ Jun 26 '17 at 16:51
  • @πάνταῥεῖ Your first response was simply useless and i couldn't decide whether to use "offensive" or "not constructive" as reason for flagging your answer. I was not asking for the difference between iterators and pointers. You would do me a favor by unmarking my question as duplicate.. – Niklas Vest Jun 26 '17 at 16:54
  • @NiklasVest There's even more: https://stackoverflow.com/questions/31128055/what-is-difference-between-iterators-and-pointers – πάντα ῥεῖ Jun 26 '17 at 16:56
  • @πάνταῥεῖ Again, I am not asking for the difference between iterators and pointers. I am asking for the difference with the built in type, which does not supply an iterator type. – Niklas Vest Jun 26 '17 at 16:58
  • @NiklasVest _"... and the ones from the C-Library"_ There are no _methods_ from the C-Library. C doesn't use methods to realize that. It's intrinsic behavior. – πάντα ῥεῖ Jun 26 '17 at 16:59
  • @πάνταῥεῖ C++ Library, close enough lol. Could you please focus on answering my question instead of nitpicking on details. Can you tell me what the difference between the two statements in my edit is or not? If not feel free to leave this thread.. – Niklas Vest Jun 26 '17 at 17:02
  • @NiklasVest I still believe the answer lies in one of those many dupes I linked. The difference may be that the implementaiton of iterators might be just ending up in the (inlined) raw c-style implementation. The advantage to use the functions and iterator semantics from the `std` namespace, that it will work well together with the functions from the standard algorithm library, will strill work if you decide to use a standard container, etc. – πάντα ῥεῖ Jun 26 '17 at 17:10
  • 2
    @Niklas - In your code there is no advantage of `std::begin`. However, if you write generic code using `std::begin(x)` it will work whether `x` is a vector or an array (or a deque, or a list, or a user defined type with a `begin` member). – Bo Persson Jun 26 '17 at 17:15
  • @πάνταῥεῖ I have checked them all and actually 4 out of 5 discuss iterators to an extent I do not require. The one generally asking for the usage of the begin and end functions helped a bit. However, I still wanted to ask you to be a bit less aggressive when you "answer" a question next time. I always try to do my research, sorry if I didn't catch a useful thread. – Niklas Vest Jun 26 '17 at 17:24
  • 1
    @NiklasVest Well, posting a duplicate question isn't inherently bad. Though yours sounded seriously lacking research in it's 1st version. Sorry for my borderline rude 1st comment. Though I think the answer is found in the marked duplicates (you confirmed that yourself). I hope all of your questions and doubts are clarified now. Have an upvote then. – πάντα ῥεῖ Jun 26 '17 at 17:28
  • 1
    `std::begin` was never meant to be used by itself, it's a pair with `std::end`. While using a pointer to the beginning of the array may be simpler, getting a pointer to one past the end of the array is best done with `std::end` to avoid bugs. – Mark Ransom Jun 26 '17 at 18:00
  • @πάνταῥεῖ Your apology is much appreciated! :) The answer concerning the advantages with generic programming was enough for me, which was in some form mentioned in a link you provided. (Had to read that two times though.) – Niklas Vest Jun 26 '17 at 18:19
  • @MarkRansom But then, again, you can simply use `while(arrP != std::end(arr)) {++arrP}` where `int* arrP = arr;`. Right? – Niklas Vest Jun 26 '17 at 18:20
  • @NiklasVest Or you can even use [range based `for()`](http://en.cppreference.com/w/cpp/language/range-for). – πάντα ῥεῖ Jun 26 '17 at 18:34
  • @πάνταῥεῖ Yes but that would hide the details about the pointers I am interested in. – Niklas Vest Jun 27 '17 at 07:46
  • 1
    @NiklasVest Hiding the details is actually the purpose of using those idioms. You don't want them. In short: Abstraction is the advantage. – πάντα ῥεῖ Jun 27 '17 at 07:48

0 Answers0