如何使用boost :: optional在C中返回NULL?
发布时间:2020-12-16 03:24:09 所属栏目:百科 来源:网络整理
导读:我有一个函数需要在某些情况下返回NULL,还有另一个函数需要测试此函数的返回值.我知道boost :: optional,但不知道如何使用语法. 以下是一个简单的例子: int funct1(const string key) { // use iterator to look for key in a map if(iterator == map.end()
我有一个函数需要在某些情况下返回NULL,还有另一个函数需要测试此函数的返回值.我知道boost :: optional,但不知道如何使用语法.
以下是一个简单的例子: int funct1(const string& key) { // use iterator to look for key in a map if(iterator == map.end()) { return NULL // need help here! else return it->second; } void funct2(string key) { if(funct1(key) == NULL) { // <-- need help here! // do something } else { // do something else } 有人可以帮忙用语法吗? 谢谢. 解决方法
它保持在“NULL”状态,直到你设置它.你可以用这个成语:
optional<int> funct1(const string& key) { // use iterator to look for key in a map optional<int> ret; if (iterator != map.end()) { ret = it->second; } return ret; } 然后: if (!funct1(key)) { /* no value */ } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |