[Boost-users] Converting an std::vector to std::wstring

1,035 views
Skip to first unread message

Sean Farrow

unread,
Jul 6, 2008, 5:20:30 PM7/6/08
to boost...@lists.boost.org
Hi:
What is the best way (in terms of efficiency) to convert an std::vector
of wchar_t to std::wstring.
I can think of a oupple of aproaches:
1. using std::copy.
2. using boost::lexical_cast.
3 using boost::foreach
4. Usin he boost::serialization library.
The std::wstring doesn't ned to be output to a file.
Any advice apreciated.
Sean.

__________ Information from ESET NOD32 Antivirus, version of virus
signature database 3244 (20080705) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Chris Jefferson

unread,
Jul 7, 2008, 4:44:18 AM7/7/08
to boost...@lists.boost.org
2008/7/6 Sean Farrow <sean....@seanfarrow.co.uk>:

> Hi:
> What is the best way (in terms of efficiency) to convert an std::vector
> of wchar_t to std::wstring.
> I can think of a oupple of aproaches:
> 1. using std::copy.
> 2. using boost::lexical_cast.
> 3 using boost::foreach
> 4. Usin he boost::serialization library.

I suspect 5. None of the above.

The quickest and most efficent way to switch between two containers
which contain the same type is:

new_container container(old_container.begin(), old_container.end() )

so in this case:

wstring my_string(v.begin(), e.end());

Sean Farrow

unread,
Jul 7, 2008, 4:49:58 AM7/7/08
to boost...@lists.boost.org
Hi:
I carn't use that approach, the string is already constructed.
The string is part of a structure. I could contruct a temporary string,
and then assign the temporary string to the string in the structure,
will this reset the length of the string in the structure?
Sean.

new_container container(old_container.begin(), old_container.end() )

so in this case:

wstring my_string(v.begin(), e.end());

signature database 3245 (20080707) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

__________ Information from ESET NOD32 Antivirus, version of virus

signature database 3245 (20080707) __________

Daniel Krügler

unread,
Jul 7, 2008, 4:54:52 AM7/7/08
to boost...@lists.boost.org
Sean Farrow wrote:
> Hi:
> What is the best way (in terms of efficiency) to convert an std::vector
> of wchar_t to std::wstring.
> I can think of a oupple of aproaches:
> 1. using std::copy.
> 2. using boost::lexical_cast.
> 3 using boost::foreach
> 4. Usin he boost::serialization library.
> The std::wstring doesn't ned to be output to a file.
> Any advice apreciated.
> Sean.

What about the sequence assignment capabilities of basic_string
itself, like this:

std::wstring from_vector(const std::vector<wchar_t>& src) {
return std::wstring(src.begin(), src.end());
}

?

For already existing std::wstring objects you could use
it's assign overload that accepts an input iterator pair.

HTH & greetings from Bremen,

Daniel Krügler

Sean Farrow

unread,
Jul 7, 2008, 4:58:09 AM7/7/08
to boost...@lists.boost.org
How does this assign overload work?
Sound like wht I need.
Sean.

-----Original Message-----
From: boost-use...@lists.boost.org [mailto:boost-use...@lists.boost.org] On Behalf Of Daniel Krügler
Sent: 07 July 2008 09:55
To: boost...@lists.boost.org
Subject: Re: [Boost-users] Converting an std::vector to std::wstring

?

Daniel Krügler

__________ Information from ESET NOD32 Antivirus, version of virus signature database 3245 (20080707) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

__________ Information from ESET NOD32 Antivirus, version of virus signature database 3245 (20080707) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

Daniel Krügler

unread,
Jul 7, 2008, 5:46:22 AM7/7/08
to boost...@lists.boost.org
Sean Farrow wrote:
> How does this assign overload work?
> Sound like wht I need.

Like the c'tor call, but using assign instead ;-):

void from_vector(std::wstring& dst, const std::vector<wchar_t>& src) {
dst.assign(src.begin(), src.end());
}

This call is semantically equivalent to:

dst.assign(wstring(src.begin(), src.end()));

The c'tor template accepts any InputIterator with value type convertible
to the value_type of the corresponding basic_string.

[N.B.: If InputIterator is actually an integral type the standard
requires that instead of a sequence constructor the constructor is
invoked which creates astring of given size with given character value]

Greetings from Bremen,

Sean Farrow

unread,
Jul 7, 2008, 5:51:30 AM7/7/08
to boost...@lists.boost.org
Hi:
Thanks that worked.
Sean.

-----Original Message-----
From: boost-use...@lists.boost.org [mailto:boost-use...@lists.boost.org] On Behalf Of Daniel Krügler
Sent: 07 July 2008 10:46
To: boost...@lists.boost.org
Subject: Re: [Boost-users] Converting an std::vector to std::wstring

dst.assign(wstring(src.begin(), src.end()));

Greetings from Bremen,

Daniel Krügler

__________ Information from ESET NOD32 Antivirus, version of virus signature database 3245 (20080707) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

__________ Information from ESET NOD32 Antivirus, version of virus signature database 3245 (20080707) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

Reply all
Reply to author
Forward
0 new messages