问题描述
我正在尝试使用std :: string作为stxxl :: map中的键对于少量的大约10-100的琴弦,插入效果很好.但是,当尝试在其中插入大量大约100000的字符串时,我遇到了段错误.
I am trying to use std::string as a key in the stxxl::mapThe insertion was fine for small number of strings about 10-100.But while trying to insert large number of strings about 100000 in it, I am getting segmentation fault.
代码如下:
struct CompareGreaterString {
bool operator () (const std::string& a, const std::string& b) const {
return a > b;
}
static std::string max_value() {
return "";
}
};
// template parameter <KeyType, DataType, CompareType, RawNodeSize, RawLeafSize, PDAllocStrategy (optional)>
typedef stxxl::map<std::string, unsigned int, CompareGreaterString, DATA_NODE_BLOCK_SIZE, DATA_LEAF_BLOCK_SIZE> name_map;
name_map strMap((name_map::node_block_type::raw_size)*3, (name_map::leaf_block_type::raw_size)*3);
for (unsigned int i = 0; i < 1000000; i++) { /// Inserting 1 million strings
std::stringstream strStream;
strStream << (i);
Console::println("Inserting: " + strStream.str());
strMap[strStream.str()]=i;
}
在这里我无法确定为什么我不能插入更多的字符串.插入"1377"时,我恰好遇到了分段错误.另外,我能够添加任意数量的整数作为键.我认为字符串的可变大小可能会导致此问题.
In here I am unable to identify why I am unable to insert more number of strings. I am getting segmentation fault exactly while inserting "1377". Plus I am able to add any number of integers as key. I feel that the variable size of string might be causing this trouble.
我也无法理解字符串max_value
的返回值.我只是返回了一个空白字符串.
Also I am unable to understand what to return for max_value
of the string. I simply returned a blank string.
推荐答案
我终于在Timo bingmann,user2079303和Martin Ba的大力帮助下找到了解决我的问题的方法.谢谢.
I have finally found the solution to my problem with great help from Timo bingmann, user2079303 and Martin Ba. Thank you.
我想和你分享.
首先,stxxl仅支持POD.这意味着它仅存储固定大小的结构.因此std :: string不能为键. stxxl :: map可用于大约100-1000个字符串,因为它们包含在物理内存本身中.当插入更多字符串时,它必须在磁盘上写,这在内部造成了一些问题.
Firstly stxxl supports POD only. That means it stores fixed sized structures only. Hence std::string cannot be a key. stxxl::map worked for about 100-1000 strings because they were contained in the physical memory itself. When more strings are inserted it has to write on disk which is internally causing some problems.
因此,我们需要使用char []使用固定字符串,如下所示:
Hence we need to use a fixed string using char[] as follows:
static const int MAX_KEY_LEN = 16;
class FixedString {
public:
char charStr[MAX_KEY_LEN];
bool operator< (const FixedString& fixedString) const {
return std::lexicographical_compare(charStr, charStr+MAX_KEY_LEN,
fixedString.charStr, fixedString.charStr+MAX_KEY_LEN);
}
bool operator==(const FixedString& fixedString) const {
return std::equal(charStr, charStr+MAX_KEY_LEN, fixedString.charStr);
}
bool operator!=(const FixedString& fixedString) const {
return !std::equal(charStr, charStr+MAX_KEY_LEN, fixedString.charStr);
}
};
struct comp_type : public std::less<FixedString> {
static FixedString max_value()
{
FixedString s;
std::fill(s.charStr, s.charStr+MAX_KEY_LEN, 0x7f);
return s;
}
};
请注意,主要所有运算符((),==,!=)必须被覆盖,所有stxxl :: map函数才能正常工作现在,我们可以为地图定义fixed_name_map,如下所示:
Please note that all the operators mainly((), ==, !=) need to be overriden for all the stxxl::map functions to workNow we may define fixed_name_map for map as follows:
typedef stxxl::map<FixedString, unsigned int, comp_type, DATA_NODE_BLOCK_SIZE, DATA_LEAF_BLOCK_SIZE> fixed_name_map;
fixed_name_map myFixedMap((fixed_name_map::node_block_type::raw_size)*5, (fixed_name_map::leaf_block_type::raw_size)*5);
现在程序可以正常编译并且可以接受大约10 ^ 8个字符串,而不会出现任何问题.我们也可以像std :: map一样使用myFixedMap. {例如:myFixedMap [fixedString] = 10}
Now the program is compiling fine and is accepting about 10^8 strings without any problem.also we can use myFixedMap like std::map itself. {for ex: myFixedMap[fixedString] = 10}
这篇关于如何在stxxl :: map中使用std :: string作为键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!