我不确定这段代码有什么问题:
std::vector<int> myVector(0);
if (myVector.back() == 12)
myVector.push_back(12);
Run Code Online (Sandbox Code Playgroud)
似乎在空向量上调用back()会使程序崩溃.
我不明白为什么会崩溃?我们需要在调用之前检查向量的长度back()吗?或者可能是一个错误?
文档说,如果向量为空,则返回未定义的值.
我致力于开发外部API.我在我的公共接口中添加了一个方法:
public void AddMode(TypeA mode);
public void AddMode(TypeB mode); // the new method, TypeB and TypeA are not related at all
Run Code Online (Sandbox Code Playgroud)
看起来很不错,直到一次测试破了才通过了null.这使编译器与模糊调用混淆.我通过转换null修复了测试.
不过我的问题是:
设计API时,这种情况下最好的是什么?
编辑:
调用就像这个AddMode(null),不像:
TypeA vl = null;
AddMode(v1); // this doesn't cause a problem
Run Code Online (Sandbox Code Playgroud) 在MSDN Diagnostics.FileVersionInfo中,它表示例如ProductMinorPart返回一个int或者null该值是未定义的.然而,它不是Nullable<int>- 它怎么能返回null?我用ILDasm检查过,定义是普通的int,没有Nullable ......
我错过了什么吗?
我只是对字符串和它们的比较感到困惑.我理解的是这样做:
string one = "stackoverflow";
string two = "stackoverflow";
bool equal = one == two;
Run Code Online (Sandbox Code Playgroud)
这会逐个字符比较吗?
为什么会这样?如果string是不可变的,并且如果它们具有相同的字符,则两个变量将始终引用相同的字符串.为什么编译器不检查引用?如果有一个地方,我认为引用平等意味着价值平等,我认为这将是字符串.我错过了什么?
我们开始在日志中看到这一点,知道从哪里开始寻找问题吗?调用堆栈中没有任何内容可以帮助我们入门。
Message :An item with the same key has already been added. at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) at
System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean
add) at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value) at
System.Web.Mvc.JsonValueProviderFactory.EntryLimitedDictionary.Add(String key,
Object value) at
System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary
backingStore, String prefix, Object value) at
System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary
backingStore, String prefix, Object value) at
System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) at
System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) at
System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) at
System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object value) at
System.Web.Mvc.JsonValueProviderFactory.AddToBackingStore(EntryLimitedDictionary backingStore, String prefix, Object …Run Code Online (Sandbox Code Playgroud) c# ×3
api ×1
asp.net ×1
asp.net-mvc ×1
c++ ×1
msdn ×1
std ×1
stdvector ×1
stl ×1
visual-c++ ×1