stl的string 查找字符,看下面代码可以优化么?

小大小丑 2010-03-26 12:01:28
1. 查找字符
std::wstring strData = L"<result>[北京市, 上海市, 深圳市]</result>"
void DoF(const std::wstring &strData, std::list<std::wstring>& listDo)
{
std::wstring strTarB = L"<result>[";
std::wstring strTarE = L"]</result>";

std::wstring::size_type nBeginTempB = 0, nEndTempB = 0, nBeginTempE = 0, nEndTempE = 0;

nEndTempB = strData.find(strTarB, nBeginTempB);
nEndTempE = strData.find(strTarE, nBeginTempE);

if(nEndTempB != std::wstring::npos && nEndTempE != std::wstring::npos && nEndTempE > nEndTempB + strTarB.length())
{
std::wstring strTemp = strData.substr(nEndTempB + strTarB.length(), nEndTempE - nEndTempB - strTarB.length());
std::wstring strSub;
strTemp += L",";

std::list<std::wstring>::iterator it;
listDo.clear();
std::wstring::size_type nBegin = 0, nEnd = 0;
while( (nEnd = strTemp.find(L",", nBegin)) != std::wstring::npos)
{
strSub = strTemp.substr(nBegin, nEnd - nBegin);
if(strSub.length() > 0)
{
listDo.push_back(strSub);
}
nBegin = nEnd + 1;
}
}
}
...全文
107 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
SiGoYi 2010-03-26
  • 打赏
  • 举报
回复


// Establish string and get the first token:
token1 = strtok_s( string1, seps, &next_token1);
token2 = strtok_s ( string2, seps, &next_token2);

// While there are tokens in "string1" or "string2"
while ((token1 != NULL) || (token2 != NULL))
{
// Get next token:
if (token1 != NULL)
{
printf( " %s\n", token1 );
token1 = strtok_s( NULL, seps, &next_token1);
}
if (token2 != NULL)
{
printf(" %s\n", token2 );
token2 = strtok_s (NULL, seps, &next_token2);
}
}
这个能简单点
AlanBruce 2010-03-26
  • 打赏
  • 举报
回复
帮你UP…………
zw1987122 2010-03-26
  • 打赏
  • 举报
回复
lz结贴率120.00% 。。。。牛
小大小丑 2010-03-26
  • 打赏
  • 举报
回复
大家给点建议, 谢谢
wade_2003 2010-03-26
  • 打赏
  • 举报
回复
std::list<std::wstring>::iterator it;
这句好像没用吧,删掉
ArmStronger 2010-03-26
  • 打赏
  • 举报
回复
lz结贴率120.00% 。。。。牛
小大小丑 2010-03-26
  • 打赏
  • 举报
回复
up and up

64,662

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