将元素按升序存储在列表中

发布于 2024-09-13 17:50:57 字数 2586 浏览 2 评论 0原文

目标是,我有多个可用元素列表,并且我希望能够以有序方式将所有这些元素存储到结果列表中。

我想到的一些想法是 a) 将结果保留为集合 (std::set),但 B 树需要时不时地重新平衡。 b) 将所有元素存储在列表中,并在末尾对列表进行排序。

但是,我想,当我们将项目添加到结果列表时,为什么不以排序的方式存储它们呢?

这是我的函数,它负责以排序的方式维护结果。有没有一种有效的方法可以做到同样的事情?

void findItemToInsertAt(std::list<int>& dataSet, int itemToInsert, std::list<int>::iterator& location)
{
    std::list<int>::iterator fromBegin = dataSet.begin();
    std::list<int>::iterator fromEnd = dataSet.end() ;
    // Have two pointers namely end and begin
    if ( !dataSet.empty() )
        --fromEnd;

    // Set the location to the beginning, so that if the dataset is empty, it can return the appropriate value
    location = fromBegin;
    while ( fromBegin != dataSet.end()  )
    {
        // If the left pointer points to lesser value, move to the next element
        if ( *fromBegin < itemToInsert )
        {
            ++fromBegin;
            // If the end is greater than the item to be inserted then move to the previous element
            if ( *fromEnd > itemToInsert )
            {
                --fromEnd;
            }
            else
            {
                // We move only if the element to be inserted is greater than the end, so that end points to the
                // right location
                if ( *fromEnd < itemToInsert )
                {
                    location = ++fromEnd;
                }
                else
                {
                    location = fromEnd;
                }
                break;
            }
        }
        else
        {
            location = fromBegin;
            break;
        }
    }

}

并且,这是函数的调用者

void storeListToResults(const std::list<int>& dataset, std::list<int>& resultset)
{

    std::list<int>::const_iterator curloc;
    std::list<int>::iterator insertAt;

    // For each item in the data set, find the location to be inserted into
    // and insert the item.
    for (curloc = dataset.begin(); curloc != dataset.end() ; ++curloc)
    {
        // Find the iterator to be inserted at
        findItemToInsertAt(resultset,*curloc,insertAt);
        // If we have reached the end, then the element to be inserted is at the end
        if ( insertAt == resultset.end() )
        {
            resultset.push_back(*curloc);
        }
        else if ( *insertAt != *curloc ) // If the elements do not exist already, then insert it.
        {
            resultset.insert(insertAt,*curloc);
        }
    }
}

Goal is, I've multiple lists of elements available, and I want to be able to store all of these elements into a resultant list in an ordered way.

Some of the ideas that comes to my mind are
a) Keep the result as a set (std::set), but the B-tree , needs to rebalanced every now and then.
b) Store all the elements in a list and sort the list at the end.

But, I thought, why not store them in a sorted fashion, as and when we add the items to the resultant list.

Here is my function, that does the job of maintaining the results in a sorted way. Is there an efficient way to do the same?

void findItemToInsertAt(std::list<int>& dataSet, int itemToInsert, std::list<int>::iterator& location)
{
    std::list<int>::iterator fromBegin = dataSet.begin();
    std::list<int>::iterator fromEnd = dataSet.end() ;
    // Have two pointers namely end and begin
    if ( !dataSet.empty() )
        --fromEnd;

    // Set the location to the beginning, so that if the dataset is empty, it can return the appropriate value
    location = fromBegin;
    while ( fromBegin != dataSet.end()  )
    {
        // If the left pointer points to lesser value, move to the next element
        if ( *fromBegin < itemToInsert )
        {
            ++fromBegin;
            // If the end is greater than the item to be inserted then move to the previous element
            if ( *fromEnd > itemToInsert )
            {
                --fromEnd;
            }
            else
            {
                // We move only if the element to be inserted is greater than the end, so that end points to the
                // right location
                if ( *fromEnd < itemToInsert )
                {
                    location = ++fromEnd;
                }
                else
                {
                    location = fromEnd;
                }
                break;
            }
        }
        else
        {
            location = fromBegin;
            break;
        }
    }

}

And, here is the caller of the function

void storeListToResults(const std::list<int>& dataset, std::list<int>& resultset)
{

    std::list<int>::const_iterator curloc;
    std::list<int>::iterator insertAt;

    // For each item in the data set, find the location to be inserted into
    // and insert the item.
    for (curloc = dataset.begin(); curloc != dataset.end() ; ++curloc)
    {
        // Find the iterator to be inserted at
        findItemToInsertAt(resultset,*curloc,insertAt);
        // If we have reached the end, then the element to be inserted is at the end
        if ( insertAt == resultset.end() )
        {
            resultset.push_back(*curloc);
        }
        else if ( *insertAt != *curloc ) // If the elements do not exist already, then insert it.
        {
            resultset.insert(insertAt,*curloc);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半城柳色半声笛 2024-09-20 17:50:58

我会对各个列表进行排序,然后使用 STL 的 list::merge 来创建结果列表。然后,如果列表很大,您可以付费将结果传输到向量。

I would sort the indivual lists and then use STL's list::merge to create the result list. Then, if the list is kind of big, you could pay to transfer the result to a vector.

雨落□心尘 2024-09-20 17:50:57

乍一看,您的代码似乎正在对列表进行线性搜索,以便找到插入项目的位置。虽然 std::set 确实必须平衡其树(我认为它是一个 红黑树)为了保持效率,它很可能会比你建议的更有效。

At a glance, your code looks like it's doing a linear search of the list in order to find the place to insert the item. While it's true that std::set will have to balance its tree (I think it's a Red-Black Tree) in order to maintain efficiency, chances are it'll do so much more efficiently than what you're proposing.

时间海 2024-09-20 17:50:57

回答所提出的问题:

有没有一种有效的方法可以做到这一点?

是的。使用std::set

Answering the question asked:

Is there an efficient way to do the same?

Yes. Use std::set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文