简体   繁体   English

使用向量 <wchar> 而不是动态分配的wchar数组

[英]use vector<wchar> instead of dynamically allocated wchar array

The other day, I got told off (on stackoverflow!) for not using vector instead of a dynamically allocated wchar array. 前几天,我因为没有使用向量而不是动态分配的wchar数组而被告知(在stackoverflow上)。

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. 我想到的是,除非我错误地使用了vector模板类,否则使用vector的灵活性要比使用堆分配的数组和良好的旧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. 我希望sOutput向量包含所有字符串的串联。 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. 另外,我想我记得曾经听说过,尽管不可能在初始化列表中分配向量的值,但这可能是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? 最后,对于我想通过上面的简单例程实现的目标,使用动态分配的数组会更好,还是应该坚持看似不灵活的wchar向量?

If you're using std::vector<WCHAR> you should probably be using std::wstring as it is also a container of WCHAR elements. 如果您使用的是std::vector<WCHAR> ,则可能应该使用std::wstring因为它也是WCHAR元素的容器。

The following links might help you: 以下链接可能会帮助您:
std::wstring (typedef of std::basic_string<WCHAR> ) std :: wstringstd::basic_string<WCHAR>
std::basic_string 性病:: basic_string的

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. 标记英格拉姆(Mark Ingram)是正确的,您可以使用wstring,但前提是wchar_t与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.): 这样的事情对于您想要的更好(注意,我没有通过编译器运行以下命令,因为有太多Microsoft特定的构造。):

WCHAR sPath[MAX_PATH]; // doesn't need to be a dynamic array, so don't bother with a vector.
SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, &sPath[0] );

const WCHAR sThemesPath[] = _T("\\Microsoft\\Windows\\Themes"); // doesn't need to be a dynamic array, so don't bother with a vector.
const WCHAR sFileName[] = _T("\\darkblue.theme"); // doesn't need to be a dynamic array, so don't bother with a vector.
vector<WCHAR> sOutput; // this needs to be dynamic so use a vector.

// wcslen should probably be replaced with an MS specific call that gets the length of a WCHAR string
copy(sPath, sPath + wcslen(sPath), back_inserter(sOutput));
copy(sThemesPath, sThemesPath + wcslen(sThemesPath), back_inserter(sOutput));
copy(sFlieName, sFileName + wcslen(sFileName), back_inserter(sOutput));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM