of saying that they return iterator?
Not really, you can in most cases work with them as you would with pointers in C, but they don't have to contain pointers (e.g. I'm not sure if std::end of an std::list is ever pointing at something for example)
I find it confusing, actually.
Like, if I said "This will return an address to", will I get downvoted on Stack Overflow?
yep because it doesn't do that.
It doesn't return an address. You can never return an address in any meaningful way because an address is just an integer and any attempt to make a pointer point to that address (except in circumstances where you are doing embedded development or kernel development in which case you ignore the standard as you don't care about things like portability and stuff) is considered undefined behavior according to the standard. end() returns an iterator and containers have the leeway to implement an iterator in any conceivable way possible as long as it obeys the interface of the iterator. This means that it doesn't have to be a pointer. GCC implements iterators as a wrapper over pointers btw.
For std::list and std::forward_list an end iterator in GCC is implemented as a wrapper over a pointer pointing to address 0.
There can be little to much difference between a pointer and an iterator, but the most obvious form of an iterator is a pointer (use * to dereference it and ++ to iterate through the array's elements). A pointer returns an address regularly but iterators are implemented and also considered a different thing from a pointer.
Thank you for this explanation. I said that because I was wondering at first whether I can say "std::begin() points to the first element of an array" or not.
Yep, that's what I was thinking of: I can dereference and increment it, so I asked: "Is iterator simply an address?".
std::begin(array) will always return a pointer to the first element. A pointer in an array is also an iterator btw
An iterator as a (user-defined) type has its own specifications different from a pointer or address. E.g., this is not possible: std::array ar {1, 2, 3}; std::cout << begin(ar); Although what exactly happens under the hood is (mostly) dealing with pointers and other stuff, I think you should yet stick to the technical term and say they return iterators only.
That's because it should be std::array<int, 3> ar. 😅
The template parameters will be deduced so there's no need to explicitly state std::array<int, 3> ar { ... } std::array ar { ... } will work just fine
I wish languages would put the array size first before the T.
Cool! I was testing the code in cpp.sh C++11,14, I didn't thought they've added this cool feature.
Template type deduction is dark magic haha
I thought you were kidding there!
You can just make an alias in your namespace like template<size_t sz, typename T> using array = std::array<T, sz>;
Yes, indeed, this would give the desired syntax for std::array. However, the desire for this syntax should be left at that; a desire, a wish, a feature for an alternate universe, or another programming language. At this point in programming history time, this syntax (dimension(s) before type) is simply too niche for C++ (or C, Java, C#, etc) that it would cause more confusion than benefit of any kind.
Why std::array over C array? It just has methods?
std::array offers much more than that. std::array is a value class and can be copied while C arrays decay to a pointer in most cases. std::array doesn't require a size value to be passed around. std::arrays offer bounds checking (albeit with a run time cost) if you so desire.
Right, so nothing crazy
Passing it as argument without decaying it, using the ranges library without needing to wrap it in a span, possible .at() to do bounds checking, carrying intent in the type system that a pointer would not, interfacing with library methods that expect standard or standard-like containers, and all of that with the mild annoyance of needing to type like 10 extra characters
Обсуждают сегодня