修改存储在std :: vector中的每个元素的值。 [英] Modifying the value of each element stored in a std::vector.
问题描述
查看我的代码:
void modify_element(BigType& x)
{
/ *未显示* /
}
BigType modify_element_copy(BigType x)
{
modify_element(x);
返回x;
}
void modify_container(std :: vector< BigType>& v)
{
std :: transform(v.begin(),v.end(),v.begin(),modify_element_copy);
}
我可以通过调用std :: for_each来替换对std :: tranform的调用吗?我的
文档说for_each不能修改元素。这是
modify_container使用std :: for_each而不是std :: transform:
void modify_container(std :: vector< BigType>& v)
{
std :: for_each(v.begin(),v.end(),modify_element);
}
感谢任何帮助。
Look at my code:
void modify_element(BigType &x)
{
/* not shown */
}
BigType modify_element_copy(BigType x)
{
modify_element(x);
return x;
}
void modify_container(std::vector<BigType> &v)
{
std::transform(v.begin(), v.end(), v.begin(), modify_element_copy);
}
Can I replace the call to std::tranform with a call to std::for_each? My
documentation says that for_each must not modify the elements. Here is
modify_container using std::for_each rather than std::transform:
void modify_container(std::vector<BigType> &v)
{
std::for_each(v.begin(), v.end(), modify_element);
}
Any help is appreciated.
推荐答案
" Jason Heyes" < JA ******** @ optusnet.com.au>在留言中写道
news:42 ********************** @ news.optusnet.com.au ...
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:42**********************@news.optusnet.com.au ...
查看我的代码:
void modify_element(BigType& x)
{
/ *未显示* /
}
BigType modify_element_copy(BigType x)
{
modify_element(x);
返回x;
}
void modify_container(std ::向量< BigType>& v)
{
std :: transform(v.begin(),v.end(),v.begin(),modify_element_copy);
}
我可以通过调用std :: for_each来替换对std :: tranform的调用吗?我的
文档说for_each不能修改元素。
这是
modify_container使用std :: for_each而不是std :: transform:
void modify_container(std :: vector< BigType>& v)
{
std :: for_each(v.begin(),v.end(),modify_element);
}
Look at my code:
void modify_element(BigType &x)
{
/* not shown */
}
BigType modify_element_copy(BigType x)
{
modify_element(x);
return x;
}
void modify_container(std::vector<BigType> &v)
{
std::transform(v.begin(), v.end(), v.begin(), modify_element_copy);
}
Can I replace the call to std::tranform with a call to std::for_each? My
documentation says that for_each must not modify the elements.
Here is
modify_container using std::for_each rather than std::transform:
void modify_container(std::vector<BigType> &v)
{
std::for_each(v.begin(), v.end(), modify_element);
}
是的,''for_each()''应该可以正常工作。记住不要尝试使用带有const向量的修改谓词或通过
const_iterators来尝试
(好吧,编译器应该抱怨,如果你是
做无论如何)。
-Mike
Yes, ''for_each()'' should work fine. Just remember not to try
using a modifying predicate with a const vector or via
const_iterators (well, the compiler should complain if you
do anyway).
-Mike
" Mike Wahler" < MK ****** @ mkwahler.net>在消息中写道
新闻:Qe **************** @ newsread3.news.pas.earthli nk.net ...
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:Qe****************@newsread3.news.pas.earthli nk.net...
是的,''for_each()''应该可以正常工作。请记住不要尝试使用带有const向量的修改谓词或通过
const_iterators(好吧,编译器应该抱怨,如果你还是做的话)。
- Mike
Yes, ''for_each()'' should work fine. Just remember not to try
using a modifying predicate with a const vector or via
const_iterators (well, the compiler should complain if you
do anyway).
-Mike
好的,谢谢。
Ok thanks for that.
for_each算法不会修改序列的元素。它
调用范围[First,Last]中每个元素的函数F和
返回输入参数F.此函数不会修改任何
$ b序列中的$ b元素。
The for_each algorithm does not modify the elements of the sequence. It
calls Function F for each element in the range [First, Last) and
returns the input parameter F. This function does not modify any
elements in the sequence.
这篇关于修改存储在std :: vector中的每个元素的值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!