Похожие чаты

Hello Can I say that std::begin and std::end return address instead

of saying that they return iterator?

23 ответов

24 просмотра

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)

Ali-Ayed / FREE PALESTINE 🇵🇸 Автор вопроса

I find it confusing, actually.

Ali-Ayed / FREE PALESTINE 🇵🇸 Автор вопроса
Ali Ayed / FREE PALESTINE 🇵🇸
I find it confusing, actually.

Like, if I said "This will return an address to", will I get downvoted on Stack Overflow?

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.

Pavel
Not really, you can in most cases work with them a...

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.

Ali-Ayed / FREE PALESTINE 🇵🇸 Автор вопроса

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.

Ali-Ayed / FREE PALESTINE 🇵🇸 Автор вопроса
Azadi
There can be little to much difference between a p...

Yep, that's what I was thinking of: I can dereference and increment it, so I asked: "Is iterator simply an address?".

Ali Ayed / FREE PALESTINE 🇵🇸
Thank you for this explanation. I said that becaus...

std::begin(array) will always return a pointer to the first element. A pointer in an array is also an iterator btw

Ali Ayed / FREE PALESTINE 🇵🇸
Yep, that's what I was thinking of: I can derefere...

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.

Ali-Ayed / FREE PALESTINE 🇵🇸 Автор вопроса
Azadi
An iterator as a (user-defined) type has its own s...

That's because it should be std::array<int, 3> ar. 😅

Ali Ayed / FREE PALESTINE 🇵🇸
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

Ali Ayed / FREE PALESTINE 🇵🇸
That's because it should be std::array<int, 3> ar....

I wish languages would put the array size first before the T.

Ali-Ayed / FREE PALESTINE 🇵🇸 Автор вопроса
Manav | avoid unnecessary messaging me
The template parameters will be deduced so there's...

Cool! I was testing the code in cpp.sh C++11,14, I didn't thought they've added this cool feature.

BSP
I wish languages would put the array size first be...

You can just make an alias in your namespace like template<size_t sz, typename T> using array = std::array<T, sz>;

Ludovic 'Archivist' Lagouardette
You can just make an alias in your namespace like ...

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.

Thomas
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.

Thomas
Why std::array over C array? It just has methods?

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

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта