project1st有什么用处? 在STL中?

发布于 2024-07-10 01:54:03 字数 246 浏览 8 评论 0原文

我在浏览 SGI STL 文档时遇到了 project1st< /代码>。 我理解它的定义,但我很难想象实际用途。

您曾经使用过project1st吗?或者您能想象一个场景吗?

I was browsing the SGI STL documentation and ran into project1st<Arg1, Arg2>. I understand its definition, but I am having a hard time imagining a practical usage.

Have you ever used project1st or can you imagine a scenario?

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

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

发布评论

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

评论(4

雄赳赳气昂昂 2024-07-17 01:54:03

project1st 的一个变体(采用 std::pair,并返回 .first)非常有用。 您可以将其与 std::transform 结合使用,将键从 std::map 复制到 std::vectorK>。 类似地,project2nd 的变体可用于将值从映射复制到 vector

碰巧的是,没有一个标准算法能够真正从 project1st 中受益。 最接近的是partial_sum(project1st),它将所有输出元素设置为第一个输入元素。 它的存在主要是因为 STL 深深地建立在数学集合论的基础上,并且像 project1st 这样的操作是基本的构建块。

A variant of project1st (taking a std::pair, and returning .first) is quite useful. You can use it in combination with std::transform to copy the keys from a std::map<K,V> to a std::vector<K>. Similarly, a variant of project2nd can be used to copy the values from a map to a vector<V>.

As it happens, none of the standard algorithms really benefits from project1st. The closest is partial_sum(project1st), which would set all output elements to the first input element. It mainly exists because the STL is heavily founded in mathematical set theory, and there operations like project1st are basic building blocks.

此岸叶落 2024-07-17 01:54:03

我的猜测是,如果您使用策略模式并且遇到需要传递身份对象的情况,那么这将是一个不错的选择。 例如,可能存在一种情况,算法采用多个此类对象,并且您可能希望其中一个在某种情况下不执行任何操作。

My guess is that if you were using the strategy pattern and had a situation where you needed to pass an identity object, this would be a good choice. For example, there might be a case where an algorithm takes several such objects, and perhaps it is possible that you want one of them to do nothing under some situation.

放飞的风筝 2024-07-17 01:54:03

并行编程。 想象一下这样的情况:两个进程对于给定的计算得出两个有效但不同的结果,并且您需要强制它们相同。 project1st/2nd 提供了一种非常方便的方法来在整个容器上执行此操作,使用以函子作为参数的适当并行调用。

Parallel programming. Imagine a situation where two processes come up with two valid but different results for a given computation, and you need to force them to be the same. project1st/2nd provides a very convenient way to perform this operation on a whole container, using an appropriate parallel call that takes a functor as an argument.

清风夜微凉 2024-07-17 01:54:03

我假设有人对它有实际用途,否则它就不会被写下来,但我对它可能是什么感到一片空白。 据推测,它的用例类似于描述中提到的identity函数,其中不需要真正的处理,但语法无论如何都需要一个函子。

同一页面上的示例建议将其与 std::transform 的两个容器形式一起使用,但如果我没有记错的话,他们使用它的方式在功能上与 相同std::copy,所以我不明白这一点。

对我来说,这看起来像是一个寻找问题的解决方案。

I assume that someone had a practical use for it, or it wouldn't have been written, but I'm drawing a blank on what it might have been. Presumably its use-case is similar to the identity function that the description mentions, where there's no real need for processing but the syntax requires a functor anyway.

The example on that same page suggests using it with the two-container form of std::transform, but if I'm not mistaken, the way they're using it is functionally identical to std::copy, so I don't see the point.

It looks like a solution in search of a problem to me.

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