English 中文(简体)
A. A Algorithm或密码,用于在C++按价值分类的清单中获取元素的矿石状况
原标题:What’s An Algorithm or code for the obtaining ordinal position of an element in a list sorted by value in c++

这与https://stackoverflow.com/questions/1746402/whats-a-qt-or- open-source-c-template-for-ordinal-sorting”相类似。

我将保留一份价值清单。 我将把具有任意价值的物品列入清单。 我每次加上价值,都想确定名单上的级别(即第1、第2、第1000次)。 实现这一目标的最有效的数据结构和算法是什么? 显然有许多算法可以允许你这样做,但我看不出利用简单的STL或QT模板功能来轻易做到。 理想的情况是,我想知道现有的开放源C++图书馆或能够这样做的样本代码。

我可以想象,如何为此目的修改“B-tree”或类似的算法,但似乎同样容易。

www.un.org/Depts/DGACM/index_spanish.htm Edit3:

迈克·塞穆里非常清楚地证实,正如我在原岗位上所写的那样,用简单的STL来完成这项任务确实没有任何办法。 因此,Im 寻找一个好的树苗、平衡的树林或类似的开放源地块(++)模板,可以在不作修改或作最小的修改的情况下完成——Pavel Shved显示,这是可能的,但我更愿意执行一个平衡的树木。

(历史应当表明,我为修改Mathieu的法典以O(log N)使用 make(heap)作出了不成功的努力。)

<>4>:

我仍然向Pavel表示,“树木能够提供<><>>a>的解决方案”,我必须提到,在不执行custombtree c++模板的情况下实现这种功能的最简单方式是

最佳回答

Bin树与这.树一样。 其修改也是容易的:在每一节点都保持其子树的节点。

在你插入一门 no子后,再次通过从根基走到那代。 重新更新指数:

if (traverse to left subtree)
  index = index_on_previous_stage;
if (traverse to right subtree)
  index = index_on_previous_stage + left_subtree_size + 1;
if (found)
  return index + left_subtree_size;

这将需要O(log N)时间,就像插入。

问题回答

我认为,你可以在此读到<代码>。 它规定,在添加价值的地方,你的行为也归还其位置。 从这个职位上看,你可以取得指数。 例如:

std::set<int> s;
std::pair<std::set<int>::iterator, bool> aPair = s.insert(5);
size_t index = std::distance(s.begin(), aPair.first) ;

请注意:名单插入(it, Value)成员功能使一名主持人返回新插入的内容。 是否能够帮助?

If, as you say in one of your comments, you only need an approximate ordinal position, you could estimate this from the range of values you already have - you only need to read the first and last values in the collection in constant time, something like this:

multiset<int> values;

values.insert(value);
int ordinal = values.size() * (value - values.front()) /
                              (values.back()-values.front());

为了改进近似性,你可以跟踪统计特性(表面和差异,以及可能更准确的顺序),因为你把这些价值加在一起。 这将是持续时间。 这里,你可能做的一件事情含糊不清:

class SortedValues : public multiset<int>
{
public:
    SortedValues() : sum(0), sum2(0) {}

    int insert(int value)
    {
        // Insert the value and update the running totals
        multiset<int>::insert(value);
        sum += value;
        sum2 += value*value;

        // Calculate the mean and deviation.
        const float mean = float(sum) / size();
        const float deviation = sqrt(mean*mean - float(sum2)/size());

        // This function is left as an exercise for the reader.
        return size() * EstimatePercentile(value, mean, deviation);
    }

private:
    int sum;
    int sum2;
};

如果你想要平坐,你想要一个模仿<代码>的集装箱。 RandomAccessContainer 概念......基本上,<代码>载:vector。

在<代码>以下的类型上操作: 速成/代码”相对较快,你可以取得你希望使用<代码>std:下限/<>或std:upper_限值/代码>,如果你想要多数值,可以自行决定收回所有同等价值,一种很好的方式是使用<代码>std:平等_range/code>,该编码基本上使你产生与应用<下限/>代码>和<上下限/代码>和<>上下限/代码>相同的结果,但具有更复杂性。

现在,就餐厅而言,大新闻是<代码>std:distance,作为Random AccessIterator模型的奥(1)复杂性。

typedef std::vector<int> ints_t;
typedef ints_t::iterator iterator;

ints_t myInts;

for (iterator it = another.begin(), end = another.end(); it != end; ++it)
{
  int myValue = *it;
  iterator search = std::lower_bound(myInts.begin(), myInts.end(), myValue);
  myInts.insert(search, myValue);
  std::cout << "Inserted " << myValue << " at "
            << std::distance(myInts.begin(), search) << "
";
  // Not necessary to flush there, that would slow things down
}


// Find all values equal to 50
std::pair<iterator,iterator> myPair =
    std::equal_range(myInts.begin(), myInts.end(), 50);
std::cout << "There are " << std::distance(myPair.first,myPair.second)
          << " values  50  in the vector, starting at index "
          << std::distance(myInts.begin(), myPair.first) << std::endl;

是否方便?

std:down_spanish,std:upper_spanish<>/code> and std: Equal_range have a O(log(n) diversity and std:distance has a O(1) diversity, so all there is well effective...

EDIT:如评论中所强调的;> 插入实际上为O(n),因为你不得不将内容移至周围。

为什么你们需要普通立场? 当你在名单上增列另一个项目时,名单后面其他项目的晚期立场就会改变,因此,在你做一个插入时,似乎很难找到其次职。

简单地把元素与病媒、类型联系起来,然后用双双双双双双双向搜索,以找到其位置,可能更好,但这取决于你真正努力达到的目标。

if you have an iterator that you want to find the index of then use std::distance, which is either O(1) or O(n) depending on the container, however the O(1) containers are going to have O(n) inserts so overall you are looking at an O(n) algorithm with any stl container.

正如其他人所说的那样,这并不直接明显为什么有用?





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...