简体   繁体   中英

Convert vector of shared_ptr into the vector of raw pointers in C++

I was wondering if there are ways to convert the vector of shared pointers into the vector of raw pointers other than doing it through the loop:

\\vecShared - initial vector of shared pointers
std::vector< double* > vecRaw;

for(unsigned int i=0; i<vecShared .size(); ++i)
    vecRaw.push_back(vecShared[i].get());

If there are other ways, is there an advantage of using those?

Of course

vecRaw.reserve(vecShared.size());
std::transform(vecShared.cbegin(), vecShared.cend(), std::back_inserter(vecRaw),
    [](auto& ptr) { return ptr.get(); });

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