我不希望這STL的容器,但我認爲這不是很難將其轉換爲Qt的容器
包括
#include <list>
#include <set>
#include <iostream>
#include <algorithm>
struct MyData {
int id;
std::string name;
};
struct MyDataSortComparator {
bool operator()(const MyData & left, const MyData & right) {
if (left.name < right.name) {
return true;
}
else if (left.name > right.name) {
return false;
}
return left.id > right.id;
}
};
struct MyDataSetComparator {
bool operator()(const MyData & left, const MyData & right) {
return left.name < right.name;
}
};
int main() {
std::list<MyData> theList = {
{ 1, "Dickson" },
{ 2, "Dickson" },
{ 3, "Dickson" },
{ 2, "borisbn" },
{ 1, "borisbn" }
};
std::set< MyData, MyDataSetComparator > theSet;
theList.sort(MyDataSortComparator());
std::for_each(theList.begin(), theList.end(), [](const MyData & data) {
std::cout << data.id << ", " << data.name << std::endl;
});
std::for_each(theList.begin(), theList.end(), [&theSet](const MyData & data) {
theSet.insert(data);
});
std::cout << "-------------------" << std::endl;
std::for_each(theSet.begin(), theSet.end(), [](const MyData & data) {
std::cout << data.id << ", " << data.name << std::endl;
});
}
http://liveworkspace.org/code/wOFnM $ 5
使用標準的C++庫功能(無論是'std :: list :: unique'還是一般的'std :: unique'函數)意味着你必須將數據複製到一個標準容器,然後將它們複製回'QList'。 –