小编Mr.*_*Ree的帖子

为什么ostream :: write()在C++中需要'const char_type*'而不是'const void*'?

fwrite()C中的函数const void *restrict buffer用作第一个参数,因此您可以直接将指针struct作为第一个参数传递给您.
http://en.cppreference.com/w/c/io/fwrite
例如fwrite(&someStruct, sizeof(someStruct), 1, file);

但在C++中,ostream::write()需求const char_type*会迫使您使用reinterpret_cast.(在Visual Studio 2013中,它是const char*.)
http://en.cppreference.com/w/cpp/io/basic_ostream/write
egfile.write(reinterpret_cast<char*>(&someStruct), sizeof(someStruct));

几乎在所有情况下,要写入文件的二进制数据都不是char数组,那么为什么标准更喜欢看起来更复杂的样式呢?

PS
1.实际上我write()ofstreamwith ios::binary模式下使用了这个方法,但根据引用,它继承了ofstream.所以我ostream::write()在上面使用.
2.如果要打印字符流,可以使用operator<<().是不是write()为编写原始数据而设计的方法?
3.如果write()不是编写二进制数据的方法,那么在标准中执行此操作的方法是什么?(尽管由于不同平台上的各种内存对齐策略,这可能会影响代码的可移植性)

c++ file fwrite void ofstream

20
推荐指数
2
解决办法
2541
查看次数

为什么HashSet <T>没有实现ICollection?

我将编写一个库来遍历一个对象图(就像某种序列化一样).
您将需要判断一个对象是否是遍历中的集合,因此ICollection我的想法就出现了.(string也已实施IEnumerable)

但是,ICollection除了HashSet仅实施之外,几乎集合中的所有容器都已实现,这真的很奇怪ICollection<T>......

我检查了System.Collections命名空间中的几乎所有常见容器:

ArrayList : IList, ICollection, IEnumerable, ICloneable  
BitArray : ICollection, IEnumerable, ICloneable  
Hashtable : IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback, ICloneable  
Queue : ICollection, IEnumerable, ICloneable  
SortedList : IDictionary, ICollection, IEnumerable, ICloneable  
Stack : ICollection, IEnumerable, ICloneable  
Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable, ISerializable, IDeserializationCallback
HashSet<T> : ISerializable, IDeserializationCallback, ISet<T>, ICollection<T>, IEnumerable<T>, IEnumerable  
LinkedList<T> …
Run Code Online (Sandbox Code Playgroud)

.net collections interface hashset

6
推荐指数
1
解决办法
1321
查看次数

标签 统计

.net ×1

c++ ×1

collections ×1

file ×1

fwrite ×1

hashset ×1

interface ×1

ofstream ×1

void ×1