Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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
在2D数组中按字母顺序对字符串数组进行排序(C++)_C++_Arrays_Sorting - Fatal编程技术网

在2D数组中按字母顺序对字符串数组进行排序(C++)

在2D数组中按字母顺序对字符串数组进行排序(C++),c++,arrays,sorting,C++,Arrays,Sorting,到目前为止,我已经编写了代码,不知道如何使用二维数组进行排序。基本上,一个函数用于对字符串数组进行排序,另一个函数用于交换两个字符串。任何帮助都将不胜感激。另外,我不允许使用C++ 11:/< /p> #include <iostream> #include <string> #include <algorithm> using namespace std; void input_name(string&); void sort_names(st

到目前为止,我已经编写了代码,不知道如何使用二维数组进行排序。基本上,一个函数用于对字符串数组进行排序,另一个函数用于交换两个字符串。任何帮助都将不胜感激。另外,我不允许使用C++ 11:/< /p>
#include <iostream>
#include <string>
#include <algorithm>


using namespace std;

void input_name(string&);
void sort_names(string&);
void repeat_pro(int&);
void sortArray(string, int);
int main() {

    string b_list[100][2];                                              
    string name;
    int choice;
    int count=0;

    cout << "Welcome to the Business Sorting Program!" << endl;
    do{
        input_name(name);
        b_list[count][1] = name;      
        count++; 
        repeat_pro(choice);
        cout<<"\n \n Your Businesses are:"<<endl;
            for(int j=0; j<count; j++){
                cout<<b_list[j][1]<<endl;  
            }
        cout << "\n\n";
    }while(choice == 0);
    cout << "Thanks for using this program"<<endl;


    return 0;
}


void input_name(string &name){
    cout << "Enter in the name of the business: ";
    getline(cin, name);
}

void sort_names(string &name){

}

void repeat_pro(int &choice){
  cout << "Do you want to enter in more names: ";
  string answ;
  cin>>answ;
  cin.ignore(1000,'\n');
  if (answ == "YES" || answ == "Y" || answ == "yes" || answ == "y"){
      choice = 0;
  }
  else {
      choice = 1;
  }
  }

从描述中我不清楚该程序真正试图解决什么问题。我假设它有点像一个两列的电子表格,第二列是用户输入的名称,但第一列是什么

假设您需要在数据进入时保持数组的排序顺序,只需进行二进制搜索,就可以对小数据集(如100个条目)进行线性搜索

// we don't have lambda before C++11
struct comparator {
  bool operator () (const string (&x)[2], const string (&y)[2]) const {
    return x[1] < y[1];
  }
};

//... omitted

string data[100][2];
int count = 0;
while (count < 100) {
    // no rvalue, move, rvo, etc. before C++11
    string name;
    input_name(name);
    // no type deduction and lambda
    string (*position)[2] =
            std::lower_bound(&data[0], &data[count], name, comparator());
    int index = position - &data[0];
    // simulate an vector::insert operation, but for our array
    for (int i = count; i > index; --i) {
        // before we had move in C++, we would do swap with an empty element.
        // in this case, the entry at data[count] is default constructed
        std::swap(data[i][1], data[i-1][1]);
    }
    data[index][1] = name;
}
//... omitted

当然,我们可以使用typedef使其更干净,但这就留给您了。

C++03有std::sort。它还有std::vector。最好避免使用原始数组。为什么使用2d数组?如果您有一对项目,请使用一对或一个结构对其进行分组。尤其难以解释原因,因为您的示例没有同时使用这两种方法。哪一个是分类的关键?IMO结构比pair更容易管理,但两者都是可行的。我必须使用数组,因为我的导师要求我使用数组。我在考虑用它的第二排来排列它们。但是我不知道如何使用Strings如果你不需要2d数组,这里有很多答案:我有;我没有看到任何b_列表[x][0],您是否引用了二维数组的第一列?