Topic: basic_string assignments
Author: Matthew Dempsky <jivera@flame.org>
Date: Mon, 8 Apr 2002 15:08:10 GMT Raw View
> you mean;
>
> copy( text.begin(), text.end(), back_inserter(wtext) );
Would that technically be the more politically correct standard library
usage? Part of the reason I came to the usage I did was reading SGI's
example for transform:
> const int N = 1000;
> vector<int> V1(N);
> vector<int> V2(N);
> vector<int> V3(N);
>
> iota(V1.begin(), V1.end(), 1);
> fill(V2.begin(), V2.end(), 75);
>
> assert(V2.size() >= V1.size() && V3.size() >= V1.size());
> transform(V1.begin(), V1.end(), V2.begin(), V3.begin(),
> plus<int>());
-- http://www.sgi.com/tech/stl/transform.html
It would seem to me that if using back_inserter was the correct technique
to use, V3 would use the default constructor and then transform's fourth
paramater would instead be back_insterter(V3).
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: Michiel.Salters@cmg.nl (Michiel Salters)
Date: Tue, 9 Apr 2002 14:39:31 GMT Raw View
Matthew Dempsky <jivera@flame.org> wrote in message news:<pan.2002.04.08.00.05.04.564298.3699@flame.org>...
> > you mean;
> >
> > copy( text.begin(), text.end(), back_inserter(wtext) );
>
> Would that technically be the more politically correct standard library
> usage? Part of the reason I came to the usage I did was reading SGI's
> example for transform:
("Presize" the container )
It usually doesn't matter a lot; preallocating prevents reallocations
but copying a small & known number of characters can be pretty fast.
OTOH, if you don't know the input size you can't preallocate
(e.g. copy with predicate)
Regards,
--
Michiel Salters
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: "nmtop40" <nmtop40@nmtop40.homechoice.co.uk>
Date: Fri, 5 Apr 2002 15:50:21 GMT Raw View
>
> string text = "Text goes here.";
> wstring wtext;
> wtext.reserve( text.capacity() );
> copy( text.begin(), text.end(), wtext.begin() );
>
you mean;
copy( text.begin(), text.end(), back_inserter(wtext) );
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: "Matthew Dempsky" <jivera@flame.org>
Date: Mon, 1 Apr 2002 08:50:07 GMT Raw View
In article <3ca5cb25$0$21379$4c41069e@reader1.ash.ops.us.uu.net>, "P.J.
Plauger" <pjp@dinkumware.com> wrote:
> And what are the rules for converting a basic_string< complex<float> >
> to a basic_string<char>?
compex<float>::operator char();
> Even if you want to interconvert between char
> and wchar_t you really should use a codecvt facet.
Using wchar_t and char as my example was only for simplicity - the actual
use I wanted it for was slightly more complicated, but I thought it would
be easiest to use two well known types in the post.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Christopher Eltschka <celtschk@web.de>
Date: Tue, 2 Apr 2002 22:08:48 GMT Raw View
"Matthew Dempsky" <jivera@flame.org> writes:
> Why is assignment from a string to a wstring (or any other basic_string
> other than charT = char) not allowed? For example the following:
>
> string text = "Text goes here.";
> wstring wtext = text;
>
> That doesn't compile because basic_string's assignment operator only
> takes parameters of the same type, but I think it should be a template
> member function so that you could do the above and it would effectively
> get transformed into:
>
> string text = "Text goes here.";
> wstring wtext;
> wtext.reserve( text.capacity() );
> copy( text.begin(), text.end(), wtext.begin() );
>
> Now as long as the types used for charT for the two strings can be cast
> from one to the other, basic_string<> automatically handles it correctly.
No, as long as *both*
a) the types for the two strings can be cast from one to another *and*
b) the wide character set is a strict superset of the narrow character
set,
your code handles this correctly.
Condition b is true for US-ASCII (7 bit) to Unicode, and provided that
char is unsigned, also for ISO-8859-1 to Unicode. It is definitvely
*wrong* if char is anything but US-ASCII or both unsigned and
ISO-8859-1, and wchar_t is Unicode.
Note that for most non-US-systems, the conditions for the conversion
being correct are not met.
The correct thing to do would be to use a locale corresponding to the
default character set and default wide character set (i.e. the
character sets used for "normal" and L"wide" string literals), and
there use the member functions narrow() and widen().
[...]
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Matthew Dempsky" <jivera@flame.org>
Date: Fri, 29 Mar 2002 21:13:39 GMT Raw View
Why is assignment from a string to a wstring (or any other basic_string
other than charT = char) not allowed? For example the following:
string text = "Text goes here.";
wstring wtext = text;
That doesn't compile because basic_string's assignment operator only
takes parameters of the same type, but I think it should be a template
member function so that you could do the above and it would effectively
get transformed into:
string text = "Text goes here.";
wstring wtext;
wtext.reserve( text.capacity() );
copy( text.begin(), text.end(), wtext.begin() );
Now as long as the types used for charT for the two strings can be cast
from one to the other, basic_string<> automatically handles it correctly.
It could also use specialization for assigning to basic_strings of the
same type to use the same method as is currently employed.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Matthew Dempsky" <jivera@flame.org>
Date: Sat, 30 Mar 2002 10:46:24 GMT Raw View
In article <pan.2002.03.29.15.07.34.36.15525@flame.org>, "Matthew Dempsky"
<jivera@flame.org> wrote:
> Why is assignment from a string to a wstring (or any other basic_string
> other than charT = char) not allowed? For example the following:
I should clarify that I'm curious as to why any of the class methods
restrict to basic_string's of the same type (other than operator+ maybe -
I could see that as being a problem), not just operator= (even though my
example actually used a constructor not assignment).
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Pete Becker <petebecker@acm.org>
Date: Sat, 30 Mar 2002 22:55:14 GMT Raw View
Matthew Dempsky wrote:
>
> In article <pan.2002.03.29.15.07.34.36.15525@flame.org>, "Matthew Dempsky"
> <jivera@flame.org> wrote:
>
> > Why is assignment from a string to a wstring (or any other basic_string
> > other than charT = char) not allowed? For example the following:
>
> I should clarify that I'm curious as to why any of the class methods
> restrict to basic_string's of the same type (other than operator+ maybe -
> I could see that as being a problem), not just operator= (even though my
> example actually used a constructor not assignment).
>
How should the class convert wchar_t (or any other character type) to
char? As with converting ints to strings, the answer is ostringstream
coupled with a locale object.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "P.J. Plauger" <pjp@dinkumware.com>
Date: Sun, 31 Mar 2002 00:37:43 GMT Raw View
"Matthew Dempsky" <jivera@flame.org> wrote in message news:pan.2002.03.30.01.05.10.47.15525@flame.org...
> In article <pan.2002.03.29.15.07.34.36.15525@flame.org>, "Matthew Dempsky"
> <jivera@flame.org> wrote:
>
> > Why is assignment from a string to a wstring (or any other basic_string
> > other than charT = char) not allowed? For example the following:
>
> I should clarify that I'm curious as to why any of the class methods
> restrict to basic_string's of the same type (other than operator+ maybe -
> I could see that as being a problem), not just operator= (even though my
> example actually used a constructor not assignment).
And what are the rules for converting a basic_string< complex<float> > to
a basic_string<char>? Even if you want to interconvert between char and
wchar_t you really should use a codecvt facet. Where does it come from?
P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]