声明指向不同字符串的指针的2d数组(在2d数组中)
declaring a 2d array of pointers that point to different strings (in a 2d array)
我实际上需要根据单词的长度对它们进行排序。所以这些单词被存储在一个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::vector
和std::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();
相关文章:
- 2D数组来自文本输入,中间有空格
- 如何使用用户输入在C++中正确填充2D数组
- 如何在C++中检查2D数组中负值的输入验证
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 如何在C++函数中声明静态 2D 数组?
- 如何声明一个可以在整个程序中使用的全局 2d 3d 4d .. 数组(堆版本)变量?
- 我已经阅读了很多关于 2d 数组的信息,但我在作业中使用它时遇到了麻烦
- 使用矢量将文本文件中的输入存储到 2D 数组中
- 获取 2D 数组 c++ 中的所有数字对
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 如何在构造函数中使用初始值设定项设置具有相同值的 2d 数组?
- 在 Python 和 c++ 2d 数组初始化之间.这是怎麽?为什么呢?
- 如何打印 2D 字符数组C++
- 如何将数组 2d 作为参数从函数传递并返回数组 2d 此函数
- 数组 2D 多个值
- 创建数组数组 (2D),而无需在 Java 中初始化内部数组
- 如何在C++中将文本文件传输到数组2D
- 从同一类的另一个方法调用数组2D-C++
- 获取地址在数组2d中的位置