Получение первого элемента vector

Не могу понять в чём разница.

vector<int> a = {1, 2, 3, 4, 5};
cout<<*a.begin(); 
cout<<*begin(a);

Чем отличаются два этих способа получения первого элемента vector.


Ответы (1 шт):

Автор решения: Harry

Да по сути ничем. Вторая вызывает первую. А оптимизатор все это как вызов удаляет...

Как говорится, найдите отличие в коде VC++ 2019:

; 20   :     cout<<*a.begin();

    mov edx, DWORD PTR [rbx]
    lea rcx, OFFSET FLAT:?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A ; std::cout
    call    ??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QEAAAEAV01@H@Z ; std::basic_ostream<char,std::char_traits<char> >::operator<<

; 21   :     cout<<*begin(a);

    mov edx, DWORD PTR [rbx]
    lea rcx, OFFSET FLAT:?cout@std@@3V?$basic_ostream@DU?$char_traits@D@std@@@1@A ; std::cout
    call    ??6?$basic_ostream@DU?$char_traits@D@std@@@std@@QEAAAEAV01@H@Z ; std::basic_ostream<char,std::char_traits<char> >::operator<<

Вот описание:

Returns an iterator to the beginning of the given range.
Returns exactly c.begin(), which is typically an iterator to the beginning of the sequence represented by c. If C is a standard Container, this returns C::iterator when c is not const-qualified, and C::const_iterator otherwise.

→ Ссылка