声明指向不同字符串的指针的2d数组(在2d数组中)

declaring a 2d array of pointers that point to different strings (in a 2d array)

本文关键字:数组 2d 字符串 声明 指针      更新时间:2023-10-16

我实际上需要根据单词的长度对它们进行排序。所以这些单词被存储在一个2d数组中。现在,我不想让单词本身以排序的形式存在,而是希望每个单词都有一个指针,可以存储在一个2d数组中,其中行表示单词的长度-1,列表示单词的数量-1。

所以单词在字典[a][b]中,我想要一个*sorted_list[最大单词大小][最大字数]

我需要帮助将*sorted_list的一行传递给函数&访问该行中的单词。

编辑:我的尝试:这给出了运行时未处理的错误

    char *sublist[10][100];
    sublist[(strlen(d[j]))-1][count[(strlen(d[j]))-1]]= d+(j*10); count[strlen(d[j])-1]++;

为什么不生成std::multimap<std::string, LenghtComparator>,其中LengthComparator是返回s1.length() < s2.length()的谓词?然后,您可以检索给定长度的所有单词的equal_range

为什么不使用单词大小作为键值来设置哈希表,然后将指向每个char字符串的指针存储在其关联的哈希槽中?如果您愿意,可以使用std::unordered_map来完成,也可以自己使用std::vectorstd::list的组合以及strlen()作为哈希函数来制作哈希表。例如,使用后一种设置,您可以执行以下操作:

char* my_string = "Something";
//create a hash-table with ten slots (that's what you have in your example)
//where each slot holds a word of N length
std::vector<std::list<char*> > hash_table(10);
//this will store my_string at the back of the list at hash_table[9]
hash_table[strlen(my_string)].push_back(my_string);
//if you want the current num elements in the list, that's easy to-do as well
int list_size = hash_table[9].size();