std::list 和 std::map 的通用算法?

发布于 2024-08-14 13:13:34 字数 601 浏览 2 评论 0原文

我有一个感兴趣的类别(称之为 X)。
我有一个 std::list(称之为L)。
我有一个函数(称之为F)。

F(L) 根据检查列表中每个 X 的内部状态的算法返回 L 的子集(std::list)。

我正在向我的应用程序添加 std::map; (称之为 M),并且我需要定义 F(M) 以与 F(L) 相同的方式操作 - 也就是说,F(M) 必须返回 std::list;同样,通过检查图中每个 X 的内部状态来确定。

作为一个自称懒惰的程序员,我立即发现该算法在逻辑上是相同的,并且每种数据类型(std::list 和 std::map)都是可迭代模板。我不想两次维护相同的算法,但我不知道如何继续前进。

一种方法是从 F(M) 中获取 X*(即键值映射中的“值”),将它们放入 std::list中,然后进行处理传递给 F(std::list),传递 return std::list;回来通过。我不明白这怎么会是唯一的方法。

我的问题:如何在一个地方维护核心算法,但保留迭代序​​列或一对关联容器的值的能力?

谢谢!

I have a class of interest (call it X).
I have a std::list<X*> (call it L).
I have a function (call it F).

F(L) returns a subset of L (a std::list<X*>) according to an algorithm that examines the internal state of each X in the list.

I'm adding to my application a std::map<int,X*> (call it M), and I need to define F(M) to operate in the same fashion as F(L) - that is to say, F(M) must return a std::list<X*> as well, determined by examining the internal state of each X in the map.

Being a self-described lazy programmer, immediately I see that the algorithm is going to be [logically] the same and that each data type (the std::list and the std::map) are iterable templates. I don't want to maintain the same algorithm twice over, but I'm not sure how to move forward.

One approach would be to take the X*'s from F(M) (that is, the 'values' from the key-value map), throw them into a std::list<X*>, and punt the processing over to F(std::list<X*>), passing the return std::list<X*>; back through. I can't see how this would be the only way.

My question: How can I maintain the core algorithm in one place, but retain the ability to iterate over either a sequence or the values of a pair associative container?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浮生面具三千个 2024-08-21 13:13:34

首先,除了两者的条件之外的所有内容都可以使用 std::remove_copy_if 来完成。尽管名称为 remove_copy_if,但它不会从原始集合中删除任何内容。我认为如果将其称为 filtered_copy 之类的名称,人们会更容易理解它。它将元素从一个集合复制到另一个集合。对于每个元素,它调用一个谓词,当且仅当该元素的谓词返回 false 时,才会复制该项目。

这让您只剩下一个责任:实现查看每个 X * 的测试函数,并说明是否应该将其排除在您正在制作的副本之外。由于您想以两种不同的方式应用一段逻辑,因此我将该逻辑封装在类的私有函数中。然后可以通过两种方式将其作为类的 operator() 的重载版本提供给外界:

class F { 
    bool do_test(X const *x) const { return x.internal_stuff; }
public:
    bool operator()(X const *x) const { return do_test(x); }

    bool operator()(std::pair<int, X const *> const &p) const { 
        return do_test(p.second);
    }
};

因为 operator()(X const *) 是一个纯粹的 thunk对于do_test(),您可能想摆脱它,但在我看来,这可能弊大于利。

无论如何,这都会将您的逻辑完全留在一个地方(F::do_test)。它还提供了一个简单、一致的语法,用于创建 liststd::map 的过滤副本:

std::list<X *> result;   
std::remove_copy_if(coll.begin(), coll.end(), std:back_inserter(result), F());

最后一点:std::list 可能是现有的最过度使用的集合。虽然它确实有其用途,但它们确实非常罕见。 std::vectorstd::deque 通常非常更好。

First, all but the condition for both can be done with std::remove_copy_if. Despite the name, remove_copy_if, doesn't remove anything from the original collection. I think people would understand it more easily if it was called something like filtered_copy. It copies elements from one collection to another. For each element, it calls a predicate, and the item gets copied if and only if the predicate returns false for that element.

