Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Fast way to add null after each char

Reply
Thread Tools

Fast way to add null after each char

 
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      09-05-2010
* Juha Nieminen, on 05.09.2010 21:14:
> Alf P. Steinbach /Usenet<(E-Mail Removed)> wrote:
>> * Brad, on 05.09.2010 18:54:
>>> std::string s = "easy";
>>>
>>> std::string unicode_string;
>>>
>>> std::string::const_iterator it,
>>>
>>> for(it = s.begin(); it != s.end(); ++it)
>>> {
>>> unicode_string.push_back(*it);
>>> unicode_string.push_back('\0');
>>> }
>>>
>>> The above for loop would make unicode_string look like this:
>>>
>>> "e null a null s null y null"
>>>
>>> Is there a faster way to do this... in place maybe?

>>
>> Depends what you want.
>>
>> It /seems/ that you're assuming a little-endian architecture

>
> No, he isn't. He is making the string UTF16LE, not assuming that the
> architecture is little-endian.


Perhaps, but it would be (I think even more) unusual.


Cheers,

- Alf

--
blog at <url: http://alfps.wordpress.com>
 
Reply With Quote
 
 
 
 
Francesco S. Carta
Guest
Posts: n/a
 
      09-05-2010
SG <(E-Mail Removed)>, on 05/09/2010 11:53:58, wrote:

> On 5 Sep., 20:04, Francesco S. Carta wrote:
>> Marc wrote:
>>> On 5 sep, 19:27, Francesco S. Carta wrote:
>>>> Define "better".
>>> Faster. [...]

>> Exactly. So neither is better than the other unless we associate
>> "better" to "more readable" or to "faster"

>
> See the original post:
>
> "...Is there a faster way to do this..."


Of course, I was just playing at nitpicking after your overzealous snip
- see my further post

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
 
 
 
Geoff
Guest
Posts: n/a
 
      09-05-2010
On Sun, 5 Sep 2010 09:54:28 -0700 (PDT), Brad <(E-Mail Removed)> wrote:

>std::string s = "easy";
>
>std::string unicode_string;
>
>std::string::const_iterator it,
>
>for(it = s.begin(); it != s.end(); ++it)
>{
> unicode_string.push_back(*it);
> unicode_string.push_back('\0');
>}
>
>The above for loop would make unicode_string look like this:
>
>"e null a null s null y null"
>
>Is there a faster way to do this... in place maybe?
>
>Thanks for any tips,
>
>Brad
>


Are you really trying to insert null after each character or are you looking for
a way to convert std::string into std::wstring?

 
Reply With Quote
 
Geoff
Guest
Posts: n/a
 
      09-05-2010
On Sun, 05 Sep 2010 13:02:11 -0700, Geoff <(E-Mail Removed)> wrote:

>On Sun, 5 Sep 2010 09:54:28 -0700 (PDT), Brad <(E-Mail Removed)> wrote:
>
>>std::string s = "easy";
>>
>>std::string unicode_string;
>>
>>std::string::const_iterator it,
>>
>>for(it = s.begin(); it != s.end(); ++it)
>>{
>> unicode_string.push_back(*it);
>> unicode_string.push_back('\0');
>>}
>>
>>The above for loop would make unicode_string look like this:
>>
>>"e null a null s null y null"
>>
>>Is there a faster way to do this... in place maybe?
>>
>>Thanks for any tips,
>>
>>Brad
>>

>
>Are you really trying to insert null after each character or are you looking for
>a way to convert std::string into std::wstring?


Forgot to attach the code.

#include <string>

int main()
{
std::string s = "easy";
std::wstring unicode_string;

unicode_string.assign(s.begin(),s.end());
return 0;
}
 
Reply With Quote
 
tni
Guest
Posts: n/a
 
      09-06-2010
On 2010-09-05 20:04, Francesco S. Carta wrote:
>>
>> Faster. SG's code has linear complexity and yours is quadratic.
>> Readability is something else...

>
> Exactly. So neither is better than the other unless we associate
> "better" to "more readable" or to "faster"


Unnecessary quadratic code is a bug (unless you have guarantees on the
input size).
 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      09-06-2010
tni <(E-Mail Removed)>, on 06/09/2010 10:37:08, wrote:

> On 2010-09-05 20:04, Francesco S. Carta wrote:
>>>
>>> Faster. SG's code has linear complexity and yours is quadratic.
>>> Readability is something else...

>>
>> Exactly. So neither is better than the other unless we associate
>> "better" to "more readable" or to "faster"

