简体   繁体   中英

Use std::vector::begin vs begin(vector)

If I have std::vector (which is a std::vector and will be always a std::vector ).

Is it superior to use std::begin() instead of std::vector::begin() (or the opposite)?

Will there be any performance increase/decrease?

Example:

std::vector<int> foo(100, 5);
std::sort(foo.begin(), foo.end());        // Case 1
std::sort(std:begin(foo), std::end(foo)); // Case 2

对于“正常” std -container类型std::begin(c)其实是一样的c.begin()

My two pence:

(which is a std::vector and will be always a std::vector).

IMHO this is a guarantee that you cannot make now - this argues for the free function form.

Is it superior to use std::begin() instead of std::vector::begin() (or the opposite)?

Only in the sense that the free function form participates in ADL

Will there be any performance increase/decrease?

Not if you enable the optimiser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM