使用向量而不是动态分配的 wchar 数组
有一天,我被告知(在 stackoverflow 上!),因为我没有使用向量而不是动态分配的 wchar 数组。
因此,我研究了使用这种字符串操作方法,因为这似乎是防止可能的内存泄漏的好主意。
我想到的是,除非我错误地使用了向量模板类,否则使用向量的灵活性远不如使用堆分配数组和旧的 memcpy 灵活。
#include <shlobj.h>
HRESULT ModifyTheme()
{
using namespace std;
vector <WCHAR> sOutput;
vector <WCHAR> sPath;
vector <WCHAR> sThemesLocation;
vector <WCHAR> sThemeName;
const WCHAR sThemesPath [] = _T("\\Microsoft\\Windows\\Themes");
const WCHAR sFileName [] = _T("\\darkblue.theme");
sOutput.resize(MAX_PATH);
sPath.resize( MAX_PATH );
sThemesLocation.resize( MAX_PATH );
sThemeName.resize( MAX_PATH );
// Get appdata\local folder
SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, &sPath[0] );
// copy consts to vectors
memcpy( &sThemesLocation[0], sThemesPath, sizeof(sThemesPath) );
memcpy( &sThemeName[0], sFileName, sizeof(sFileName) );
// append themes path & filename
sOutput.insert( sOutput.begin(), sPath.begin(), sPath.end() );
sOutput.insert( sOutput.end()-1, sThemesLocation.begin(), sThemesLocation.end() );
sOutput.insert( sOutput.end()-1, sThemeName.begin(), sThemeName.end() );
wcout << &sThemeName[0] << endl;
wcout << &sThemesLocation[0] << endl;
wcout << &sPath[0] << endl;
wcout << &sOutput[0] << endl;
return S_OK;
}
我希望 sOutput 向量包含所有字符串的串联。相反,它仅包含第一个插入的字符串。
另外,我想我记得听说过虽然不可能在初始化列表中分配向量的值,但这可能是 c++0x 的一个功能。这是正确的吗?(目前)有什么方法可以执行以下操作:
vector<wchar> sBleh = { _T("bleh") };
最后,对于我想通过上面的简单例程实现的目标,我使用动态分配的数组会更好,还是应该坚持使用看似wchar 的不灵活向量?
The other day, I got told off (on stackoverflow!) for not using vector instead of a dynamically allocated wchar array.
So I had a look into using this method of string manipulation, as it seems like a good idea to prevent possible memory leaks.
What I came up with was, that unless I'm using the vector template class incorrectly, using vector is much less flexible than using a heap allocated array and good old memcpy.
#include <shlobj.h>
HRESULT ModifyTheme()
{
using namespace std;
vector <WCHAR> sOutput;
vector <WCHAR> sPath;
vector <WCHAR> sThemesLocation;
vector <WCHAR> sThemeName;
const WCHAR sThemesPath [] = _T("\\Microsoft\\Windows\\Themes");
const WCHAR sFileName [] = _T("\\darkblue.theme");
sOutput.resize(MAX_PATH);
sPath.resize( MAX_PATH );
sThemesLocation.resize( MAX_PATH );
sThemeName.resize( MAX_PATH );
// Get appdata\local folder
SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, &sPath[0] );
// copy consts to vectors
memcpy( &sThemesLocation[0], sThemesPath, sizeof(sThemesPath) );
memcpy( &sThemeName[0], sFileName, sizeof(sFileName) );
// append themes path & filename
sOutput.insert( sOutput.begin(), sPath.begin(), sPath.end() );
sOutput.insert( sOutput.end()-1, sThemesLocation.begin(), sThemesLocation.end() );
sOutput.insert( sOutput.end()-1, sThemeName.begin(), sThemeName.end() );
wcout << &sThemeName[0] << endl;
wcout << &sThemesLocation[0] << endl;
wcout << &sPath[0] << endl;
wcout << &sOutput[0] << endl;
return S_OK;
}
I'd expect the sOutput vector to contain a concatenation of all strings. Instead, it only contains the first inserted string.
Also, I think I remember hearing that although it's not possible to assign a vector's values in an initializer list, it may be a feature of c++0x. Is this correct - and is there any way (at the minute) to do the following:
vector<wchar> sBleh = { _T("bleh") };
Finally, for what I want to achive with the simple routine above, would I be better with a dynamically allocated array, or should I persist with the seemingly inflexible vector of wchar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是
std::vector
,您可能应该使用std::wstring
,因为它也是WCHAR
的容器元素。以下链接可能对您有帮助:
std::wstring(
std::basic_string<的类型定义) WCHAR>
)std::basic_string
If you're using
std::vector<WCHAR>
you should probably be usingstd::wstring
as it is also a container ofWCHAR
elements.The following links might help you:
std::wstring (typedef of
std::basic_string<WCHAR>
)std::basic_string
使用最适合工作的工具。有些情况需要使用静态数组,有些情况需要使用动态数组。当情况需要动态数组时,请使用向量。
Mark Ingram 是正确的,您可以使用 wstring,但前提是 wchar_t 与 WCHAR 大小相同。
这样的东西更适合你想要的(注意,我没有通过编译器运行下面的代码,因为有太多微软特定的构造。):
Use the best tool for the job. Some situations call for using static arrays and some for dynamic arrays. When the situation calls for a dynamic array, use a vector instead.
Mark Ingram is correct that you could use wstring, but only if wchar_t is the same size as WCHAR.
Something like this is better for what you want (note, I didn't run the below through a compiler because there are too many Microsoft specific constructs.):