>
> Unnecessary quadratic code is a bug (unless you have guarantees on the
> input size).


That was a deliberately slow implementation - see all the other posts.

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
tni
Guest
Posts: n/a
 
      09-06-2010
On 2010-09-06 10:56, Francesco S. Carta wrote:
> tni <(E-Mail Removed)>, on 06/09/2010 10:37:08, wrote:
>
>> On 2010-09-05 20:04, Francesco S. Carta wrote:
>>>>
>>>> Faster. SG's code has linear complexity and yours is quadratic.
>>>> Readability is something else...
>>>
>>> Exactly. So neither is better than the other unless we associate
>>> "better" to "more readable" or to "faster"

>>
>> Unnecessary quadratic code is a bug (unless you have guarantees on the
>> input size).

>
> That was a deliberately slow implementation - see all the other posts.
>


My point isn't that the implementation is a bit slower, it's wrong and
should never be used. There is no question whether one of the two is better.

Feed your quadratic implementation a 10MB string and it will literally
run for hours.
 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      09-06-2010
tni <(E-Mail Removed)>, on 06/09/2010 11:38:54, wrote:

> On 2010-09-06 10:56, Francesco S. Carta wrote:
>> tni <(E-Mail Removed)>, on 06/09/2010 10:37:08, wrote:
>>
>>> On 2010-09-05 20:04, Francesco S. Carta wrote:
>>>>>
>>>>> Faster. SG's code has linear complexity and yours is quadratic.
>>>>> Readability is something else...
>>>>
>>>> Exactly. So neither is better than the other unless we associate
>>>> "better" to "more readable" or to "faster"
>>>
>>> Unnecessary quadratic code is a bug (unless you have guarantees on the
>>> input size).

>>
>> That was a deliberately slow implementation - see all the other posts.
>>

>
> My point isn't that the implementation is a bit slower, it's wrong and
> should never be used. There is no question whether one of the two is
> better.
>
> Feed your quadratic implementation a 10MB string and it will literally
> run for hours.


You're right, of course, and finally somebody posted the correct,
explicit objection to the first response of mine, which was
over-zealously half-snipped by SG:

"Faster, I don't know (measure it), in place, yes: use the
std::string::insert() method."

My purpose was to push the OP to make all the tests and the reasonings.

But the OP disappeared and the group took circa ten posts to come down
to this, I won't post any bait like this anymore, just to save my time

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
Goran Pusic
Guest
Posts: n/a
 
      09-06-2010
On Sep 5, 6:54*pm, Brad <(E-Mail Removed)> wrote:
> std::string s = "easy";
>
> std::string unicode_string;
>
> std::string::const_iterator it,
>
> for(it = s.begin(); it != s.end(); ++it)
> {
> * * * * unicode_string.push_back(*it);
> * * * * unicode_string.push_back('\0');
>
> }
>
> The above for loop would make unicode_string look like this:
>
> "e null a null s null y null"
>
> Is there a faster way to do this... in place maybe?


+1 for Alf. Chances are that you are just looking for
MultiByteToWideChar (or libiconv, but that's less likely).

Guys, aren't you a bit misleading with iterators and big-O and
stuff?

Goran.

Goran.
 
Reply With Quote
 
Francesco S. Carta
Guest
Posts: n/a
 
      09-06-2010
Goran Pusic <(E-Mail Removed)>, on 06/09/2010 03:15:21, wrote:

> Guys, aren't you a bit misleading with iterators and big-O and
> stuff?


My bad. I intentionally posted a wrong suggestion without clearly
marking it as such - I thought I was going to be castigated immediately,
but since the punishment didn't come at once, I kept it on to see what
was going to happen... now I realize that it wasn't all that fun for the
others, so I present my apologies to the group for the wasted time.

--
FSC - http://userscripts.org/scripts/show/59948
http://fscode.altervista.org - http://sardinias.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(const char *cp) and (char *p) are consistent type, (const char **cpp) and (char **pp) are not consistent lovecreatesbeauty C Programming 1 05-09-2006 08:01 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
/usr/bin/ld: ../../dist/lib/libjsdombase_s.a(BlockGrouper.o)(.text+0x98): unresolvable relocation against symbol `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostre silverburgh.meryl@gmail.com C++ 3 03-09-2006 12:14 AM
The difference between char a[6] and char *p=new char[6] ? wwj C Programming 24 11-07-2003 05:27 PM
the difference between char a[6] and char *p=new char[6] . wwj C++ 7 11-05-2003 12:59 AM



Advertisments