请帮助:[警告]已弃用从字符串常量转换为' char *' [-Wwrite串]

时间:2015-10-16 23:01:27

标签: c++ warnings deprecated

这是我第一次来这里,而且我有点腌渍。我在课堂上有一个任务,询问用户他们想要排序哪个排序算法,我的教授已经为我们提供了程序的框架,我们所要做的就是编写3个排序算法的代码。它说在第41行我试图传递一个带有char数据类型的字符串(我的教授写道)并且我很难解决这个问题,因为我查看了类似的论坛帖子和那些人与我有同样的错误,解决方案没有工作。您能否看看该计划,看看有什么问题以及如何解决?我会非常感激。

#include <stdlib.h>
#include <time.h>
#include <string>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAX_SIZE = 1000000;

// Set this to true if you wish the arrays to be printed.
const bool OUTPUT_DATA = false;


void ReadInput(string& sortAlg, int& size);

void GenerateSortedData(int data[], int size);
void GenerateReverselySortedData(int data[], int size);
void GenerateRandomData(int data[], int size);
void GenerateNearlySortedData(int data[], int size);

void Sort(int data[], int size, string sortAlg, char* dataType);

void InsertionSort(int data[], int size);
void MergeSort(int data[], int lo, int hi);
void combine(int data[], int size, int lo, int hi, int mid);
void QuickSort(int data[], int lo, int hi);
int partition(int data[], int lo, int hi);

void Swap(int &x, int &y);

bool IsSorted(int data[], int size);
void printData(int data[], int size, string title);


int main(void)
{
    int size;
    string sortAlg;
    ReadInput(sortAlg, size);

    int * data = new int[size];

    GenerateSortedData(data, size);
    Sort(data, size, sortAlg, "Sorted Data");

    GenerateReverselySortedData(data, size);
    Sort(data, size, sortAlg, "Reversely Sorted Data");

    GenerateRandomData(data, size);
    Sort(data, size, sortAlg, "Random Data");

    GenerateNearlySortedData(data, size);
    Sort(data, size, sortAlg, "Nearly Sorted Data");

    cout << "\nProgram Completed Successfully." << endl;

    return 0;
}
/********************************************************************/




void ReadInput(string& sortAlg, int& size)
{
    cout << "  I:\tInsertion Sort" << endl;
    cout << "  M:\tMergeSort" << endl;
    cout << "  Q:\tQuickSort" << endl;
    cout << "Enter sorting algorithm: ";
    cin >> sortAlg;
    string sortAlgName;


    if(sortAlg == "I")
        sortAlgName = "Insertion Sort";
    else if(sortAlg == "M")
        sortAlgName = "MergeSort";
    else if(sortAlg == "Q")
        sortAlgName = "QuickSort";
    else {
        cout << "\nUnrecognized sorting algorithm Code: " << sortAlg << endl;
        exit(1);
}


    cout << "Enter input size: ";
    cin >> size;

    if(size < 1 || size > MAX_SIZE)
    {
        cout << "\nInvalid input size " << size
        << ". Size should be between 1 and " << MAX_SIZE << endl;
        exit(1);
    }

    cout << "\nSorting Algorithm: " << sortAlgName;
    cout << "\nInput Size = " << size << endl;
    cout << endl;

}
/******************************************************************************/




void GenerateSortedData(int data[], int size)
{
    int i;

    for(i=0; i<size; i++)
        data[i] = i * 3 + 5;
}
/*****************************************************************************/




void GenerateReverselySortedData(int data[], int size) 
{
    int i;

    for(i = 0; i < size; i++)
        data[i] = (size-i) * 2 + 3;
}
/*****************************************************************************/




void GenerateRandomData(int data[], int size)
{
    int i;

    for(i = 0; i < size; i++)
        data[i] = rand();
 }
/*****************************************************************************/




void GenerateNearlySortedData(int data[], int size)
{
    int i;

    GenerateSortedData(data, size);

    for(i=0; i<size; i++)
        if(i % 10 == 0)
            if(i+1 < size)
                data[i] = data[i+1] + 9;
}
/*****************************************************************************/




void Sort(int data[], int size, string sortAlg, char* dataType)
{

    cout << endl << dataType << ":";

    if (OUTPUT_DATA)
        printData(data, size, "Data before sorting:");



    // Sorting is about to begin ... start the timer!
    clock_t start = clock();


    if(sortAlg == "I")
        InsertionSort(data, size);
    else if(sortAlg == "M")
        MergeSort(data, 0, size-1);
    else if(sortAlg == "Q")
        QuickSort(data, 0, size-1);
    else
    {
        cout << "Invalid sorting algorithm!" << endl;
        exit(1);
    }

    // Sorting has finished ... stop the timer!
    clock_t end = clock();
    double elapsed = (((double) (end - start)) / CLOCKS_PER_SEC) * 1000;



    if (OUTPUT_DATA)
        printData(data, size, "Data after sorting:");


    if (IsSorted(data, size))
    {
        cout << "\nCorrectly sorted " << size << " elements in " << elapsed << "ms";
    }
    else
        cout << "ERROR!: INCORRECT SORTING!" << endl;
cout <<  "\n-------------------------------------------------------------\n";
   }
    /*****************************************************************************/




