English 中文(简体)
采用分机:复印视窗上的大座
原标题:Using std::copy to copy wide strings on Windows
  • 时间:2011-11-23 15:29:47
  •  标签:
  • c++

让我说一下,我有这么多的经历。

std::wstring s(L"Stack Over Flow");

that I want to copy into a vector of wide characters std::vector<wchar_t> (of a null terminated string ) using std::copy
Both the following approaches seem to work
First

std::vector<wchar_t> c( s.size() + 1 );
std::copy(s.c_str(), s.c_str() + s.length() + 1, c.begin());
wprintf( L"%s
",&c[0] );

二、导 言

std::vector<wchar_t> d( s.size() + sizeof(wchar_t));
std::copy(s.c_str(), s.c_str() + s.length() + sizeof(wchar_t), d.begin());
wprintf( L"%s
",&d[0] );

But I am thinking the first should fail because I am not allocating the correct size for the ending null character. What did I miss?
Edit
No, I don t want to to this

std::vector<wchar_t> c(s.begin(), s.end()); <br>

because I am resizing an existing buffer
Edit
By changing the use case (print using the C like API ) I can see that the second version is probably the correct one.
First prints Stack Over Flow????????
Second prints Stack Over Flow as expected

问题回答

为什么如此多的热点?

我想知道,为什么你不这样做:

std::vector<wchar_t> c(s.begin(), s.end());

简明扼要!

As for your code, both versions are wrong. Both are wrong because the second argument is going beyond the end, as it should be only s.c_str() + s.length(). That is,

2nd arg ---> s.c_str() + s.length() //correct

2nd arg ---> s.c_str() + s.length() + 1 //wrong - first version
2nd arg ---> s.c_str() + s.length() + sizeof(wchar_t) //wrong - second version

If you follow simple code, idiomatic code, you can avoid much of error.


如果您使用<代码>std:copy,例如,如果您因某种原因不能使用病媒构造,那么辅助器<代码>则:copy:

void f(std::vector<wchar_t> & c)
{
  //you cannot use constructor version now, so use std::copy as
  std::copy(s.begin(), s.end(), std::back_inserter(c));
}

EDIT:

如果你重新与C间图书馆和有方位的APICS合作,那么我建议你使用<代码>std:wstring <>code>(或std:string)。 我在使用<代码>std:vector方面看不出什么点,因为它给守则增加了混乱。 如果你使用病媒,你最终必须证明无效,以便发挥作用:

  c.push_back(  ); //do this after copying!

But I still don t see any point in doing that, as you re trying to treat vector like a string. If you need string, why not use std::wstring and it s std::wstring::c_str() to get the c-string.

When you ask for a std::vector<wchar_t> c(size) it allocates enough space to contain size wchar_t items - the size you ask for is not in bytes, but is in wchar_t items.

首先:当你拥有一个大小设备集装箱时,你实际上并不需要终止无效。

第二,<代码>的这个构件: T> 系指T元素的数量,而不是tes。 http://code>+1。

[edit] With the edited question, the easiest solution seems c.assign(s.begin(), s.end()).





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签