c ++ unordered_set如何防止从语法级别开始“排序”?

时间:2018-09-15 09:09:39

标签: c++ sorting compilation set unordered-set

我很好奇,如果我在unordered_set上应用“ std :: sort”,将会发生什么。 STL设计是否可以保护unordered_set避免在其上使用std :: sort,如果可以,如何从语法级别实现它?

class Path
{
    List<Node> nodesOfThePath;
    int length;
}

List<Path> paths = findAllLinksRelatedTo(startPoint); //You have to write a function to get the nodes linked to the given point
paths.Sort(); //sort using the length, ascending

while (paths.length > 0)
{
    if (paths[0].getLastestNode() == FinalPoint)
        return (paths[0]); //this is the shortest one

    List<Path> tempPaths = findAllLinksRelatedTo(paths[0].getLastestNode()); //get all nodes related to the lastest one of the shortest actual path or null if none was found
    paths.removeAt(0); //remove the shortest path
    foreach (tempPath in tempPaths)
        paths.Add(tempPath);
    paths.sort();
}
return (null); //no path found

如我所料,它在下面提供了许多错误信息:

#include<algorithm>
#include<iostream>
#include<unordered_set>
using namespace std;
struct E{
    char vertex;
    size_t distance;
    E(char v, size_t d):vertex(v),distance(d){}
    E(const E& e):vertex(e.vertex),distance(e.distance){}
    E& operator=(const E& e){
        vertex = e.vertex;
        distance = e.distance;
        return *this;
    }
};
template<>
struct std::hash<E> {
    std::size_t operator()(const E& e) const {
        return e.vertex;
    }
};  
using edgeSet=unordered_set<E>;
int main(){ 
    edgeSet U;
    while(!U.empty()){
        sort(U.begin(),U.end());//ERROR!
    }   
    return 0;
}

那么,您能否帮助解释STL如何保护这种“排序”,我不太了解此错误信息的含义,该信息表示什么?只想学习技巧。非常感谢。

0 个答案:

没有答案