A small problem with 'string'

Matej Urbas matej.urbas@gmail.com
Thu Mar 2 16:42:00 GMT 2006


Hi

I don't know what was I thinking, but for a week or two I have simply 
assumed that time efficiency of string::length() is O(1). Ok, I know 
(and I knew before) that it isn't - so, this is not the problem... To 
make it clear: I DON'T WANT TO START THE O(1) QUESTION AGAIN!

Before you read on, you should know that I just need a hint or something 
(not an enormous amount of your time). If you'd say that this task would 
be too hard or even impossible, I'll believe you, no problem, just 
please don't go crazy about my question, please.

Like I said, I totally forgot :(  And now I have a problem... I have 
used length() extensively, and would like to get out of this mess. But 
before I erase all those length() calls, I'd like to be sure that there 
is absolutely no other way for ME (I repeat: ME - as in 'NOT YOU') to 
somehow substitute, implement (derive, modify... whatever) the string 
class and make the length() operation an O(1) thingy...

My question: Is it possible to extract the length of a string at 
construction time (i.e. when the char* array gets copied into the 
string's buffer - obviously the buffer must know how many characters get 
copied)? If it is possible, then how hard would it be to manage the 
length on operations such as erase and replace?

Is something like this already implemented somewhere else?

Should I just shut up and erase all those length() calls?

Thank you in ahead, and have a nice day
---
Matej

This is an excerpt I've removed from my first draft of this mail: Why do 
I want length() that much? Let me put it so: Since you'd normally reply 
to people like me that "wherever people use length() it's mostly because 
they have a flawed design or just don't know what they are doing" I'd 
rather think of myself as being one of those "few" who think that they 
have finally found a good use for length() and just want to find out 
more about how it could be done.



More information about the Libstdc++ mailing list