如果你创建一个单词表,我正在编写代码。它包含一个“单词”和一个“描述”。单词和描述有自己的 vector 。我也在尝试使用 map 进行同样的尝试。
在我尝试查找单词之前,该程序运行良好。该程序只会从描述中提取最后一个词。有没有办法把整个句子变成一个 vector ?
这是我如何写下描述的代码。整个程序代码很长所以我只提重要的东西:
cout<< "Describe your word:"; //Describtion by using vectors
cin>> desc; //Here you enter the decribtion
getline(cin, desc); //So you can have "space" and write a whole sentence.
d.push_back(desc); //Place the describe at the back of the list so it is at the same index as the matching word
这是应该显示单词和描述的代码:
cout<< "Enter a word to lookup:";
cin>> word;
if (find(o.begin(), o.end(), word) !=o.end()) //Lookup if word excist in vector
{
int pos = find(o.begin(), o.end(), word) - o.begin(); //Searches which index the word is in the vector
cout<< "Describtion for " << word << " is " << d[pos] << endl; //d[pos] takes the description vector that is in the same index as the word vector
}
else
cout<< "Word not found! Try something else." << endl; //If word not found
它只会从描述中提取最后一个词。我通过使用 map 遇到了同样的问题:
cout<< "Enter a word to lookup:";
cin>> word;
if (L.find(word) != L.end()) //Lookup if the key excist
{
cout<< "Describtion for " << word << " is " << L[word] << endl; //Tells what the description is for the word if it excist
}
else
cout<< "Word not found! Try something else." << endl; //Tells you this if key is not found
那么,我怎样才能打印出特定单词的整个描述呢?
编辑:我注意到它只是缺少描述中的第一个单词(我很愚蠢,不会尝试使用超过 2 个单词)
那么,有什么问题吗?如何在输出的描述中获取第一个单词?
最佳答案
如果您从 std::cin 中提取单个 std::string,它只会得到一个单词。首先,您获取描述的第一个词并将其放入 desc:
cin >> desc; // Here you enter the decribtion
然后您将获取描述中剩余的单词并将它们放入desc。覆盖desc(第一个字)之前的内容:
getline(cin, desc); // So you can have "space" and write a whole sentence.
所以,现在 desc 包含描述的第一个单词以外的所有内容。考虑使用调试器来查找此类内容。
其他一些建议:避免搜索 vector 两次。将 find 的结果存储在一个变量中:
auto search = find(o.begin(), o.end(), word);
if (search != o.end()) {
int pos = std::distance(o.begin(), search);
// Use pos ...
}
关于C++ 词汇表;在 vector/ map 中找到完整的描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27424378/
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我正在学习http://ruby.railstutorial.org/chapters/static-pages上的RubyonRails教程并遇到以下错误StaticPagesHomepageshouldhavethecontent'SampleApp'Failure/Error:page.shouldhave_content('SampleApp')Capybara::ElementNotFound:Unabletofindxpath"/html"#(eval):2:in`text'#./spec/requests/static_pages_spec.rb:7:in`(root)'
如何将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.你能做的最好的事情是:
如何找到调用此方法的位置?defto_xml(options={})binding.pryoptions=options.to_hifoptions&&options.respond_to?(:to_h)serializable_hash(options).to_xml(options)end 最佳答案 键入caller。这将返回当前调用堆栈。文档:Kernel#caller.例子[0]%rspecspec10/16|===================================================62=====
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我
我需要从json记录中获取一些值并像下面这样提取curr_json_doc['title']['genre'].map{|s|s['name']}.join(',')但对于某些记录,curr_json_doc['title']['genre']可以为空。所以我想对map和join()使用try函数。我试过如下curr_json_doc['title']['genre'].try(:map,{|s|s['name']}).try(:join,(','))但是没用。 最佳答案 你没有正确传递block。block被传递给参数括号外的方法
我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么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”]、[“苹果”、“
我使用的第一个解析器生成器是Parse::RecDescent,它的指南/教程很棒,但它最有用的功能是它的调试工具,特别是tracing功能(通过将$RD_TRACE设置为1来激活)。我正在寻找可以帮助您调试其规则的解析器生成器。问题是,它必须用python或ruby编写,并且具有详细模式/跟踪模式或非常有用的调试技术。有人知道这样的解析器生成器吗?编辑:当我说调试时,我并不是指调试python或ruby。我指的是调试解析器生成器,查看它在每一步都在做什么,查看它正在读取的每个字符,它试图匹配的规则。希望你明白这一点。赏金编辑:要赢得赏金,请展示一个解析器生成器框架,并说明它的
有没有办法让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=