我无法对字符串进行标记,以便在迭代循环中将子字符串添加到向量中。
我在下面有这个。
当我运行它时,我从这个函数调用得到一个返回值1,我很确定它不准确。
serialized.find_first_of(categoryDelim, outterPrev)
代码
void Serialized::deserialize()
{
std::string serialized = "-1~-2~BK~123|~me|~you|~us|~9|~stuff|~";
char categoryDelim = '~';
char elemDelim = '|';
if (!serialized.empty())
{
//Make sure the container is empty
innerClear();
//do the work
std::vector<std::string>::iterator vecIt;
std::vector<std::vector<std::string>*>::iterator vecVecIt;
vecVecIt = vecVec.begin();
vecIt = (*vecVecIt)->begin();
//int categoryCount;
//int elemCount;
size_t innerPrev = 0;
size_t innerNext = 0;
size_t outterPrev = 0;
size_t outterNext = 0;
//Check to see whether there is another category delimter after the previous
while (outterNext = serialized.find_first_of(categoryDelim, outterPrev) != std::string::npos)
{
//Check to see whether there are more characters or category delimiters after the previous
while (innerNext = serialized.find_first_of(elemDelim, innerPrev) != std::string::npos &&
(serialized.find_first_of(elemDelim, innerPrev)+1 < serialized.find_first_of(categoryDelim, innerPrev)))
{
//Add the element to the current inner vector
(*vecVecIt)->push_back(serialized.substr(innerPrev, (innerNext - innerPrev)));
innerPrev = innerNext + 1;
}
//Advance to the next category delimiter and inner vector
outterPrev = outterNext + 1;
vecVecIt++;
}
}
}
答案 0 :(得分:3)
这是因为您使用了错误的条件:
outterNext = serialized.find_first_of(categoryDelim, outterPrev) != std::string::npos
平均
outterNext = (serialized.find_first_of(categoryDelim, outterPrev) != std::string::npos)
outterNext = 1
serialized.find_first_of(categoryDelim, outterPrev) != std::string::npos
我认为你应该避免这样的代码,它经常导致许多错误。使您的代码简单,有利于阅读和维护。