C++ 不使用内置的Sort()按字母顺序对字符串中的字母进行排序
函数C++ 不使用内置的Sort()按字母顺序对字符串中的字母进行排序,c++,sorting,C++,Sorting,函数sort必须对字符串中的字母进行排序。但不使用内置的C++ >代码> SoTo()/函数!我尝试过不同的方法,但没有任何效果。以下是此函数中的参数。需要对文本进行排序。(是否可以在不删除文本之前的和的情况下执行此操作?) 这段代码进入了无止境的循环 std::string TextUtility::sort(const std::string &text) { string toSort = text; string sorted_str; int size
sort
必须对字符串中的字母进行排序。但不使用内置的C++ >代码> SoTo()/<代码>函数!我尝试过不同的方法,但没有任何效果。以下是此函数中的参数。需要对文本进行排序。(是否可以在不删除文本之前的和的情况下执行此操作?)
这段代码进入了无止境的循环
std::string TextUtility::sort(const std::string &text)
{
string toSort = text;
string sorted_str;
int size = sizeof(toSort) / sizeof(char);
for (int i = 0; i < size - 1; i++)
{
if (toSort[i] > toSort[i + 1])
{
int temp = toSort[i + 1];
toSort[i + 1] = toSort[i];
toSort[i] = temp;
i = -1;
}
}
for (int i = 0; i < toSort.size(); i++)
{
sorted_str += toSort[i];
}
cout << endl
<< "Sorted: " << sorted_str;
return sorted_str;
}
std::string TextUtility::sort(常量std::string&text)
{
字符串toSort=文本;
字符串排序;
int size=sizeof(toSort)/sizeof(char);
对于(int i=0;itoSort[i+1])
{
int temp=toSort[i+1];
toSort[i+1]=toSort[i];
toSort[i]=温度;
i=-1;
}
}
对于(int i=0;i
#include <string>
#include <iostream>
std::string sort(const std::string &text)
{
std::string toSort = text;
for (int i = 0; i < (int)(toSort.size()) - 1; i++)
{
if (toSort.at(i) > toSort.at(i + 1))
{
char temp = toSort.at(i + 1);
toSort.at(i + 1) = toSort.at(i);
toSort.at(i) = temp;
i = -1;
}
}
std::cout << "Sorted: " << toSort << std::endl;
return toSort;
}
int main()
{
sort("fgfghafbkgdbGHKGHjbkgfnvnbjb14563f");
sort("X");
sort("");
return(0);
}
#包括
#包括
std::string排序(const std::string和text)
{
std::string toSort=文本;
对于(int i=0;i<(int)(toSort.size())-1;i++)
{
如果(在(i)处排序>在(i+1)处排序)
{
char temp=toSort.at(i+1);
toSort.at(i+1)=toSort.at(i);
toSort.at(i)=温度;
i=-1;
}
}
std::cout“是否可以在不删除文本之前的和的情况下执行此操作?”--当然可以。string toSort=text;
无论如何都会破坏常量引用的全部目的。与其删除一个问题并发布一个几乎相同的问题和其他详细信息,您可以添加详细信息。另外,您会惊讶地发现sizeof(toSort)无论字符串是空的还是包含了“战争和和平”的整体,都给出相同的结果。你的C++书应该有很多获得字符串长度的例子。对不起,我在StAcExcel中没有太多的经验。字符串有<代码> siz()/<代码>成员。<代码> siZeof(SoOTT)/ sieOf(char);< /C>完全错误。