啊,这是真的,性LevelDB确实需要在字符串,它可以返回一个字符串,但的Slice
结构还具有构造函数与数据的不透明数组:
// Create a slice that refers to data[0,n-1].
Slice(const char* data, size_t n)
当你一键搞定Slice
你仍然有char*
其中的数据,所以你真的不与琴弦打扰:
// Return a pointer to the beginning of the referenced data
const char* data() const { return data_; }
如果你的整个目标是有一个整数的关键,那么就你的整数转换为char *和商店它leveldb
,像这样:
int oldKey = 123;
char key[8];
memset(key, 0, 8);
*(int*)(&key) = oldKey;
*(int*)(&key) += 1;
// key is now 124
// want to put it back in a slice?
Slice s(key, sizeof(int));
无需麻烦的和昂贵的琴弦......
你确定的值不是124? – 2012-02-16 10:30:54
@izomorphius:typo ..谢谢..它是从123到124 – ezdazuzena 2012-02-16 10:31:31
所以你的关键实际上是123,而不是你的价值?你的旧密钥会发生什么? – Kiril 2012-02-16 10:37:05