简体   繁体   English

C ++ STL算法在列表中添加元素

[英]C++ STL algorithms to add element in list

I want to know if anyone has a quick way for adding an element to a std::list<T*> if the element is not already in it. 我想知道是否有人可以将元素添加到std::list<T*>的快速方法(如果该元素尚不在其中)。

It's a generic function and I can not use loops so something like this 这是一个通用函数,我不能使用循环,所以像这样

template <class T>
bool Class<T>::addElement(const T* element)
{
    for (list<T*>::iterator it = list_.begin(); it != list_.end(); it++)
    {
        if (element == *it)
            return false;
    }
    list_.push_back(element);
    return true;
}

Is not ok because of the loop. 由于循环不好。 Does anyone have ideas? 有人有想法吗?

Why is what you have "not ok"? 为什么会有“不正常”的情况? Looks perfectly fine and readable to me (modulo missing typename ). 对我来说看起来很好并且可读(模数缺少typename )。

If you really don't want to use a loop, you can accomplish the same by using the algorithm to does precisely that loop: std::find : 如果您确实不想使用循环,则可以通过使用算法精确执行该循环来实现相同目的: std::find

template <class T>
bool Class<T>::addElement(const T* element)
{
    if (std::find(list_.begin(), list_.end(), element) != list_.end()) {
        return false;
    }

    list_.push_back(element);
    return true;
}

If you can add other members to your class, you could add an index such as a std::unordered_set . 如果可以将其他成员添加到类中,则可以添加索引,例如std::unordered_set That container stores a list of unique values, and can be searched for specific values in O(1) complexity, which implies that no full-loop search is done by the implementation for checking if the value already exists. 该容器存储一个唯一值列表,并且可以以O(1)复杂度搜索特定值,这意味着该实现不执行完整循环搜索来检查该值是否已存在。 It will stay fast even if you have a lot of values already stored. 即使您已经存储了很多值,它也会保持快速运行。

With std::list, using library functions such as std::find will avoid explicitely writing a loop, but the implementation will perform the loop and this will be slow when a lot of values are already stored (O(n) complexity) 使用std :: list时,使用诸如std::find类的库函数将避免显式地编写循环,但是实现将执行该循环,并且在已存储大量值的情况下这样做会很慢(O(n)复杂度)

You can use intrusive list instead of the std::list. 您可以使用侵入式列表而不是std :: list。 In this case each element in the list keeps its node data, so you can just query that data to find out if the element is already in the list. 在这种情况下,列表中的每个元素都会保留其节点数据,因此您只需查询该数据即可确定该元素是否已在列表中。 The disadvantage is that all elements in this list must be able to provide such data, and you can't put in such lists, for example, integer or boolean elements. 缺点是此列表中的所有元素都必须能够提供此类数据,并且您不能放入此类列表中,例如,整数或布尔元素。

If you still need the std::list and/or the elements can be of any type, then the only way of fast queryng whether the element already exists in the list is to use an index. 如果仍然需要std :: list和/或元素可以是任何类型,那么快速查询列表中元素是否已存在的唯一方法是使用索引。 The indexes can be stored in separate std::unordered_set for fast lookups. 索引可以存储在单独的std :: unordered_set中,以进行快速查找。 You can use for indexes either the list's values "as is" or calculate the indexes using any custom function. 您可以“按原样”使用列表的值作为索引,也可以使用任何自定义函数来计算索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM