Transform - 一种变异序列算法
Transform - a mutating sequence algorithm
我知道 C++ 中的 transform
算法是 mutating sequence algorithm
。但我从未见过有人使用 transform
来改变序列。每当我在互联网上搜索示例代码时,我得到的是使用类似于 for_each
算法的转换算法。
请提供一个 link 或例子,我可以在其中理解 mutating sequence
的性质。
编辑:当我经历 This SO question. 时,我更加困惑了
它说 for_each
是一个 non-modifying sequence
算法。所以 我可以 修改元素 for_each
而不是 container.Is 提供的答案不正确的结构。如果for_each
也可以修改元素,我们可以将for_each
替换为transform
,不需要for_each
算法,只是实现可能比较简单。
变异序列算法意味着算法会改变(修改)它所工作的容器。在下面的示例中,类型 std::vector
的容器 foo
被修改。
std::string s("hello");
std::vector<int> foo;
std::transform(s.begin(), s.end(), back_inserter(foo), ::toupper);
std::cout << std::string(foo.begin(), foo.end());
output是"HELLO"
std::for_each
无法做到这一点
是的,提供的答案对我来说是正确的。还要检查 C++ 中的 Nonmodifying Sequence Algorithms and Mutating Sequence Algorithms 列表以验证您的断言。
这是一个简单的例子
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for ( int x : a ) std::cout << x << ' ';
std::cout << std::endl;
std::transform( std::begin( a ), std::end( a ), std::begin( a ),
[]( int x ) { return x * x; } );
for ( int x : a ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
输出为
1 2 3 4 5 6 7 8 9 10
1 4 9 16 25 36 49 64 81 100
同样可以使用算法 std::for_each
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for ( int x : a ) std::cout << x << ' ';
std::cout << std::endl;
std::for_each( std::begin( a ), std::end( a ),
[]( int &x ) { x = x * x; } );
for ( int x : a ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
尽管 std::for_each
被称为非可变算法,但在其描述中却写着
2 Effects: Applies f to the result of dereferencing every iterator in
the range [first,last), starting from first and proceeding to last -
1. [ Note: If the type of first satisfies the requirements of a mutable iterator, f may apply nonconstant functions through the
dereferenced iterator.—end note ]
所以实际上它可以用作可变算法。
std::for_each
和 std::transform
这两种算法都属于 非修改序列操作的范畴 因为它们不改变元素的顺序源序列..
您似乎混合了两个概念:可变算法和非修改序列算法。:)
我知道 C++ 中的 transform
算法是 mutating sequence algorithm
。但我从未见过有人使用 transform
来改变序列。每当我在互联网上搜索示例代码时,我得到的是使用类似于 for_each
算法的转换算法。
请提供一个 link 或例子,我可以在其中理解 mutating sequence
的性质。
编辑:当我经历 This SO question. 时,我更加困惑了
它说 for_each
是一个 non-modifying sequence
算法。所以 我可以 修改元素 for_each
而不是 container.Is 提供的答案不正确的结构。如果for_each
也可以修改元素,我们可以将for_each
替换为transform
,不需要for_each
算法,只是实现可能比较简单。
变异序列算法意味着算法会改变(修改)它所工作的容器。在下面的示例中,类型 std::vector
的容器 foo
被修改。
std::string s("hello");
std::vector<int> foo;
std::transform(s.begin(), s.end(), back_inserter(foo), ::toupper);
std::cout << std::string(foo.begin(), foo.end());
output是"HELLO"
std::for_each
是的,提供的答案对我来说是正确的。还要检查 C++ 中的 Nonmodifying Sequence Algorithms and Mutating Sequence Algorithms 列表以验证您的断言。
这是一个简单的例子
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for ( int x : a ) std::cout << x << ' ';
std::cout << std::endl;
std::transform( std::begin( a ), std::end( a ), std::begin( a ),
[]( int x ) { return x * x; } );
for ( int x : a ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
输出为
1 2 3 4 5 6 7 8 9 10
1 4 9 16 25 36 49 64 81 100
同样可以使用算法 std::for_each
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for ( int x : a ) std::cout << x << ' ';
std::cout << std::endl;
std::for_each( std::begin( a ), std::end( a ),
[]( int &x ) { x = x * x; } );
for ( int x : a ) std::cout << x << ' ';
std::cout << std::endl;
return 0;
}
尽管 std::for_each
被称为非可变算法,但在其描述中却写着
2 Effects: Applies f to the result of dereferencing every iterator in the range [first,last), starting from first and proceeding to last - 1. [ Note: If the type of first satisfies the requirements of a mutable iterator, f may apply nonconstant functions through the dereferenced iterator.—end note ]
所以实际上它可以用作可变算法。
std::for_each
和 std::transform
这两种算法都属于 非修改序列操作的范畴 因为它们不改变元素的顺序源序列..
您似乎混合了两个概念:可变算法和非修改序列算法。:)