啊,這是真的,性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