bool IsSorted(int data[], int size) 
{
    int i;

    for(i=0; i<(size-1); i++)
    {
        if(data[i] > data[i+1])
            return false;
    }
    return true;
}
/*****************************************************************************/




void InsertionSort(int data[], int size)
{
    //Write your code here
    int i, j, temp;

    for(i = 1; i < size; i++)                           //first element in the array
    {                                           
        temp = data[i];                                 //Data[i] values are stored in temp.
        while(j > 0 && data[j-1] > data[j])             //While j > 0 and the value of j-1 position is greater
        {                                               //than the value of position j, begin swap.
            temp = data[j];                             //Value of data[j] is moved to temp.
            data[j] = data[j-1];                        //The values of data[j-1] is moved to data[j].
            data[j-1] = temp;                           //Then the temp value is moved to the data[j-1] array.
            j--;                                        //Decrement j value.
        }
    }


}
/*****************************************************************************/




void MergeSort(int data[], int size)
{
    //Write your code here
    int hi, lo, mid;
    if(lo <= hi)
    {
        mid = (hi + lo)/2;                          //Pivot.
        MergeSort(data, lo, mid);                   //recurssively call lowerhalf of the array.
        MergeSort(data, mid+1, hi);                 //recurssively call upperhalf of the array.
        combine(data, size, lo, hi, mid);           //combine the array.    
    }


    return; 
}
void combine(int data[], int size, int lo, int hi, int mid)
{
    int temp[size];                                 
    int i = lo;                                     
    int j = mid+1;
    int k = lo;

    for(int i = lo; i <= hi; i++)
    {
        temp[i] = data[i];                          //store the values in data array into the temp array
    }
    while(i <= mid && j <= hi)                      
    {                                               
        if(temp[i] = temp[j])                       //if i value in the temp array is equal to the j value in the temp array
        {
            data[k] = temp[i];                      //move the temp[i] values into the main data[k] array
            i++;                                    //increment i by 1
        }
        else
        {
            data[k] = temp[j];                      //otherwise, move the temp[j] values into the main array
            j++;                                    //increment j by 1
        }
        k++;
    }
    while(i <= mid)
    {
        data[k] = temp[i];                          //while i value is <= to mid value, the temp[i] value is moved to data[k]
        k++;                                        //increment k
        i++;                                        //increment i
    }

}
/*****************************************************************************/




void QuickSort(int data[], int size, int lo, int hi)
{
    //Write your code here
    int q;
    if(lo>=hi)
    {
        q=partition(data, lo, hi);
        QuickSort(data, lo, (q-1));
        QuickSort(data, (q+1), hi);

    }
}
int partition(int data[], int lo, int hi)
{
    int temp;                                   //temp for swaping
    int i = lo;                             
    int j = hi;
    int pivot = data[(lo+hi)/2];                //pivot takes the end element
    while(i<=j)
    {
        while(data[i] < pivot)
        {
            i++;                                //left hand side partition
        }
        while(data[j] > pivot)
        {
            j--;                                //right hand side partition
        }
        if(i <= j)                              //swaping occurs
        {
            temp = data[i];                     //take data[i] and put it into temp
            data[i] = data[j];                  //take array sub data[j] values and put it into array sub data[i]
            data[j] = temp;                     //take the temp values and put it into arra sub data[j]
            i++;
            j--;
        }
    }


}
/*****************************************************************************/




void Swap(int &x, int &y)
{
    int temp = x;
    x = y;
    y = temp;
}
/*****************************************************************************/




void printData(int data[], int size, string title) {
    int i;

    cout << endl << title << endl;
    for(i=0; i<size; i++)
    {
        cout << data[i] << " ";
        if(i%10 == 9 && size > 10)
            cout << endl;
    }
}

1 个答案:

答案 0 :(得分:6)

字符文字为char const(&)[]

  • 因此,您无法将其传递给char*。相反,将它们传递为const char*,例如:

    void Sort(int data[], int size, string sortAlg, const char* dataType)
    
  • 在第265行有一个变量lenth数组(VLA)。那是C99 / C11的东西,而不是C ++。

  • 这里的作业是错误的:

    if(temp[i] = temp[j]) //if i value in the temp array is equal to the j value in the temp array
    

    if(temp[i] == temp[j])来比较整数。

  • partitionint partition(int data[], int lo, int hi))有Undefined Behaviour,因为您没有返回值(并且已使用)

  • 您缺少代码:

    //Write your code here
    int hi, lo, mid;
    if(lo <= hi)
    

    也是Undefined Behaviour,因为hilo在那里未初始化。

  • void InsertionSort(int data[], int size)

  • 中的内容相同
  • void Sort(int*, int, std::string, const char*)

  • 中的内容相同
  • void MergeSort(int data[], int lo, int hi);需要定义(已使用过)

  • void QuickSort(int data[], int size, int lo, int hi)有一个冗余的size参数,该参数在声明的原型中不存在(第27行)。删除它

至少编译代码,而不是为你完成实现:

Live on Coliru