English 中文(简体)
(a) 列出在范围[a、b]中未排出以下位置的所有数字:
原标题:Find all the numbers in the range [a, b] that are not in the given std::set S

a and b> be integers,a < b。 鉴于<代码>std:set<int> S,是有效的and elegant(最好没有明确的左轮)方式找到和储存(载于vector 页: 1 S。

Solution 1:

 vector<int> v;
 for(int i = a; i <= b; ++i)
 {
     if(S.find(i) == S.end())
     {
        v.push_back(i);
     }         
}

<<>Solution2:

缩略语

解决办法1 含有明确的循环,解决办法2 似乎效率不高(至少在记忆方面)。 你会提出什么建议? 我正在寻找一种明智的STL-ish(boost也是可以接受的)辅助方式来做到这一点。

最佳回答

你可以做像你的解决办法2。 但是,使用boost:irange, 它是数字范围的一个虚拟集装箱。 这样,你就不会有明确的 lo,而是会步不前,不会留下太多的记忆。

使之更快,只包括使用<代码>的下限(<>限值/代码>/<代码>的下限/代码>/上限<<>代码/代码>确定的相关部分:

auto abRange = boost::irange(a,b+1);
std::set_difference(abRange.begin(), abRange.end(), 
                    s.lower_bound(a), s.upper_bound(b), 
                    std::back_inserter(resultVector));

Or using Boost.Range s set_difference:

boost::set_difference(boost::irange(a,b+1),
                      std::make_pair(s.lower_bound(a), s.upper_bound(b)),
                      std::back_inserter(resultVector));
问题回答

下面的一段话可以避开,但我不敢肯定会给你留下什么:

void inSet(int i, int b, vector<int>& v, set<int>& S)
{
   if(S.find(i) == S.end())
        v.push_back(i);

   if(i<b)
        inSet(i+1,b,v,S);
}

// ... snip
vector<int> v;
inSet(a,b,v,S);

Also, is there not a loop putting all the integers [a,b] into a std::set in your solution 2?

http://www.un.org/Depts/DGACM/index_french.htm t 系指<代码>std:set——它简单是指一套逻辑;一组内容。 如果对这两种收集进行分类,你可以简单地读到<条码>_intersection。 这两辆集装箱到第三集装箱。

vector<int> common;
set_intersection(v.begin(), v.end(), s.begin(), s.end(), back_inserter(common));

EDIT:

下面是上述例子。 这使用C++11 lambdas,但如果你没有C++11或能够使用斜体,你可以自行使用ctor子。 指出缺乏明确的住所。

#include <set>
#include <vector>
#include <algorithm>
#include <iterator>
#include <functional>
#include <iostream>
using namespace std;

static const int numbers[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181};
static const size_t num_numbers = sizeof(numbers)/sizeof(numbers[0]);

int main()
{
    /*** GET THE SET ****/
    set<int> s(begin(numbers), end(numbers));
    //copy(&numbers[0], &numbers[num_numbers], inserter(s, s.begin()));

    /*** GET THE NUMBERS TO LOOK FOR **/
    int first = 5, last = 10;
    vector<int> targets;
    generate_n(back_inserter(targets), last-first, [&first]() -> int {
        return first++;
    });

    /*** FIND THE INTERSECTION ***/
    vector<int> common;
    set_intersection(s.begin(), s.end(), targets.begin(), targets.end(), back_inserter(common));

    /*** DUMP RESULTS ***/
    cout << "Intersecton of:
	";
    copy(s.begin(), s.end(), ostream_iterator<int>(cout,"	"));
    cout << "
with:
	";
    copy(targets.begin(), targets.end(), ostream_iterator<int>(cout,"	"));
    cout << "
= = = = = = = =
	";
    copy(common.begin(), common.end(), ostream_iterator<int>(cout,"	"));
    cout << "
";

}

产出是:

Intersecton of:
        0       1       2       3       5       8       13      21      34
55      89      144     233     377     610     987     1597    2584    4181

with:
        5       6       7       8       9
= = = = = = = =
        5       8

您可以从<条码>S.down_有约束力(a)到<条码>,下限(b),并收集你没有发现的所有愤怒:

auto end = S.lower_bound(b);
int seen = a;

for (auto it = S.lower_bound(a); it < end; ++it) {
   for (int i = seen+1; i < *it; ++i)
      v.push_back(i);
   seen = *it;
}

It contains an explicit loop, but somehow you ll have to look at all the integers in [a,b].





相关问题
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 "...

热门标签