Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Char*作为映射C+中的键+; 我在C++程序中声明了一个哈希图,比如“代码>图M < /代码>。但它不起作用,所以我按照 并像map m一样声明了我的map。我的程序有点像这样 struct cmp_str { bool operator()(char const *a, char const *b) { return std::strcmp(a, b) < 0; } }; int main(int argc, char *argv[]) { map<char *, int, cmp_str> m //Reading strings from a file while( not end of file ) { // char *str contains the line if(m.find(str) != m.end()) {m[str]++; } else {m[str] = 1;} } } struct cmp\u str { 布尔运算符()(字符常量*a,字符常量*b) { 返回std::strcmp(a,b)_C++_Hash - Fatal编程技术网

Char*作为映射C+中的键+; 我在C++程序中声明了一个哈希图,比如“代码>图M < /代码>。但它不起作用,所以我按照 并像map m一样声明了我的map。我的程序有点像这样 struct cmp_str { bool operator()(char const *a, char const *b) { return std::strcmp(a, b) < 0; } }; int main(int argc, char *argv[]) { map<char *, int, cmp_str> m //Reading strings from a file while( not end of file ) { // char *str contains the line if(m.find(str) != m.end()) {m[str]++; } else {m[str] = 1;} } } struct cmp\u str { 布尔运算符()(字符常量*a,字符常量*b) { 返回std::strcmp(a,b)

Char*作为映射C+中的键+; 我在C++程序中声明了一个哈希图,比如“代码>图M < /代码>。但它不起作用,所以我按照 并像map m一样声明了我的map。我的程序有点像这样 struct cmp_str { bool operator()(char const *a, char const *b) { return std::strcmp(a, b) < 0; } }; int main(int argc, char *argv[]) { map<char *, int, cmp_str> m //Reading strings from a file while( not end of file ) { // char *str contains the line if(m.find(str) != m.end()) {m[str]++; } else {m[str] = 1;} } } struct cmp\u str { 布尔运算符()(字符常量*a,字符常量*b) { 返回std::strcmp(a,b),c++,hash,C++,Hash,当我执行该程序时,if将查找所有字符串,但首先查找,即使它们没有插入。当我尝试使用map m并将char*str转换为std::string它工作正常。但是输入文件太大了,当我使用字符串时需要花费很多时间。我不知道为什么我使用char*时它会找到所有字符串。任何帮助都将不胜感激。当您使用map m时,您不能在将该缓冲区插入std::map后修改该缓冲区,因为map不会复制数据,而是复制指针本身。当您使用std::mapstd::string时,确实会生成一个副本,这就是为什么它可以工作并且速度较

当我执行该程序时,if将查找所有字符串,但首先查找,即使它们没有插入。当我尝试使用
map m并将
char*str
转换为
std::string
它工作正常。但是输入文件太大了,当我使用字符串时需要花费很多时间。我不知道为什么我使用
char*
时它会找到所有字符串。任何帮助都将不胜感激。

当您使用
map m
时,您不能在将该缓冲区插入
std::map
后修改该缓冲区,因为map不会复制数据,而是复制指针本身。当您使用
std::map
std::string
时,确实会生成一个副本,这就是为什么它可以工作并且速度较慢的原因。因此,您要么需要手动创建许多缓冲区并在其中存储字符串(这会使您的程序变慢),要么使用
std::string
,这是一种合适且更好的方法。

那么您的问题是什么?实际上不是哈希映射,而是二叉树。如果你想要一个散列,你应该使用。您发布的代码不完整。这是不正确的duplicate@JoachimPileborg
unordered_map
没有帮助这是真的。我的琴弦固定长度为50。有没有更快的方法可以做到这一点?@CPP_NEW也许是的,但你需要解释一下你想要实现什么。请注意,如果您决定这样做,请打开新问题。我有一个大文件(~5GB),我想将每行的计数存储在哈希表中。目前,我使用
std::string
大约需要10分钟。我想要它faster@CPP_NEW这取决于文件中的数据。如果有许多重复,您可能需要使用基数树-