Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按字母顺序排列c字符串数组 我有一个家庭作业问题,我有点问题,我要求用C++来按字母顺序排序C字符串数组,排序ALGO使用的必须是冒泡排序。到目前为止,我所做的(复制如下)可以对数组进行排序,但只能基于第一个字母表。如何对字符串进行进一步排序-使用相同的初始字母表的字符串 <snipped>@arch:~/College/OOP/Lab/W3$ cat 2.cpp /* * Write a function which sorts an array of C strings in ascending order using bubble sort. The * number of strings in the array and the array must be passed as parameters to the function */ #include <iostream> #include <cstring> using namespace std; void sort(char **sar, unsigned num, unsigned len) { char *temp = new char[len]; if (temp == NULL) { cout << "\nOut-Of-Memory\n"; return; } for (unsigned a = 0; a < num-1; a++) { for (unsigned b = 0; b < ((num-a)-1); b++) { if (sar[b][0] > sar[b+1][0]) { strcpy(temp, sar[b]); strcpy(sar[b], sar[b+1]); strcpy(sar[b+1], temp); } } } delete[] temp; } int main(int argc, char *argv[]) { char **sar; unsigned num; unsigned len; cout << "Number of Strings: "; cin >> num; cout << "Length of Strings: "; cin >> len; cin.ignore(); // Flush buffer to fix a bug (getline after cin). sar = (char **) new char*[num]; if (sar == NULL) { cout << "\nOut-Of-Memory\n"; return -1; } for (unsigned i = 0; i < num; i++) { sar[i] = (char *) new char[len]; if (sar[i] == NULL) { // Let's pretend we 'know' memory management // because obviously modern OSs are incapable // of reclaiming heap from a quitting process.. for (unsigned j = 0; j < i; j++) delete[] sar[j]; cout << "\nOut-Of-Memory\n"; return -1; } } for (unsigned x = 0; x < num; x++) cin.getline(&sar[x][0], 512); sort(sar, num, len); cout << '\n'; for (unsigned y = 0; y < num; y++) cout << sar[y] << '\n'; for (unsigned z = 0; z < num; z++) delete[] sar[z]; delete[] sar; return 0; } @arch:~/College/OOP/Lab/W3$cat 2.cpp /* *编写一个函数,使用冒泡排序按升序对C字符串数组进行排序。这个 *数组和数组中的字符串数必须作为参数传递给函数 */ #包括 #包括 使用名称空间std; 无效排序(字符**sar、无符号num、无符号len) { char*temp=新字符[len]; if(temp==NULL) { coutsar[b+1][0]) { strcpy(温度,sar[b]); strcpy(sar[b],sar[b+1]); strcpy(sar[b+1],温度); } } } 删除[]临时; } int main(int argc,char*argv[]) { char**sar; 无符号数; 无符号len; cout>num; cout>len; cin.ignore();//刷新缓冲区以修复错误(cin之后的getline)。 sar=(字符**)新字符*[num]; 如果(sar==NULL) { 不能_C++_Arrays_String_Sorting - Fatal编程技术网 num; cout>len; cin.ignore();//刷新缓冲区以修复错误(cin之后的getline)。 sar=(字符**)新字符*[num]; 如果(sar==NULL) { 不能,c++,arrays,string,sorting,C++,Arrays,String,Sorting" /> num; cout>len; cin.ignore();//刷新缓冲区以修复错误(cin之后的getline)。 sar=(字符**)新字符*[num]; 如果(sar==NULL) { 不能,c++,arrays,string,sorting,C++,Arrays,String,Sorting" />

按字母顺序排列c字符串数组 我有一个家庭作业问题,我有点问题,我要求用C++来按字母顺序排序C字符串数组,排序ALGO使用的必须是冒泡排序。到目前为止,我所做的(复制如下)可以对数组进行排序,但只能基于第一个字母表。如何对字符串进行进一步排序-使用相同的初始字母表的字符串 <snipped>@arch:~/College/OOP/Lab/W3$ cat 2.cpp /* * Write a function which sorts an array of C strings in ascending order using bubble sort. The * number of strings in the array and the array must be passed as parameters to the function */ #include <iostream> #include <cstring> using namespace std; void sort(char **sar, unsigned num, unsigned len) { char *temp = new char[len]; if (temp == NULL) { cout << "\nOut-Of-Memory\n"; return; } for (unsigned a = 0; a < num-1; a++) { for (unsigned b = 0; b < ((num-a)-1); b++) { if (sar[b][0] > sar[b+1][0]) { strcpy(temp, sar[b]); strcpy(sar[b], sar[b+1]); strcpy(sar[b+1], temp); } } } delete[] temp; } int main(int argc, char *argv[]) { char **sar; unsigned num; unsigned len; cout << "Number of Strings: "; cin >> num; cout << "Length of Strings: "; cin >> len; cin.ignore(); // Flush buffer to fix a bug (getline after cin). sar = (char **) new char*[num]; if (sar == NULL) { cout << "\nOut-Of-Memory\n"; return -1; } for (unsigned i = 0; i < num; i++) { sar[i] = (char *) new char[len]; if (sar[i] == NULL) { // Let's pretend we 'know' memory management // because obviously modern OSs are incapable // of reclaiming heap from a quitting process.. for (unsigned j = 0; j < i; j++) delete[] sar[j]; cout << "\nOut-Of-Memory\n"; return -1; } } for (unsigned x = 0; x < num; x++) cin.getline(&sar[x][0], 512); sort(sar, num, len); cout << '\n'; for (unsigned y = 0; y < num; y++) cout << sar[y] << '\n'; for (unsigned z = 0; z < num; z++) delete[] sar[z]; delete[] sar; return 0; } @arch:~/College/OOP/Lab/W3$cat 2.cpp /* *编写一个函数,使用冒泡排序按升序对C字符串数组进行排序。这个 *数组和数组中的字符串数必须作为参数传递给函数 */ #包括 #包括 使用名称空间std; 无效排序(字符**sar、无符号num、无符号len) { char*temp=新字符[len]; if(temp==NULL) { coutsar[b+1][0]) { strcpy(温度,sar[b]); strcpy(sar[b],sar[b+1]); strcpy(sar[b+1],温度); } } } 删除[]临时; } int main(int argc,char*argv[]) { char**sar; 无符号数; 无符号len; cout>num; cout>len; cin.ignore();//刷新缓冲区以修复错误(cin之后的getline)。 sar=(字符**)新字符*[num]; 如果(sar==NULL) { 不能

