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

标签 c++ pointers

好的,所以我有一个家庭作业要使用隐藏在函数中的两种不同类型对指针数组进行排序。我相信我知道如何实现冒泡排序,但我真的很难理解在不使用 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
}

我的代码无法构建文件,这让我抓狂。有人可以告诉我我做错了什么吗?错误消息只是说: 对 `insertionSort(std::string* (*) [100], int) 的 undefined reference

最佳答案

前向声明是这样的:

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

但是定义是这样的:

void insertionSort (string* p2employee_array, int elemCnt)
{
    ...
}

函数签名需要匹配。

void insertionSort (string* p2employee_array[2][MAXELEMENTS], int elemCnt)
{
    ...
}

关于c++ - 如何使用指针数组实现插入排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19847799/

相关文章:

string - 我如何在 Rust 中将 &str 转换为 ~str?

c++ - 如何从静态成员函数调用指向成员函数的指针?

c++ - 如何为自定义输入迭代器定义指针

c# - 尝试读取或写入 protected 内存。这通常表明其他内存已损坏。 C# 错误

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?

c++ - 带有信息的 CGAL 3D 周期性 Delaunay 三角剖分问题

C++ 库包含

c# - 我如何使用像 C# 这样的 C++ 枚举类型?

c++ - 返回值优化和私有(private)复制构造函数

c++ - 在c++中返回指向 vector 元素的指针