合并排序 std:string 中的字符

Merge sort the character in a std:string

本文关键字:字符 string 排序 std 合并      更新时间:2023-10-16

所以我正在尝试合并对字符串的字母进行排序,以便它们按顺序排列。大写并不重要,因为家庭作业不需要它。由于某种原因,我无法templet[index] = split[leftfirst].我得到一个"没有合适的转换函数从 std::string 到 char 存在"。这是我的合并函数

 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,如果要修改变量。