相关疑难解决方法(0)

std :: list应该被弃用吗?

根据Bjarne Stroustrup 在他的Going Native 2012主题演讲中幻灯片,在现代硬件中插入和删除是非常低效的:std::list

矢量vs列表

矢量节拍列表大量插入和删除

如果确实如此,还有什么用例std::list?那不应该被弃用吗?

c++ performance containers vector doubly-linked-list

10
推荐指数
2
解决办法
1698
查看次数

是否有类似“默认比较器”的东西?

我编写了一个包装 std::vector 的模板,以确保向量始终排序:

template <typename T> class SortedVector{
public:    
    SortedVector(bool (*comparator)(T,T)=DefaultComparator<T>){
        this->comparator = comparator;
    }
    void insertValue(T newElement){            
        vect.insert(std::lower_bound(
           vect.begin(),vect.end(),newElement,comparator),newElement);
    }
private:
    std::vector<T> vect;
    bool (*comparator)(T,T);
};
Run Code Online (Sandbox Code Playgroud)

我希望能够使用自定义比较器,但在大多数情况下,只需使用T's<运算符就可以了。但是,我没有找到比使用这个更好的方法

template <typename T> bool DefaultComparator(T a,T b){return a<b;}
Run Code Online (Sandbox Code Playgroud)

作为默认参数。

也许这是一个愚蠢的问题...是否有更好的方法可以在不定义我自己的情况下获得相同的结果DefaultComparator

我无法使用 C++11。

c++ compare

4
推荐指数
1
解决办法
3224
查看次数