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

问题描述 投票:2回答:3

我的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;
}
c++ arrays string sorting
3个回答
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;
}

0
投票

好吧,我将给你一个简单明了的答案,这个答案很容易理解。

  1. 开始使用STL。它将拥有大多数可能随时都能派上用的预构建函数。
  2. 而不是使用#include<iostream>使用#include<bits/stdc++.h>

现在回答你的问题,我将调整你的代码并使其更简单,并且将变得易于理解。

#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);
}

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


-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;
}
© www.soinside.com 2019 - 2024. All rights reserved.