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

发布于 2024-11-01 17:28:01 字数 1679 浏览 0 评论 0原文

有一天,我被告知(在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱我好吗 2024-11-08 17:28:01

如果您使用的是 std::vector,您可能应该使用 std::wstring,因为它也是 WCHAR 的容器元素。

以下链接可能对您有帮助:
std::wstringstd::basic_string<的类型定义) WCHAR>)
std::basic_string

If you're using std::vector<WCHAR> you should probably be using std::wstring as it is also a container of WCHAR elements.

The following links might help you:
std::wstring (typedef of std::basic_string<WCHAR>)
std::basic_string

若沐 2024-11-08 17:28:01

使用最适合工作的工具。有些情况需要使用静态数组,有些情况需要使用动态数组。当情况需要动态数组时,请使用向量。

Mark Ingram 是正确的,您可以使用 wstring,但前提是 wchar_t 与 WCHAR 大小相同。

这样的东西更适合你想要的(注意,我没有通过编译器运行下面的代码,因为有太多微软特定的构造。):

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));

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.):

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文