你好,
我正在尝试调整现有代码以适应 boost::variant。这个想法是将 boost::variant 用于异构 vector 。问题是其余代码使用迭代器访问 vector 的元素。有没有办法将 boost::variant 与迭代器一起使用?
我试过了
typedef boost::variant<Foo, Bar> Variant;
std::vector<Variant> bag;
std::vector<Variant>::iterator it;
for(it= bag.begin(); it != bag.end(); ++it){
cout<<(*it)<<endl;
}
但是没有用。
编辑:感谢您的帮助!但在我的设计中,我需要从列表中获取一个元素并将它传递给代码的其他部分(这可能很麻烦,因为我使用的是 GSL)。使用迭代器的想法是我可以将迭代器传递给一个函数,该函数将对从该特定元素返回的数据进行操作。我看不到如何使用 for_each 来做到这一点。我需要做类似的事情:
for(it=list.begin(); it!=list.end();++it) {
for(it_2=list.begin(); it_2!=list.end();++it_2) {
if(it->property() != it_2->property()) {
result = operate(it,it_2);
}
}
}
谢谢!
最佳答案
当然有。取消引用迭代器自然会产生 boost::variant<...>引用或常量引用。
然而,这确实意味着其余代码应该是变体感知的。值得注意的是使用 boost::static_visitor对变体执行操作。
编辑:
简单!
struct Printer: boost::static_visitor<>
{
template <class T>
void operator()(T const& t) const { std::cout << t << std::endl; }
};
std::for_each(bag.begin(), bag.end(), boost::apply_visitor(Printer());
请注意编写访问者如何自动为 STL 算法生成谓词,miam!
现在,对于返回值的问题:
class WithReturn: boost::static_visitor<>
{
public:
WithReturn(int& result): mResult(result) {}
void operator()(Foo const& f) const { mResult += f.suprise(); }
void operator()(Bar const& b) const { mResult += b.another(); }
private:
int& mResult;
};
int result;
std::for_each(bag.begin(), bag.end(), boost::apply_visitor(WithReturn(result)));
编辑 2:
这很容易,但确实需要一些指导:)
首先,我们注意到有两种不同的操作:!=和 operate
struct PropertyCompare: boost::static_visitor<bool>
{
template <class T, class U>
bool operator()(T const& lhs, U const& rhs)
{
return lhs.property() == rhs.property();
}
};
struct Operate: boost::static_visitor<result_type>
{
result_type operator()(Foo const& lhs, Foo const& rhs);
result_type operator()(Foo const& lhs, Bar const& rhs);
result_type operator()(Bar const& lhs, Bar const& rhs);
result_type operator()(Bar const& lhs, Foo const& rhs);
};
for(it=list.begin(); it!=list.end();++it) {
for(it_2=list.begin(); it_2!=list.end();++it_2) {
if( !boost::apply_visitor(PropertyCompare(), *it, *it_2) ) {
result = boost::apply_visitor(Operate(), *it, *it_2));
}
}
}
因为这个 if,这里的 each 并不是那么好.如果你能以某种方式分解 if,它就会起作用在 operate虽然。
另请注意,我传递的不是迭代器而是引用。
关于c++ - boost::variant 的迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3002115/
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我在用Ruby执行简单任务时遇到了一件奇怪的事情。我只想用每个方法迭代字母表,但迭代在执行中先进行:alfawit=("a".."z")puts"That'sanalphabet:\n\n#{alfawit.each{|litera|putslitera}}"这段代码的结果是:(缩写)abc⋮xyzThat'sanalphabet:a..z知道为什么它会这样工作或者我做错了什么吗?提前致谢。 最佳答案 因为您的each调用被插入到在固定字符串之前执行的字符串文字中。此外,each返回一个Enumerable,实际上您甚至打印它。试试
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我
我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“
有没有办法让Ruby能够做这样的事情?classPlane@moved=0@x=0defx+=(v)#thisiserror@x+=v@moved+=1enddefto_s"moved#{@moved}times,currentxis#{@x}"endendplane=Plane.newplane.x+=5plane.x+=10putsplane.to_s#moved2times,currentxis15 最佳答案 您不能在Ruby中覆盖复合赋值运算符。任务在内部处理。您应该覆盖+,而不是+=。plane.a+=b与plane.a=
出于某种原因,heroku尝试要求dm-sqlite-adapter,即使它应该在这里使用Postgres。请注意,这发生在我打开任何URL时-而不是在gitpush本身期间。我构建了一个默认的Facebook应用程序。gem文件:source:gemcuttergem"foreman"gem"sinatra"gem"mogli"gem"json"gem"httparty"gem"thin"gem"data_mapper"gem"heroku"group:productiondogem"pg"gem"dm-postgres-adapter"endgroup:development,:t
我最近开始编写Ruby代码,但我对block参数有误解。以下面的代码为例:h={#Ahashthatmapsnumbernamestodigits:one=>1,#The"arrows"showmappings:key=>value:two=>2#ThecolonsindicateSymbolliterals}h[:one]#=>1.Accessavaluebykeyh[:three]=3#Addanewkey/valuepairtothehashh.eachdo|key,value|#Iteratethroughthekey/valuepairsprint"#{value}:#{ke
我是Ruby和这个网站的新手。下面两个函数是不同的,一个在函数外修改变量,一个不修改。defm1(x)x我想确保我理解正确-当调用m1时,对str的引用被复制并传递给将其视为x的函数。运算符当调用m2时,对str的引用被复制并传递给将其视为x的函数。运算符+创建一个新字符串,赋值x=x+"4"只是将x重定向到新字符串,而原始str变量保持不变。对吧?谢谢 最佳答案 String#+::str+other_str→new_strConcatenation—ReturnsanewStringcontainingother_strconc
我正在使用PostgreSQL9.1.3(x86_64-pc-linux-gnu上的PostgreSQL9.1.3,由gcc-4.6.real(Ubuntu/Linaro4.6.1-9ubuntu3)4.6.1,64位编译)和在ubuntu11.10上运行3.2.2或3.2.1。现在,我可以使用以下命令连接PostgreSQLsupostgres输入密码我可以看到postgres=#我将以下详细信息放在我的config/database.yml中并执行“railsdb”,它工作正常。开发:adapter:postgresqlencoding:utf8reconnect:falsedat