简体   繁体   English

如何轻松使用 char**?

[英]How can I easily work with a char**?

I have a char** that I frequently need to insert into or perform a lookup.我有一个经常需要插入或执行查找的char** It is very tedious to realloc() , malloc() the array and insert strings. realloc()malloc()数组和插入字符串非常繁琐。

Is there any standard way that I can add strings to or do lookups in a char** ?是否有任何标准方法可以添加字符串或在char**中进行查找? I guess I'm looking for something like string, but using char** 's instead.我想我正在寻找类似字符串的东西,但使用char**代替。

If you're frequently inserting into this structure, you shouldn't be using a char** at all;如果您经常插入此结构,则根本不应该使用char** an array isn't a suitable data structure for these kinds of operations.数组不是此类操作的合适数据结构。 Consider a std::vector<string> or something similar if possible.如果可能,考虑使用std::vector<string>或类似的东西。

Sounds like you want to use something like an STL List or Boost Array of char*.听起来您想使用 STL 列表或 char* 的Boost Array之类的东西。

Note that STL Vectors are not preferable if you need to insert or remove elements from your array.请注意,如果您需要从数组中插入或删除元素,则不推荐使用 STL 向量。

It's not clear what question you're trying to ask.目前尚不清楚您要问什么问题。 If you want a sequence of strings, perhaps the Seq_T type from Dave Hanson's C Interfaces and Implementations will do the trick---it's a collection of very helpful data structures written in C, and it manages to grow and shrink memory for you as you insert and remove items. If you want a sequence of strings, perhaps the Seq_T type from Dave Hanson's C Interfaces and Implementations will do the trick---it's a collection of very helpful data structures written in C, and it manages to grow and shrink memory for you as you插入和删除项目。

If this is plain ol' C -如果这是普通的 ol' C -

I haven't seen any standard ways to do this, but that being said, I've done this a lot in my C routines.我还没有看到任何标准的方法来做到这一点,但话虽如此,我已经在我的 C 例程中做了很多。 It's actually fairly straightforward to make a series of functions to do the manipulations you want on char* and char** values.实际上,创建一系列函数来对 char* 和 char** 值进行您想要的操作是相当简单的。

For me, it was just a matter of building a (small) library of routines.对我来说,这只是建立一个(小型)例程库的问题。 I started with the routines for the memory management and all of the char* operations I wanted.我从 memory 管理的例程和我想要的所有 char* 操作开始。 Then used those in making the routines for inserting into char**, etc.然后使用这些来制作插入 char** 等的例程。

For C++, you should consider refactoring to use std::vector or some other similar data structure.对于 C++,您应该考虑重构以使用 std::vector 或其他类似的数据结构。 It will be much nicer.会好很多。

if C++ you can use STL but be warned that STL's cross platform support is iffy, which may not matter to you.如果 C++ 你可以使用 STL 但要注意 STL 的跨平台支持是不确定的,这对你来说可能无关紧要。

if C then you probably want to create a data structure to hold all your strings and create insert, remove, modify functions on that data structure.如果 C 那么您可能想要创建一个数据结构来保存所有字符串并在该数据结构上创建插入、删除、修改函数。 you should be able to find plenty of open source code for the data structure of your choice.您应该能够为您选择的数据结构找到大量开源代码。

either way to select a proper data structure you need to consider your lookup and insertion patterns, how often do you insert?无论哪种方式 select 一个适当的数据结构,您需要考虑您的查找和插入模式,您多久插入一次? how often do you "look up"?你多久“抬头”一次? is the look up done by searching for a matching string or can you do something more intelligent / faster?是通过搜索匹配的字符串来完成查找,还是可以做一些更智能/更快的事情? basically more information is needed.基本上需要更多信息。

for example if the strings are all unique and your dealing with lots of strings it makes sense to calculate a hash of the string and store them in something like a RB-tree keyed on that hash.例如,如果字符串都是唯一的,并且您处理大量字符串,那么计算字符串的 hash 并将它们存储在类似于 RB-tree 的东西中,该树以 hash 为键。 if your never getting more than 10 strings it may not make sense to do this or maybe the application is such that you can simply assign an ID to a string and use the index as a look up key.如果你从来没有得到超过 10 个字符串,那么这样做可能没有意义,或者应用程序可能是这样的,你可以简单地为字符串分配一个 ID 并将索引用作查找键。

there are lots of options.有很多选择。

I agree with the (many,) other who have suggested that another data structure is probably better for this situation, but if you must use char** for some reason, the usual heuristic is to我同意(许多)其他人的观点,他们建议另一种数据结构可能更适合这种情况,但如果您出于某种原因必须使用char** ,通常的启发式方法是

  1. maintain a current length and available length at all times (say in a struct ) and始终保持当前长度和可用长度(例如在struct中)和
  2. always increase the buffer size by a factor of two (or more) when realloc ing (which results in an amortized O(n) time for allocation and coping).总是在重新分配时将realloc大小增加两倍(或更多)(这导致分配和应对的摊销 O(n) 时间)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM