【发布时间】:2022-01-20 14:45:48
【问题描述】:
这是我的工作代码。我需要清除或释放 func() 中的 wstring、wstringstream、vector 吗?如果是这样,怎么做?我看到向量和 wstring 和 wstream 有一个 .clear() 函数。
这个示例程序是为了展示代码。我使用 wstringstream、wstring 和向量,其中我有一个分隔字符串,我需要提取并处理列表中的每个项目。
任何有关优化此代码和/或做家务的建议都非常感谢。
#include <windows.h>
#include <strsafe.h>
#include <vector>
#include <sstream>
using namespace std;
void func()
{
WCHAR sComputersInGroup[200] = L"PC1|PC2|PC3|PC4|";
WCHAR sSN[200]{};
wstringstream wSS(sComputersInGroup);
wstring wOut;
vector<wstring> vComputer;
while (wSS.good())
{
getline(wSS, wOut, L'|');
vComputer.push_back(wOut);
}
INT i = 0;
while (i < (INT) vComputer.size())
{
if (vComputer[i].length() > 0)
{
StringCchCopy(sSN, 16, vComputer[i].c_str());
}
i++;
}
}
int main()
{
for (INT i=0;i<20000;i++)
func();
}
【问题讨论】:
-
除非你使用
malloc/new,否则不需要free/delete。查找 RAII,因为提到的所有类型都是 RAII 类型,而漫画本身就是这样。 -
请阅读this。
标签: c++ vector namespaces stringstream