将元素按升序存储在列表中
目标是,我有多个可用元素列表,并且我希望能够以有序方式将所有这些元素存储到结果列表中。
我想到的一些想法是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会对各个列表进行排序,然后使用 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.
乍一看,您的代码似乎正在对列表进行线性搜索,以便找到插入项目的位置。虽然
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.回答所提出的问题:
是的。使用
std::set
。Answering the question asked:
Yes. Use
std::set
.