That leaves you with only one responsibility: to implement the test function that looks at each X *, and says whether it should be left out of the copy you're making. Since you have one piece of logic you want to apply in two different ways, I'd encapsulate the logic in a private function of a class. The two ways it can then be supplied to the outside world as overloaded versions of operator() for the class:

class F { 
    bool do_test(X const *x) const { return x.internal_stuff; }
public:
    bool operator()(X const *x) const { return do_test(x); }

    bool operator()(std::pair<int, X const *> const &p) const { 
        return do_test(p.second);
    }
};

Since operator()(X const *) is a pure thunk to do_test(), you might want to get rid of it, but IMO that would probably do more harm than good.

In any case, this leaves your logic entirely in one place (F::do_test). It also gives a simple, consistent syntax for creating a filtered copy of either a list<X *> or a std::map<int, X *>:

std::list<X *> result;   
std::remove_copy_if(coll.begin(), coll.end(), std:back_inserter(result), F());

As a final note: std::list is probably the most over-used collection in existence. While it does have its uses, they're really quite rare. std::vector and std::deque are very frequently better.

鲸落 2024-08-21 13:13:34

编写一个接受两个前向迭代器作为参数(开始和结束)的函数,然后该函数仅测试迭代器的值,如果通过测试,则将其添加到列表中,递增迭代器,并测试它是否未达到end(如果是则中断。)

然后您只需调用该函数并向其传递集合的 begin() 和 end() 迭代器。

Write a function that accepts two forward iterators as it's parameters ( the beginning and end), then the function just tests the iterator's value, adding it to the list if it passes the test, increments the iterator, and tests that it doesn't reach the end (break if it does.)

Then you just call the function and pass it the begin() and end() iterators of the collections.

和我恋爱吧 2024-08-21 13:13:34

怎么样:

template<typename T>
struct func {
    std::list<T>& r;
    func(std::list<T>& r_) : r(r_) {}

    bool algorithm(const T& t) {
        return t<5; // obviously meant to be replaced :)
    }

    void operator()(const T& t) {
        if (algorithm(t)) r.push_back(t);
    }

    void operator()(const std::pair<int, T>& t) {
        if (algorithm(t.second)) r.push_back(t.second);
    }

};

template<typename T, typename ForwardIterator>
std::list<T> subset(ForwardIterator begin, ForwardIterator end) {
    std::list<T> r;
    std::for_each(begin, end, func<T>(r));
    return r;
}

How about something like:

template<typename T>
struct func {
    std::list<T>& r;
    func(std::list<T>& r_) : r(r_) {}

    bool algorithm(const T& t) {
        return t<5; // obviously meant to be replaced :)
    }

    void operator()(const T& t) {
        if (algorithm(t)) r.push_back(t);
    }

    void operator()(const std::pair<int, T>& t) {
        if (algorithm(t.second)) r.push_back(t.second);
    }

};

template<typename T, typename ForwardIterator>
std::list<T> subset(ForwardIterator begin, ForwardIterator end) {
    std::list<T> r;
    std::for_each(begin, end, func<T>(r));
    return r;
}
伴我心暖 2024-08-21 13:13:34

一种解决方案是将算法从两个函数中移出,这些函数只需迭代其容器,然后调用 algo 函数来确定特定项是否属于返回列表。

One solution would be to shift the algorithm out of both functions, and those functions simply iterate over their container, and call the algo function to determine whether a specific item belongs in the return list or not.

断舍离 2024-08-21 13:13:34

您可能是对的,您建议的方法不是唯一的解决方案,但它可能是最容易正确编写和理解的。如果您正在编写生产代码,我肯定会从那里开始。仅在需要时分析代码并变得更高级。

在探索其他选项时,您可以看看 boost::bind。我收到这个答案 当我不久前尝试做类似的事情时。而且我认为 std::tr1::bind 基本上是相同的,所以如果你没有 Boost,你应该能够替换 TR1 版本。

You're probably right that your suggested method isn't the only solution, but it's likely the easiest to write correctly and understand. If you're writing production code, I would definitely start there. Profile the code and get fancier only if you need to.

In exploring other options, you might take a look at boost::bind. I received this answer when I was trying to do something similar a while back. And I think std::tr1::bind is basically the same, so you should be able to substitute the TR1 version if you don't have Boost.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文