C++ 在ref/cref范围内同时迭代
我有一个接受两个整数向量的函数。第一个向量作为引用传递,第二个向量作为常量的引用传递。我想同时迭代两个向量,并更新第一个向量。下面是这样的例子:C++ 在ref/cref范围内同时迭代,c++,c++11,boost,C++,C++11,Boost,我有一个接受两个整数向量的函数。第一个向量作为引用传递,第二个向量作为常量的引用传递。我想同时迭代两个向量,并更新第一个向量。下面是这样的例子: #include <iostream> #include <vector> #include <boost/foreach.hpp> #include <boost/range/combine.hpp> void foo(std::vector<int>& a, std::vecto
#include <iostream>
#include <vector>
#include <boost/foreach.hpp>
#include <boost/range/combine.hpp>
void foo(std::vector<int>& a, std::vector<int> const& b)
{
boost::tuple<int&, int const&> x;
BOOST_FOREACH(x, boost::combine(a,b)) {
int& v1 = x.get<0>();
int const& v2 = x.get<1>();
v1 = v1 + v2 + 5;
}
}
int main(int argc, char **argv)
{
std::vector<int> a(3,10);
std::vector<int> b(3,10);
foo(a,b);
for (int v : a) {
std::cout << v << std::endl;
}
return 0;
}
#包括
#包括
#包括
#包括
void foo(std::vector&a,std::vector const&b)
{
boost::tuplex;
增压器(x,增压器::联合收割机(a,b)){
int&v1=x.get();
int const&v2=x.get();
v1=v1+v2+5;
}
}
int main(int argc,字符**argv)
{
std::载体a(3,10);
std::载体b(3,10);
foo(a,b),;
对于(INTV:a){
std::cout至少如果我正确地阅读了Boost的内容,那么您似乎想要类似的东西:
std::transform(a.begin(), a.end(),
b.begin(),
a.begin(),
[] (int x, int y) { return x + y + 5; });
目前,它使用的是C++11。如果您在没有C++11的情况下需要它,毫无疑问,您可以使用Boost Lambda来完成同样的事情,或者您可以自己编写一个函数对象:
struct combine {
int operator()(int x, int y) { return x + y + 5; }
};
void foo(std::vector<int>& a, std::vector<int> const & b)
{
std::transform(a.begin(), a.end(),
b.begin(),
a.begin(),
combine());
}
结构组合{
int运算符()(int x,int y){返回x+y+5;}
};
void foo(std::vector&a,std::vector const&b)
{
std::transform(a.begin(),a.end(),
b、 begin(),
a、 begin(),
combine());
}
还要注意的是,std::vector&const b
是不正确的。您几乎可以肯定希望使用:std::vector const&b
。在和之后加上const
,表示引用本身是const
。将其移到前面意味着引用的是const
rmer毫无意义(您无法将const
应用于引用;只要const引用的概念有任何意义,每个引用都始终是const)。至少如果我正确阅读了Boost内容,您似乎想要类似于:
std::transform(a.begin(), a.end(),
b.begin(),
a.begin(),
[] (int x, int y) { return x + y + 5; });
目前,它使用的是C++11。如果您在没有C++11的情况下需要它,毫无疑问,您可以使用Boost Lambda来完成同样的事情,或者您可以自己编写一个函数对象:
struct combine {
int operator()(int x, int y) { return x + y + 5; }
};
void foo(std::vector<int>& a, std::vector<int> const & b)
{
std::transform(a.begin(), a.end(),
b.begin(),
a.begin(),
combine());
}
结构组合{
int运算符()(int x,int y){返回x+y+5;}
};
void foo(std::vector&a,std::vector const&b)
{
std::transform(a.begin(),a.end(),
b、 begin(),
a、 begin(),
combine());
}
还要注意的是,std::vector&const b
是不正确的。您几乎可以肯定希望使用:std::vector const&b
。在和之后加上const
,表示引用本身是const
。将其移到前面意味着引用的是const
rmer毫无意义(您不能将const
应用于引用;只要const-reference的概念有意义,每个引用都是const)。您没有充分利用C++11的类型推断。此外,在对整数进行迭代时,通过引用访问它们是毫无意义的
您的代码没有编译的原因有两个:1.您不能使用引用默认构造元组;2.当它有常量引用时,您不能更改其值。boost::tuple x;
无论如何都是不必要的
#include <iostream>
#include <vector>
#include <boost/range/combine.hpp>
void foo(std::vector<int>& a, std::vector<int> const& b)
{
for(auto x : boost::combine(a,b))
x.get<0>() += x.get<1>() + 5;
}
int main()
{
std::vector<int> a(3,10);
std::vector<int> b(3,10);
foo(a,b);
for (auto v : a)
std::cout << v << std::endl;
}
#包括
#包括
#包括
void foo(std::vector&a,std::vector const&b)
{
用于(自动x:boost::联合收割机(a、b))
x、 get()+=x.get()+5;
}
int main()
{
std::载体a(3,10);
std::载体b(3,10);
foo(a,b),;
用于(自动v:a)
std::cout您没有充分利用C++11的类型推断。另外,当对整数进行迭代时,通过引用访问它们是毫无意义的
您的代码没有编译的原因有两个:1.您不能使用引用默认构造元组;2.当它有常量引用时,您不能更改其值。boost::tuple x;
无论如何都是不必要的
#include <iostream>
#include <vector>
#include <boost/range/combine.hpp>
void foo(std::vector<int>& a, std::vector<int> const& b)
{
for(auto x : boost::combine(a,b))
x.get<0>() += x.get<1>() + 5;
}
int main()
{
std::vector<int> a(3,10);
std::vector<int> b(3,10);
foo(a,b);
for (auto v : a)
std::cout << v << std::endl;
}
#包括
#包括
#包括
void foo(std::vector&a,std::vector const&b)
{
用于(自动x:boost::联合收割机(a、b))
x、 get()+=x.get()+5;
}
int main()
{
std::载体a(3,10);
std::载体b(3,10);
foo(a,b),;
用于(自动v:a)
std::cout为什么要给你的问题c++11加上标签并使用boost元组和foreach?为什么要把第二个作为const引用传递?第三,你能不能放一个我们可以编译和运行的代码?或者在最坏的情况下,我们可以编辑以便它可以编译?1.删除了c++11;2.没有特别的原因,因为b没有修改,所以它被传递了作为const(尝试了解我所问的是否可行),3.希望您能编译显示问题的示例。@Chiel C++11标记是可以的,因为代码使用范围等。当然,大多数boost使用是不必要的,但您应该更清楚这是您的意思。没有必要混淆问题。为什么要标记问题C++11并使用boost元组和foreach?为什么要通过sec第二个是常量引用?第三个是,你能放一个我们可以编译和运行的代码吗?或者在最坏的情况下,我们可以编辑它以便它可以编译吗?1.删除了c++11;2.没有特别的原因,因为b没有被修改,所以它被作为常量传递(尝试了解我所问的是否可行),3.希望您能编译显示该问题的示例。@Chiel C++11标记可以,因为代码使用range for etc。当然,大多数boost的使用是不必要的,但您应该更清楚这是您的意思。没有必要混淆问题。谢谢您的时间。现在有意义了。我同意我不会将其与int一起使用。实际代码是using更复杂的结构,但为了简单起见,我只是将问题语句简化为使用整数向量。谢谢您的时间。现在它有意义了。我同意我不会将它用于整数。实际代码使用更复杂的结构,但为了简单起见,我只是将问题语句简化为使用整数向量。