简体   繁体   中英

C++: std::vector::resize vs. “normal” allocation

In the code example for std::transform , there is an example with code like this:

std::vector<int> foo;
std::vector<int> bar;

//add some elements to foo

bar.resize(foo.size());

//store elements transformed from foo's in bar

And I was wondering whether

std::vector<int> bar;    
bar.resize(foo.size());

was any different from

std::vector<int> bar(foo.size());

and if so, how?

没有什么区别,但后者是一点点更高效和简洁。

No, there's no difference. At least not in the way you show it (with no insertions into foo between the definition of bar and the call to resize ).

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