C++ 多次调用方法时出现奇数结果
我创建了一个类,该类的行为在某种程度上类似于std::string。我测试过的所有函数似乎都工作得很好,但我正在创建一个justify方法,该方法应该在已经存在的空白点中向字符串添加空格。该方法相对简单:C++ 多次调用方法时出现奇数结果,c++,C++,我创建了一个类,该类的行为在某种程度上类似于std::string。我测试过的所有函数似乎都工作得很好,但我正在创建一个justify方法,该方法应该在已经存在的空白点中向字符串添加空格。该方法相对简单: String String::justify(const int Width) const { String result = *this; int i = 0; if(nextBlank(0) == -1 || length() > Width)
String String::justify(const int Width) const
{
String result = *this;
int i = 0;
if(nextBlank(0) == -1 || length() > Width)
return *this;
while(result.length() < Width)
{
i = result.nextBlank(i);
if(i == -1) result.nextBlank(0);
result = result.substr(0,i) + ' ' + result.substr(i + 1, result.length());
i = result.nextNonBlank(i);
}
return result;
}
String字符串::justify(常量int-Width)常量
{
字符串结果=*this;
int i=0;
如果(下一个空格(0)=-1 | |长度()>宽度)
归还*这个;
while(result.length()
当调用这个方法时,我通常会得到一个奇怪的字符混合,因为我认为它会在某个地方通过空终止字符,但我不知道在哪里或为什么,这就是我希望你们能帮助我的地方。我相信这与substr方法有关,因为它的输出非常奇怪。即使不在justify函数中,如果substr被调用两次,输出也将不正确,但调用一次将正常工作
此外,以下是nextNonBlank、nextBlank和substr函数,因为它们是相关的:
int String::nextBlank(const int Index) const
{
int i = Index;
while(i <= length())
{
if(S[i] == ' ' && (i != Index || S[0] == ' '))
{
return i;
}
else if(i == length())
return -1;
++i;
}
return -1;
}
int String::nextNonBlank(const int Index) const
{
int i = Index;
while(i <= length())
{
if(S[i] != ' ' && i != Index )
{
return i;
}
++i;
}
return -1;
}
String String::substr(int Start, int End) const
{
String result;
assert(End <= length());
for(int i = Start; i <= End; ++i)
result = result + S[i];
return result;
}
int String::nextBlank(const int Index)const
{
int i=指数;
而(i您的substr()
函数有问题。添加了一些用于调试的变量,并使用两个不同的字符串运行。以下是每个函数的第一次迭代:
while(result.length() < Width)
{
i = result.nextBlank(i);
String temp1 = result.substr(0,i);
String temp2 = " ";
String temp3 = result.substr(i + 1, result.length());
int tmpint = result.length();
result = result.substr(0,i) + " " +result.substr(i + 1, result.length());
i = result.nextNonBlank(i);
}
while(result.length()
i:1
tmpint:34
结果:0x603450“没有起始空格的字符串”
tmp1:0x6038b0“带起始空格\021\001A”
tmp2:0x603670“”
tmp3:0x603ad0“xÿ[÷ÿ\177无起始空格的字符串”
i:0
tmpint:24
结果:0x603450“带起始空格”
tmp1:0x6038b0“xÿ[÷ÿ\177xÿ[÷ÿ\177xÿ[÷ÿ\177xÿ[÷ÿ\177\021\001”
tmp2:0x603690“”
tmp3:0x603ad0“带起始空格”
请提供您获取的测试样本输入和样本输出更改断言(结束您的字符串::字符串(const char*Temp)在Temp超过默认字符串容量时具有未定义的行为。这也适用于字符串::运算符+(const String&Rhs)const.result必须具有LHS+RHS的容量!在答案中保留空白量的最佳方法是什么?我在这里使用了
while(result.length() < Width)
{
i = result.nextBlank(i);
String temp1 = result.substr(0,i);
String temp2 = " ";
String temp3 = result.substr(i + 1, result.length());
int tmpint = result.length();
result = result.substr(0,i) + " " +result.substr(i + 1, result.length());
i = result.nextNonBlank(i);
}
i: 1
tmpint: 34
result: 0x603450 "A string without a beginning space"
tmp1: 0x6038b0 " with beginning space\021\001A "
tmp2: 0x603670 " "
tmp3: 0x603ad0 "x¦[÷ÿ\177string without a beginning space"
i: 0
tmpint: 24
result: 0x603450 " with beginning space"
tmp1: 0x6038b0 "x¦[÷ÿ\177x¦[÷ÿ\177x¦[÷ÿ\177x¦[÷ÿ\177\021\001 "
tmp2: 0x603690 " "
tmp3: 0x603ad0 " with beginning space"