不能使用std :: iota与std :: set [英] Cannot use std::iota with std::set

查看:394
本文介绍了不能使用std :: iota与std :: set的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一组数字范围:0,1,2,3,4,...
以下代码无法编译:

I'd like to create a set of a range of numbers: 0, 1, 2, 3, 4, ... The following code fails to compile:

std::set<int> s;
std::iota(s.begin(), s.end(), 0);

,并显示以下错误讯息:

with the following error message:

error C3892: '_First' : you cannot assign to a variable that is const

编译器是VC ++ 2012。同样的代码适用于一个向量。我应该如何使用它与一组?

The compiler is VC++2012. The same code works fine for a vector. How should I use it with a set?

UPDATE

现在我的代码是无意义的,因为没有指定的集合大小。

I can see now that my code is meaningless, because there's no set size specified.

这里有一些关于我的问题的更多细节。

Here are some more details about my problem.

我有一个包含[0,N]范围内的几个数字的集合。在我的应用程序中,我需要为这样的集合计算设置差异多次。 N是固定的。

I have a set containing SOME numbers from [0, N] range. In my application I need to calculate set difference many times for such sets. N is fixed.

让我们说 N = 5 ,第一组是 s1 = {0,3,4} 。我需要计算集合差异 {0,1,2,3,4} \ {0,3,4} == {1,2} 。这个操作应该经常为不同的集合执行,所以我想我可以创建一个所有数字( {0,1,2,3,4}

Let's say N = 5 and the first set is s1 = {0, 3, 4}. I need to calculate set difference {0, 1, 2, 3, 4} \ {0, 3, 4} == {1, 2}. This operation should be performed quite often for different sets, so I thought that I could create a set with all numbers ({0, 1, 2, 3, 4} in this case) and use std::set_difference to calculate those differences.

推荐答案

为了解决你的实际问题: std :: set_difference 与你所期望的 std :: set 。您可以使用任何一对迭代器作为 set_difference 的前两个参数,前提是它们按顺序返回值。它是一个集没有特别的好处。

To solve your actual problem: std::set_difference has less to do with std::set than you might expect. You can use any pair of iterators as the first two parameters of set_difference provided they return the values in order. There is no particular benefit in it being a set.

因此,例如包含值0 ... n的 std :: vector 的begin / end迭代器-1按顺序工作,或者一对 boost :: counting_iterator

So for example the begin/end iterators of a std::vector containing the values 0 ... n-1 in order would work, or a pair of boost::counting_iterator:

std::set result;
std::set_difference(
    boost::counting_iterator<int>(0), boost::counting_iterator<int>(n),
    s1.begin(), s1.end(),
    std::inserter(result, result.end())
);

输出不需要设置,你也可以使用向下与 back_inserter

The output doesn't need to be a set either, you could just as well use a vector with back_inserter.

尝试在集合上使用 iota 没有意义。 iota 通过为范围分配新值来更改范围中包含的值。您不能分配到 set 中的值。

To solve what you asked: it doesn't make sense to try to use iota on a set. iota changes the values contained in a range, by assigning new values to them. You can't assign to the values in a set.

如果您想要一个包含数字 0 ... n-1 ,则:

If you want a set containing the numbers 0 ... n-1, then:

std::set<int> s;
for (int i = 0; i < n; ++i) {
    s.insert(s.end(), i);
}

如果有人告诉你循环是为了wuss,而真正的C ++程序员使用算法,如果你真的需要,你可以得到 iota

If someone told you that loops are for wusses and real C++ programmers use algorithms, then you can get iota involved if you really want:

std::set<int> s;
{
    std::vector<int> vec(n);
    std::iota(vec.begin(), vec.end(), 0);
    s.insert(vec.begin(), vec.end());
}


$ b。所以如果你喜欢算法,以至于你可以嫁给他们,那么你可以达到标准库之外:

Unfortunately that's kind of inefficient. So if you love algorithms so much that you might as well marry them, then you can reach outside the standard libraries:

std::set<int> s(boost::counting_iterator<int>(0), boost::counting_iterator<int>(n));

这篇关于不能使用std :: iota与std :: set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