What is the optimal (from code readability and semantics) way to get a pointer to all std::vector elements so it can be used as usual array? I used to use:
void Func( int* )
...
std::vector<int> x(1000);
Func(&(x[0]));
but i wonder is there any prettier way to do this? x.begin() returns vector::iterator.
x.data()
在C ++ 11或&x.front()
在C ++ 03
Aside from Dave's answers, you can leave out the parentheses:
int * xP = &x[0];
Which is really as short as it's going to get.
您也可以这样编写: int * xp = &x.front()
,但是对此没有最佳解决方案,编译器应将您编写的所有内容均优化为相同的结果。
可以保证向量在C ++ 11中以及从vc ++ 6.0到vc ++ 2012的平台中具有连续的存储。
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.