按字母顺序排列c字符串数组 我有一个家庭作业问题,我有点问题,我要求用C++来按字母顺序排序C字符串数组,排序ALGO使用的必须是冒泡排序。到目前为止,我所做的(复制如下)可以对数组进行排序,但只能基于第一个字母表。如何对字符串进行进一步排序-使用相同的初始字母表的字符串 <snipped>@arch:~/College/OOP/Lab/W3$ cat 2.cpp /* * Write a function which sorts an array of C strings in ascending order using bubble sort. The * number of strings in the array and the array must be passed as parameters to the function */ #include <iostream> #include <cstring> using namespace std; void sort(char **sar, unsigned num, unsigned len) { char *temp = new char[len]; if (temp == NULL) { cout << "\nOut-Of-Memory\n"; return; } for (unsigned a = 0; a < num-1; a++) { for (unsigned b = 0; b < ((num-a)-1); b++) { if (sar[b][0] > sar[b+1][0]) { strcpy(temp, sar[b]); strcpy(sar[b], sar[b+1]); strcpy(sar[b+1], temp); } } } delete[] temp; } int main(int argc, char *argv[]) { char **sar; unsigned num; unsigned len; cout << "Number of Strings: "; cin >> num; cout << "Length of Strings: "; cin >> len; cin.ignore(); // Flush buffer to fix a bug (getline after cin). sar = (char **) new char*[num]; if (sar == NULL) { cout << "\nOut-Of-Memory\n"; return -1; } for (unsigned i = 0; i < num; i++) { sar[i] = (char *) new char[len]; if (sar[i] == NULL) { // Let's pretend we 'know' memory management // because obviously modern OSs are incapable // of reclaiming heap from a quitting process.. for (unsigned j = 0; j < i; j++) delete[] sar[j]; cout << "\nOut-Of-Memory\n"; return -1; } } for (unsigned x = 0; x < num; x++) cin.getline(&sar[x][0], 512); sort(sar, num, len); cout << '\n'; for (unsigned y = 0; y < num; y++) cout << sar[y] << '\n'; for (unsigned z = 0; z < num; z++) delete[] sar[z]; delete[] sar; return 0; } @arch:~/College/OOP/Lab/W3$cat 2.cpp /* *编写一个函数,使用冒泡排序按升序对C字符串数组进行排序。这个 *数组和数组中的字符串数必须作为参数传递给函数 */ #包括 #包括 使用名称空间std; 无效排序(字符**sar、无符号num、无符号len) { char*temp=新字符[len]; if(temp==NULL) { coutsar[b+1][0]) { strcpy(温度,sar[b]); strcpy(sar[b],sar[b+1]); strcpy(sar[b+1],温度); } } } 删除[]临时; } int main(int argc,char*argv[]) { char**sar; 无符号数; 无符号len; cout>num; cout>len; cin.ignore();//刷新缓冲区以修复错误(cin之后的getline)。 sar=(字符**)新字符*[num]; 如果(sar==NULL) { 不能,c++,arrays,string,sorting,C++,Arrays,String,Sorting,改变 if(sar[b][0]>sar[b+1][0]) 到 if(stricmp(sar[b],sar[b+1])>0) 更新:代替,您可以使用delete的无效用法。您应该使用delete[]。确切位置在哪里?valgrind不会对任何这样的事情嗤之以鼻。在排序函数中也是如此。接近结尾时,您在temp上调用delete。它应该是delete[]。谢谢,但这对我的问题并没有什么影响。将立即更新。这不是标准的,是吗?(另请参阅)非常感谢!我搜索了一些有关该函数的信息,并在g++手册中找到了一个等

改变

if(sar[b][0]>sar[b+1][0])

if(stricmp(sar[b],sar[b+1])>0)


更新:代替,您可以使用
delete
的无效用法。您应该使用
delete[]
。确切位置在哪里?valgrind不会对任何这样的事情嗤之以鼻。在排序函数中也是如此。接近结尾时,您在temp上调用
delete
。它应该是
delete[]
。谢谢,但这对我的问题并没有什么影响。将立即更新。这不是标准的,是吗?(另请参阅)非常感谢!我搜索了一些有关该函数的信息,并在g++手册中找到了一个等效的strcasecmp,可以实现这一点。