标准:Qt中的唯一类似物?
我浏览了文档,但没有找到。标准:Qt中的唯一类似物?,qt,sorting,stl,qt4,unique,Qt,Sorting,Stl,Qt4,Unique,我浏览了文档,但没有找到。 有什么想法吗?现在我有以下想法: //filter out duplicates: stl algorithm 'unique' would be useful here QList<int> uniqueIDs; qStableSort(res); foreach(int id, res) if ( (uniqueIDs.empty()) || (uniqueIDs
有什么想法吗?现在我有以下想法:
//filter out duplicates: stl algorithm 'unique' would be useful here
QList<int> uniqueIDs;
qStableSort(res);
foreach(int id, res)
if ( (uniqueIDs.empty())
|| (uniqueIDs.back() != id))
uniqueIDs.push_back(id);
swap(res, uniqueIDs);
//过滤掉重复项:stl算法“unique”在这里很有用
QList uniqueIDs;
qStableSort(res);
foreach(int-id,res)
if((uniqueid.empty())
||(uniqueid.back()!=id))
唯一标识。推回(id);
交换(res,uniqueid);
res是用于过滤的输入,但并不满意。您应该能够将
std::unique
应用于QList
的迭代器std::unique
只要求迭代器是前向迭代器(and),而QList
的迭代器似乎满足了这一要求。考虑改用QSet
(当需要它作为列表时,使用QSet::toList
。这就是我创建唯一整数列表的方式:
list=QSet::fromList(list).toList()
不需要添加STD,它可能不是使用非常大的整数的最佳方法,尽管好吧,它还需要使用STL支持编译Qt(到目前为止还不需要),set是个好主意。我使用列表的唯一原因是它更容易添加项目(通过呼吸优先搜索可以收集数千个项目)。
mySet.insert(foo)
对我来说似乎并不特别难。