C++ 快速排序字符串数组帮助c++;
这是我的密码。我不知道如何让代码运行。我对字符串的快速排序有问题C++ 快速排序字符串数组帮助c++;,c++,string,quicksort,C++,String,Quicksort,这是我的密码。我不知道如何让代码运行。我对字符串的快速排序有问题 #include <iostream> #include <fstream> #include <cstdlib> #include <time.h> #include <string> #include <ctime> #include <stdio.h> using namespace std; int count; template
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <time.h>
#include <string>
#include <ctime>
#include <stdio.h>
using namespace std;
int count;
template <class T>
void printArray(T ar[], int sz);
template <class T>
void bubbleSort(T ar[], int sz);
void quickSortMain(string items[], int ct);
void quickSort(string items[], int left, int right);
//////////////////////////////////////////////////////////////////////////
// Main Function Implementation
//////////////////////////////////////////////////////////////////////////
int main() {
int numOfData = 50000;
string line, temp;
ofstream resultFile;
ofstream tableFile;
double data[100][2];
string patient[numOfData];
ifstream dataFile("shufflePatient.txt");
int i;
int SIZE = 0;
cout << "Program to shuffle data" << endl << endl;
cout << "This program will calculate swapping processes and running time.";
/*Storing data*/
cout << "Reading data in process.." << endl;
if (dataFile.is_open()) {
i=-1;
while (dataFile.good()) {
getline (dataFile, line);
if (i>=0) patient[i] = line;
i++;
}
dataFile.close();
}
SIZE = 5;
quickSortMain(patient, 5);
/*Writing to file*/
cout << "Writing to file.." << endl;
resultFile.open ("test.txt");
for (int i=0 ; i<numOfData ; i++) {
resultFile << patient[i] << "\n";
}
resultFile.close();
system("pause");
return 0;
}
void quickSortMain(string items[], int ct)
{
quickSort(items, 0, ct-1);
}
void quickSort(string items[], int left, int right)
{
int i, j;
char *x;
string temp[10];
i = left;
j = right;
x = items[(left+right)/2];
do {
while((strcmp(items[i],x) < 0) && (i < right)) {
i++;
}
while((strcmp(items[j],x) > 0) && (j > left)) {
j--;
}
if(i <= j) {
strcpy(temp, items[i]);
strcpy(items[i], items[j]);
strcpy(items[j], temp);
i++;
j--;
}
} while(i <= j);
if(left < j) {
quickSort(items, left, j);
}
if(i < right) {
quickSort(items, i, right);
}
}
//----------------------------------------------------------------------------
// prints array of size size
//----------------------------------------------------------------------------
template <class T>
void printArray(T patient[], int size)
{
for(int i = 0; i < size; i++)
cout << patient[i] << " ";
cout << endl;
}
//----------------------------------------------------------------------------
// sorts array of size size by Bubble Sort method
//----------------------------------------------------------------------------
template <class T>
void bubbleSort(T patient[], int size) //returning an int
{
bool noChange = true;
for(int i = size; i > 0; i--)
{
noChange = true;
for(int j = 1; j < i; j++)
{
if(patient[j] < patient[j - 1])
{
swap(patient[j], patient[j-1]);
count = count + 1;
noChange = false;
}
}
if (noChange)
return ;
}
}
我变了
charx*代码>到字符串x代码>
我不知道这是否正确。现在我得到的错误是没有声明strcmp
和strcpy
。关于我下一步应该做什么有什么帮助吗?没错,现在就换
strcmp(items[i],x) < 0
到
及
到
等等
但实际上这是RTFM的一个例子。如果您有足够的能力编写快速排序例程,那么您应该有足够的能力查看std::string
的工作原理。能够访问参考信息对程序员来说是一项非常重要的技能。VLA不是标准的。字符串可以与
进行比较,并分配=
。无需strcmp
和strcpy
.1。摆脱左
和右
,如果你远程精通指针数学,你只需要len
。2.停止C++运行时函数为C++运行时提供的东西,如<代码>操作符也改变<代码>字符串TEMP(10);<代码>至字符串温度代码>虽然交换项目时,std::swap
会是一个更好的选择。@RetiredInja Right我错过了。我猜它过去是chartemp[10]代码>std::swap
是一个更好的选择,但我保留它是为了显示strcpy
和=
的等价性。也许OP可以通过查找std::swap
来练习他的研究技能。谢谢你的评论!!最后一个问题。你们能告诉我们我应该从哪里研究std::string和std::swap吗?我打开的大多数网站都没有在快速排序方面对这两个方面做过太多解释。@user2959974这一个没问题。查找STD::String和STD::SWAP,但查找它们自己,而不仅仅是QuaskSuff.@ USER 55974注释:该网站是所有与C++语言和标准库相关的网站。它比你在网上找到的任何东西都要好。
strcmp(items[i],x) < 0
items[i] < x
strcmp(items[j],x) > 0
items[j] > x
strcpy(temp, items[i]);
temp = items[i];