对字符串的指针数组进行排序

时间:2018-03-03 02:35:58

标签: c++ arrays string sorting

sortArray函数中的swap函数似乎无法正常工作。错误是: No viable overloaded operator[] for type 'string *' (aka 'basic_string<char, char_traits<char>, allocator<char> > *')

我设法找到输出中第一个的项目,但现在我只需要一点帮助,让其余的用户输入按预期显示。 回顾一下,程序需要从用户读入许多单字名称(每个Pokemon一个)并将名称存储在数组中。用户输入完姓名后,程序应显示用户输入的所有小宠物的列表,但按字母顺序排列。我输出的最远的是:

Welcome! How many Pokemon do you own?

4

Ok, enter the names!

Pikachu

Snorlax

Ekans

Squirtle

输出:

Thanks, here are the pokemon you entered: Ekans Ekans Ekans Ekans Program ended with exit code: 0

这是我的代码:

#include <iostream>
#include "playground.h"
using namespace std;

string findFirst(string *x, int start, int end)
{
    string first = x[0];
    for(int i=start; i <= end; i++)
    {
        if(x[i] < first)
        {
            first = x[i];
        }
    }
    return first;
}

void sortArray(string *items, int start, int end)
{
    for(int i=start; i<=end; i++)
    {
        string min = findFirst(items,i, end);
        swap(items[i], items[min]);
    }
}


int main()
{
    cout<<"Welcome! How many Pokemon do you own?"<<endl;
    int num = 0;
    cin >> num;

    cout<< "Ok, enter the names!"<<endl;
    string *names = new string[num];
    for(int i=0; i<num; i++)
    {
        cin>>names[i];
    }
    cout<<"Thanks, here are the pokemon you entered: ";
    for(int i=0; i<num; i++)
    {
        cout << sortArray(names, 0, num) << " ";
    }        

    return 0;
}

3 个答案:

答案 0 :(得分:2)

C ++就是不重新发明轮子。它专门用于编写库和通用代码,如STL(标准模板库)。

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;  // Kids, do not try this at home

int main() {
    cout << "Welcome! How many Pokemon do you own?" << endl;
    int num = 0;
    cin >> num;
    cout << "Ok, enter the names!" << endl;
    std::vector<std::string> names(num);
    for (auto &name: names) {
        cin >> name;
    }

    std::sort(names.begin(), names.end());

    cout << "Thanks, here are the pokemon you entered: ";
    for (auto name: names) {
        cout << name << " ";
    }
    cout << endl;
    return 0;
}

答案 1 :(得分:0)

好的,我会给你一个简单易懂的简单答案。

  1. 开始使用 STL 。大部分预构建功能都可以随时使用。
  2. 而不是使用#include<iostream>使用 #include<bits/stdc++.h>
  3. 现在回答您的问题,我会调整您的代码并使其更简单易懂。

    #include <bits/stdc++.h>//read the link that I provided above
    using namespace std;
    
    void sortArray(string names[],int num)
    {
      sort(names,names+num);//This is the function of the  STL  for which I 
      //included <bits/stdc++.h>.Read from link provided above.
      for(int i=0;i<num;i++)
      {
        cout<<names[i]<<endl;
      }
    }
       int main()
    {
      cout<<"Welcome! How many Pokemon do you own?"<<endl;
      int num = 0;
      cin >> num;
      cout<< "Ok, enter the names!"<<endl;
      string names[num];
      for(int i=0;i<num;i++)
      {
        cin>>names[i];
      }
      cout<<"Thanks, here are the pokemon you entered:"<<endl;  
      sortArray(names,num);
    }
    

    如果你什么都不懂,请告诉我。

答案 2 :(得分:-1)

//得到了它!!!!

#include <iostream>
using namespace std;

int findFirst(string *x, int start, int end)
{
    int first = start;
    for(int i=start; i <= end; i++)
    {
        if(x[i] < x[first])
        {
            first = i;
        }
    }
    return first;
}
/*
void swap(string &s1, string &s2) {
   string temp = s1;
   s1 = s2;
   s2 = temp;
}
*/
void sortArray(string *items, int start, int end)
{
    for(int i=start; i<=end; i++)
    {
        int min = findFirst(items,i, end);
        swap(items[i], items[min]);
    }
}


int main()
{
    cout<<"Welcome! How many Pokemon do you own?"<<endl;
    int num = 0;
    cin >> num;

    cout<< "Ok, enter the names!"<<endl;
    string *names = new string[num];
    for(int i=0; i<num; i++)
    {
        cin>>names[i];
    }
    sortArray(names, 0, num-1);
    cout<<"Thanks, here are the pokemon you entered: ";
    for(int i=0; i<num; i++)
    {
        cout << names[i] << " ";
    }
    cout << endl;


    return 0;
}