返回右值或左值
returning either a rvalue or an lvalue
本文关键字:返回 更新时间:2023-10-16
我想定义s[i]
,如果从未分配s[0]
则返回0
,如果之前分配了s[i]
则返回对s[i]
的引用(以实现稀疏数组)。 下面的代码可以做到这一点,但由于 map 的语义,每当我尝试获取其值时,它最终都会创建s[i]
。
struct svec{
map<int,double> vals;
/*
double operator[](int index){
return (vals.count(index) > 0) ? vals[index] : 0 ;
else return 0;
}
*/
double &operator[](int index){
return vals[index];
}
};
int main(){
svec s;
s[0] = 10;
cout << s[1] << endl;
}
我希望注释的代码用于解析表达式s[1]
。 但是如果我取消注释它,我会收到一个错误。
你不能重载返回值,所以你必须坚持按引用或按值(或指针等)返回。通过引用返回的问题在于,您必须引用内存中的现有值。当然,当值在地图中时,这很好。如果不是,则必须创建默认值并将其存储在内存中。然后,您必须确保正确删除它以免泄漏内存,但也必须确保用户没有持有对值的引用,因为它会引入意外行为。
此外,您必须考虑用户可以更改您返回的值这一事实。如果返回相同的默认值,则用户可以将其更改为另一个值。然后,所有后续调用都将返回对新值的引用。每次返回时将默认值重置为 0 对于仍保留对它的引用的所有用户来说也是出乎意料的。
您可能能够以稳定的方式解决此问题,但它可能需要大量样板代码。在这种情况下,我建议将负担放在用户身上。
class SparseVector {
private:
std::unordered_map<int, double> elements;
public:
void set(int index, double value) {
elements[index] = value;
}
double& get(int index, double& optional) {
auto it = elements.find(index);
if (it != elements.end())
return it->second;
else
return optional;
}
double& get(int index) {
auto it = elements.find(index);
if (it != elements.end())
return it->second;
throw std::runtime_error(
"Couldn't find element at index " + std::to_string(index) +
"! Use get(int index, double& optional) if you don't want errors."
);
}
}
int main() {
double default_value = 0.0;
SparseVector vector;
std::cout << vector.get(0, default_value) << std::endl;
}
相关文章:
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 什么时候在C++中返回常量引用是个好主意
- 你能重载对象变量名本身返回的内容吗
- 为什么 Serial.println(<char[]>);返回随机字符?
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何获取std::result_of函数的返回类型
- QueryWorkingSet总是返回false
- (C++)分析树以计算返回错误值的简单算术表达式
- 访问者访问变体并返回不同类型时出错
- 如何返回一个类的两个对象相加的结果
- OpenInventor从9.8升级到10.4.2后,GLSL纹理返回零
- lower_bound()返回最后一个元素
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 如何取消对nullptr的屏蔽,返回正确的对象
- 奇怪的结构&GCC&clang(void*返回类型)
- 架构决策:返回std::future还是提供回调
- 从python中调用C++函数并获取返回值
- 矩阵向量乘法(cublasDgemv)返回零
- 为什么模板类中的对象不能返回值