Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.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++_Pointers - Fatal编程技术网

C++ 如何使用指针数组实现插入排序?

C++ 如何使用指针数组实现插入排序?,c++,pointers,C++,Pointers,好的,我有一个家庭作业,用函数中的两种不同排序对指针数组进行排序。我相信我知道如何实现冒泡排序,但我真的很难理解没有使用STL容器我必须做什么。以下是我已经得到的: #include <iostream> #include <string> #include <iomanip> #include <conio.h> #include <cstdlib> #include <fstream> #include <iost

好的,我有一个家庭作业,用函数中的两种不同排序对指针数组进行排序。我相信我知道如何实现冒泡排序,但我真的很难理解没有使用STL容器我必须做什么。以下是我已经得到的:

#include <iostream>
#include <string>
#include <iomanip>
#include <conio.h>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <cstring>
using namespace std;



//Enumeration
enum columns{SSN=0, LASTNAME=1};

//Global Constants
const int MAXELEMENTS = 100;

void insertionSort (string* [2][MAXELEMENTS], int);

int main ()
{

int index = 0;
int elemCnt = 0;
string employee_array[2][MAXELEMENTS];
string * p2employee_array [2] [MAXELEMENTS] = {{NULL}};
fstream inFile;


inFile.open("Lab4.csv", ios::in);
if (inFile.fail())
{
    cout<<"File Failed To Open"<<endl;
}

while(!inFile.eof())
{
    //Load the values array
    getline(inFile,employee_array[LASTNAME][index],',');
    getline(inFile,employee_array[SSN][index],'\n');

    //Load the pointers array
    p2employee_array[LASTNAME][index] = &employee_array[LASTNAME][index];
    p2employee_array[SSN][index] = &employee_array[SSN][index];
    elemCnt++;
    index++;
}

inFile.close();
if (inFile.fail())
{
    cout<<"File Failed To Close"<<endl;
}

//Construct a separator line
string line;
line = line.assign(64,'-');

//Display the original data
cout << "ORIGINAL DATA..." << endl << endl;
cout << setw(40) << "Employee Data" << endl
     << setw(40) << "Adrian Rodriguez" << endl << endl << endl;
cout << left << setw(40) << " " << setw(12) << "Address of" << "Address of"
     << endl
     << setw(20) << "SSN"
     << setw(20) << "Last Name" << setw(12) << "SSN" << "Last Name"
     << endl;
cout << line << endl << endl;

elemCnt = elemCnt;

for(index = 0; index<elemCnt; index++)
{
    cout << setw(20) << employee_array[SSN][index]
         << setw(21) << employee_array[LASTNAME][index]
         << setw(12) << p2employee_array[SSN][index]
         << setw(12) << p2employee_array[LASTNAME][index]
         << endl;
}
cout<<endl<<endl;
cout<<"About to sort arrays of pointers in ASCENDING ORDER on LAST NAME..."<<endl;


insertionSort (p2employee_array, elemCnt);
getch ();
return 0;
 }

 //************************************************

void insertionSort (string* p2employee_array, int elemCnt)
{
int j;
char* cmp; // cmp is a pointer to a C string
for( int i = 1; i < size; ++i )
{
cmp = (char*)p2employee_array[i].c_str();//pointer in cmp
j = i - 1;

while(j>=0 && strcmp(cmp,p2employee_array[j].c_str())<0)
{
  p2employee_array[j + 1] = p2employee_array[j]; 
    --j;
}
 p2employee_array[j + 1] = string(cmp); // insert
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//列举
枚举列{SSN=0,LASTNAME=1};
//全局常数
常量int最大元素=100;
void insertionSort(字符串*[2][MAXELEMENTS],int);
int main()
{
int指数=0;
int elemCnt=0;
字符串employee_数组[2][MAXELEMENTS];
字符串*P2U数组[2][MAXELEMENTS]={{NULL};
河道充填;
infle.open(“Lab4.csv”,ios::in);
if(infle.fail())
{

cout远期声明如下:

void insertionSort (string* [2][MAXELEMENTS], int);
void insertionSort (string* p2employee_array, int elemCnt)
{
    ...
}
但定义是:

void insertionSort (string* [2][MAXELEMENTS], int);
void insertionSort (string* p2employee_array, int elemCnt)
{
    ...
}
函数签名需要匹配