是否存在使用std函式轉換不同型別容器的方法?
QSet<QString> res;
QList<QNetworkInterface> allInterfaces = QNetworkInterface::allInterfaces();
for(const auto& interface : allInterfaces){
res.insert(interface.name());
}
uj5u.com熱心網友回復:
std::transform 可以接受不同的容器。您可以在函式簽名中看到它:
template< class InputIt,
class OutputIt,
class UnaryOperation >
OutputIt transform( InputIt first1,
InputIt last1,
OutputIt d_first,
UnaryOperation unary_op );
如您所見,有兩個模板引數InputIt和OutputIt,因此這意味著它們可以不同。
std這是使用容器的示例:
#include <iostream>
#include <vector>
#include <set>
int main()
{
std::set<int> set = {0, 1, 2, 3 };
std::vector<bool> vec(set.size());
std::transform(set.begin(), set.end(), vec.begin(), [](int v){ return v % 2 == 0; });
for(auto&& e : vec){
std::cout << e << " ";
}
std::cout << std::endl;
}
現場示例
使用 Qt 容器應該是這樣的(雖然我無法驗證)
QList<QNetworkInterface> allInterfaces = QNetworkInterface::allInterfaces();
QSet<QString> res(allInterfaces.size());
std::transform(allInterfaces.begin(), allInterfaces.end(), res.begin(), [](const QNetworkInterface& interface){
return interface.name();
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/487711.html
下一篇:Qstring中的雙引號問題
