合并排序 std:string 中的字符
Merge sort the character in a std:string
所以我正在尝试对字符串的字母进行合并排序,以便它们按顺序排列。大写无关紧要,因为家庭作业不需要它。由于某种原因,我无法获得 templet[index] = split[leftfirst]
。我得到一个 "no suitable conversion function from std::string to char exists"。这是我的合并函数
void merge(string *split, int leftfirst, int leftlast, int rightfirst, int rightlast)
{
string templet;
int index = leftfirst;
int savefirst = leftfirst;
while ((leftfirst <= leftlast) && (rightfirst <= rightlast))
{
if (split[leftfirst] < split[rightfirst])
{
templet[index] = split[leftfirst];
leftfirst++;
}
else
{
templet[index] = split[rightfirst];
rightfirst++;
}
index++;
}
while (leftfirst <= leftlast)
{
templet[index] = split[leftfirst];
leftfirst++;
index++;
}
while (rightfirst <= rightlast)
{
templet[index] = split[rightfirst];
rightfirst++;
index++;
}
for (index = savefirst; index <= rightlast; index++)
split[index] = templet[index];
}
感谢任何帮助。
split
是一个 string*
,这意味着 split[some]
不会从字符串中获取字符,而是从字符串数组中获取字符串。
解决此问题的最简单方法是将函数定义更改为具有 string &split
,如果您想修改变量。
所以我正在尝试对字符串的字母进行合并排序,以便它们按顺序排列。大写无关紧要,因为家庭作业不需要它。由于某种原因,我无法获得 templet[index] = split[leftfirst]
。我得到一个 "no suitable conversion function from std::string to char exists"。这是我的合并函数
void merge(string *split, int leftfirst, int leftlast, int rightfirst, int rightlast)
{
string templet;
int index = leftfirst;
int savefirst = leftfirst;
while ((leftfirst <= leftlast) && (rightfirst <= rightlast))
{
if (split[leftfirst] < split[rightfirst])
{
templet[index] = split[leftfirst];
leftfirst++;
}
else
{
templet[index] = split[rightfirst];
rightfirst++;
}
index++;
}
while (leftfirst <= leftlast)
{
templet[index] = split[leftfirst];
leftfirst++;
index++;
}
while (rightfirst <= rightlast)
{
templet[index] = split[rightfirst];
rightfirst++;
index++;
}
for (index = savefirst; index <= rightlast; index++)
split[index] = templet[index];
}
感谢任何帮助。
split
是一个 string*
,这意味着 split[some]
不会从字符串中获取字符,而是从字符串数组中获取字符串。
解决此问题的最简单方法是将函数定义更改为具有 string &split
,如果您想修改变量。