【发布时间】:2014-03-18 08:37:29
【问题描述】:
是否有 std/boost 算法来验证向量中的所有向量是否具有相同的大小?并且通过扩展,所有元素的属性都是相同的?
在以下示例中,我使用了我正在寻找的假设std::all_equal:
typedef std::vector<int> Line;
std::vector<Line> lines;
lines.push(Line(10));
lines.push(Line(11));
auto equalLengths = std::all_equal(lines.begin(), lines.end(),
[](const Line& x){ return x.size(); });
(并且通过扩展:
std::vector<MyClass> vec;
auto equal = std::all_equal(std::begin(vec), std::end(vec),
[](const MyClass& x) { return x.property(); });
)
【问题讨论】:
-
你可以自己写函数,不会超过10行。
-
如果您需要一个方形向量,请使用一个向量并在顶部伪造二维索引。
-
@LightnessRacesinOrbit 只是为了迂腐:矩形向量。而且我认为可以有更多的用例,而不仅仅是 2D 矢量。 (它可能只是一个线条容器,其中线条表示为 N 维空间中法线向量的系数,并且 2D 索引不再有意义)否则我同意 Victor。
-
@Xarn:哦,是的,我的意思是矩形,确实。