Dmy*_* S. 7 c++ vector c++11 reference-wrapper
我有一个本地std::vector<std::reference_wrapper<T> >
,现在我想返回其元素的真实副本(即std::vector<T>
).有没有比循环更好的方法?
例:
std::vector<T> foobar() {
std::vector<std::reference_wrapper<T> > refsToLocals;
/*
do smth with refsToLocals
*/
std::vector<T> copyOfLocals;
for (auto local : refsToLocals)
copyOfLocals.insert_back(local.get());
return copyOfLocals;
}
Run Code Online (Sandbox Code Playgroud)
看起来,显而易见的方法是std::vector<T>
从以下序列构造一个std::vector<std::reference_wrapper<T>>
:
std::vector<T> foobar() {
std::vector<std::reference_wrapper<T> > refsToLocals;
/* do smth with refsToLocals */
return std::vector<T>(refsToLocals.begin(), refsToLocals.end());
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
557 次 |
最近记录: |