C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

STL string::resize question / proposal

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Hector
Guest





PostPosted: Sun Feb 22, 2004 11:00 am    Post subject: STL string::resize question / proposal Reply with quote



I think that the class basic_string would benefit from a function
similar to resize but that would not overwrite whatever (garbage) was
in the buffer. Obviuosly, I don't know how to obtain such desired
result without a new function in the string class. Let us call xresize
to the ideal function that would allow us do the following:

std::string s;
unsigned int sz = SOME_VALUE;

s.reserve(sz);
if (s.capacity() >= sz) {
int newsize = WriteMaxNoOfChars(& s[0], sz);
s.xresize(newsize);
}

WriteMaxNoOfChars is any of those old functions that take the address
and size of a buffer and return the number of written characters. The
problem with using the current string::resize is that it would
overwrite whatever was in the buffer with the default character.

Does anybody know how to obtain such behaviour with current functions?
Note that I don't know the size of the written part beforehand, just
the size of the buffer.

If a new function with the behaviour of xresize is required, can
somebody pass along this request to the people who makes the
standards?

Greetings,

Hector C.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Niklas Matthies
Guest





PostPosted: Sun Feb 22, 2004 5:25 pm    Post subject: Re: STL string::resize question / proposal Reply with quote



On 2004-02-22 11:00, Hector wrote:
:
Quote:
s.reserve(sz);
if (s.capacity() >= sz) {
int newsize = WriteMaxNoOfChars(& s[0], sz);
s.xresize(newsize);
}

WriteMaxNoOfChars is any of those old functions that take the address
and size of a buffer and return the number of written characters. The
problem with using the current string::resize is that it would
overwrite whatever was in the buffer with the default character.

Does anybody know how to obtain such behaviour with current functions?
Note that I don't know the size of the written part beforehand, just
the size of the buffer.

s.resize(sz);
int newsize = WriteMaxNoOfChars(& s[0], sz);
s.resize(newsize);

It's not optimally efficient, but that shouldn't make much of a
difference in practice.

-- Niklas Matthies

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ulrich Eckhardt
Guest





PostPosted: Sun Feb 22, 2004 5:27 pm    Post subject: Re: STL string::resize question / proposal Reply with quote



Hector wrote:
Quote:
I think that the class basic_string would benefit from a function
similar to resize but that would not overwrite whatever (garbage) was
in the buffer. Obviuosly, I don't know how to obtain such desired
result without a new function in the string class. Let us call xresize
to the ideal function that would allow us do the following:

std::string s;
unsigned int sz = SOME_VALUE;
s.reserve(sz);
if (s.capacity() >= sz) {
int newsize = WriteMaxNoOfChars(& s[0], sz);
s.xresize(newsize);
}

This doesn't necessarily work, string is not required to store its
characters in a contiguous array, IIRC. Also, your use of reserve() is not
guaranteed to work, I think.

Anyhow, I'd rather say that basic_string needs a function to create an
array of given size and return the pointer to it, for interfacing to
legacy C-functions. CString::GetBufferSetLength() comes to mind...

Uli

--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Dhruv Matani
Guest





PostPosted: Sun Feb 22, 2004 5:30 pm    Post subject: Re: STL string::resize question / proposal Reply with quote

On Sun, 22 Feb 2004 06:00:53 -0500, Hector wrote:

Quote:
I think that the class basic_string would benefit from a function
similar to resize but that would not overwrite whatever (garbage) was
in the buffer. Obviuosly, I don't know how to obtain such desired
result without a new function in the string class. Let us call xresize
to the ideal function that would allow us do the following:

Try string::reserve(...);



--
Regards,
-Dhruv.

Proud to be a Vegetarian.
http://www.vegetarianstarterkit.com/
http://www.vegkids.com/vegkids/index.html


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Andrei Alexandrescu
Guest





PostPosted: Mon Feb 23, 2004 11:37 am    Post subject: Re: STL string::resize question / proposal Reply with quote

"Hector" <hhcalderon (AT) yahoo (DOT) com> wrote

Quote:
I think that the class basic_string would benefit from a function
similar to resize but that would not overwrite whatever (garbage) was
in the buffer.

Indeed such a functionality would add string more efficient. The safe way to
do it, however, is to have basic_string define a resize() that takes a
function that will initialize the data:

template <class F>
void basic_string::resize_f(size_type s, F init);

During resizing, resize_f will call init(old_end, new_end) to initialize the
data. If a programmer is sure she wants to leave the data uninitialized,
then she passes a function that does nothing.

This way the STL remains as safe as the programmer using it wants.


Andrei



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Sergei Emantayev
Guest





PostPosted: Wed Mar 03, 2004 8:25 pm    Post subject: Re: STL string::resize question / proposal Reply with quote

[email]hhcalderon (AT) yahoo (DOT) com[/email] (Hector) wrote in message news:<e64dc7b5.0402220018.4e557e9c (AT) posting (DOT) google.com>...
Quote:
I think that the class basic_string would benefit from a function
similar to resize but that would not overwrite whatever (garbage) was
in the buffer. Obviuosly, I don't know how to obtain such desired
result without a new function in the string class. Let us call xresize
to the ideal function that would allow us do the following:

std::string s;
unsigned int sz = SOME_VALUE;

s.reserve(sz);
if (s.capacity() >= sz) {
int newsize = WriteMaxNoOfChars(& s[0], sz);
s.xresize(newsize);
}


Once you have used &s[0] (that may be unsupported by some STL providers),
you can also try the following trick. Note, it can slightly hit the performance.

s.assign(&s[0], newsize);

Quote:

Greetings,

Hector C.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.