我想创建一个具有如下签名的函数:
// Set found to be an iterator to the location of key in map or end()
// if not found.
bool lookup(const Key &key,
const std::map<Key, Value> &map,
std::map<Key, Value>::const_iterator &found);
但我也想在映射和迭代器不是 const 的情况下调用它,以便我可以修改找到的值:
const Key key;
std::map<Key, Value> map;
std::map<Key, Value>::iterator found;
if (lookup(key, map, found)) {
found->second.modifingNonConstFunction()
}
但我不相信我可以通过 std::map<Key, Value>::iterator对象到期望引用 std::map<Key, Value>::const_iterator 的函数因为它们是不同的类型,而我通常可以如果 const是这种类型的 C++ 声明的一部分,我可以将非常量类型提升为常量类型:
void someFunction(const int &arg);
int notConstArg = 0;
someFunction(nonConstArg);
除了使用模板为 lookup() 提供两个定义之外, 一个如图所示 const参数 2 和 3 以及另一个带有非常量参数 2 和 3 的参数,在 C++ 中是否有更好的方法来完成这个更类似于 const int & 的方式?可以传递一个非常量 int在上面的例子中。换句话说,我可以只有一个功能而不是两个吗?
最佳答案
如果函数很简单或者您不介意二进制膨胀,只需将每个参数都设为模板参数即可。
template <typename Key, typename T, typename Iter>
bool lookup(Key const& key,
T& map,
Iter &found)
{
return (found=map.find(key))!=map.end();
}
int main()
{
std::map<std::string, int> m; m["hello"] = 42;
std::map<std::string, int> const cm(m.begin(), m.end());
std::map<std::string, int>::iterator it;
std::map<std::string, int>::const_iterator cit;
std::cout << std::boolalpha << lookup("hello", m, it) << '\n'; // Key isn't even std::string
std::cout << std::boolalpha << lookup("hello", m, cit) << '\n';
//std::cout << std::boolalpha << lookup("hello", cm, it) << '\n'; // error
std::cout << std::boolalpha << lookup("hello", cm, cit) << '\n';
}
这是可行的,因为 T 可以是 map 和 const map 所以 T& 是 map& 或 const map&。
关于c++ - 如何为集合提供带有迭代器的 const 接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14571221/
我刚刚为fedora安装了emacs。我想用emacs编写ruby。为ruby提供代码提示、代码完成类型功能所需的工具、扩展是什么? 最佳答案 ruby-mode已经包含在Emacs23之后的版本中。不过,它也可以通过ELPA获得。您可能感兴趣的其他一些事情是集成RVM、feature-mode(Cucumber)、rspec-mode、ruby-electric、inf-ruby、rinari(用于Rails)等。这是我当前用于Ruby开发的Emacs配置:https://github.com/citizen428/emacs
我的瘦服务器配置了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.你能做的最好的事情是:
1.postman介绍Postman一款非常流行的API调试工具。其实,开发人员用的更多。因为测试人员做接口测试会有更多选择,例如Jmeter、soapUI等。不过,对于开发过程中去调试接口,Postman确实足够的简单方便,而且功能强大。2.下载安装官网地址:https://www.postman.com/下载完成后双击安装吧,安装过程极其简单,无需任何操作3.使用教程这里以百度为例,工具使用简单,填写URL地址即可发送请求,在下方查看响应结果和响应状态码常用方法都有支持请求方法:getpostputdeleteGet、Post、Put与Delete的作用get:请求方法一般是用于数据查询,
//1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json
使用rspec-rails3.0+,测试设置分为spec_helper和rails_helper我注意到生成的spec_helper不需要'rspec/rails'。这会导致zeus崩溃:spec_helper.rb:5:in`':undefinedmethod`configure'forRSpec:Module(NoMethodError)对thisissue最常见的回应是需要'rspec/rails'。但这是否会破坏仅使用spec_helper拆分rails规范和PORO规范的全部目的?或者这无关紧要,因为Zeus无论如何都会预加载Rails?我应该在我的spec_helper中做
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我
假设我有一个类A,里面有一些方法。假设stringmethodName是这些方法之一,我已经知道我想给它什么参数。它们在散列中{'param1'=>value1,'param2'=>value2}所以我有:params={'param1'=>value1,'param2'=>value2}a=A.new()a.send(methodName,value1,value2)#callmethodnamewithbothparams我希望能够通过传递我的哈希以某种方式调用该方法。这可能吗? 最佳答案 确保methodName是一个符号,而
我使用rails3.1+rspec和factorygirl。我对必填字段(validates_presence_of)的验证工作正常。我如何让测试将该事实用作“成功”而不是“失败”规范是:describe"Addanindustrywithnoname"docontext"Unabletocreatearecordwhenthenameisblank"dosubjectdoind=Factory.create(:industry_name_blank)endit{shouldbe_invalid}endend但是我失败了:Failures:1)Addanindustrywithnona