Re: [boost] Boost.Algorithm design question

On Fri, 07 Oct 2011 10:30:26 +0200, Thorsten Ottosen <thorsten.ottosen@dezide.com> wrote:
Den 06-10-2011 18:27, Phil Endecott skrev:
Thorsten Ottosen wrote:
Even better, don't use vector<const char*>. What o you need that for? [snip]
Here's a real motivating example where I want to use vector<const char*>. I have a large file containing null-terminated strings which I memory map. After opening the file, I construct some sort of index of those strings in a sorted vector. Then I search for things using std::equal_range, std::lower_bound, or similar.
That's a good example, I grant you.
I actually have a helper class for this called cstrptr which is a typedef for the template class basic_cstrptr<char>. It is a "dumb" wrapper around const char * that provides some std::string functions to the normally even dumber bare pointer to char. It's designed for holding those magic strings that creep into production code all the time. sizeof cstrptr == sizeof const char* is always true so the constructor has very little overhead. The calling code is responsible for ensuring the lifetime of the ptr exceeds the lifetime of the cstrptr instance. It implements all the comparison operators against std::basic_string and const char*. It implements a const operator[] and an O(N) length() function. It only implements assignment and copy constructor to allow containers to hold them. I've wondered whether it was worth submitting to boost. It is so small I can't imagine it sitting on its own as a "library".

Den 07-10-2011 20:45, Joe Mucchiello skrev:
On Fri, 07 Oct 2011 10:30:26 +0200, Thorsten Ottosen<thorsten.ottosen@dezide.com> wrote:
Den 06-10-2011 18:27, Phil Endecott skrev:
Thorsten Ottosen wrote:
Even better, don't use vector<const char*>. What o you need that for? [snip]
Here's a real motivating example where I want to use vector<const char*>. I have a large file containing null-terminated strings which I memory map. After opening the file, I construct some sort of index of those strings in a sorted vector. Then I search for things using std::equal_range, std::lower_bound, or similar.
That's a good example, I grant you.
I actually have a helper class for this called cstrptr which is a typedef for the template class
basic_cstrptr<char>. It is a "dumb" wrapper around const char * that provides some std::string
Also, something like iterator_range<const char*> might be somewhat more handy to store in the vector. -Thorsten
participants (2)
-
Joe Mucchiello
-
Thorsten Ottosen