Похожие чаты

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

of saying that they return iterator?

23 ответов

21 просмотр

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

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

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

30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
Чёт не понял, я ж правильной функцией воспользовался чтобы вывести отладочную информацию? но что-то она не ловится
notme
18
Also, why can’t the community have a vote/ say when it comes to initiatives like buybacks. Isn’t the point of crypto decentralisation? Don’t we deserve input as long term supp...
👨🏽‍🦰
13
Привет)) уже кажется эту тему перемусолили, но вот я так и не понял. Я сейчас сижу на 27дюймов 2к мониторе. На Актуальной макоси, если я куплю 27д 4к монитор: - будет ли изобр...
Vladislav Piskunov
16
any reference of this implementation?
BitBuddha
29
Hi guys, any problem with Pulsebrige? Trying to transfer from wETH to ETH. First it tells me to connect my metamask "through mobile app" not desktop. Then I did and confirmed ...
Snowflakecrypto
13
Страшнейшая правда про списки ЦБ. С первых дней жизни P2P сферы, молодые человеки, начитавшись законодательной базы и "внутренних" документов, решили, что им противостоит сер...
Foxcool
3
У меня есть функция где происходит это: write_bit(buffer, 1); write_bit(buffer, 0); write_bit(buffer, 1); write_bit(buffer, 1); write_bit(buffer, 1); w...
~
13
Карта сайта