简体   繁体   中英

Sorting a pointer array of strings

The swap function in my sortArray function doesn't seem to work properly. Error is: No viable overloaded operator[] for type 'string *' (aka 'basic_string<char, char_traits<char>, allocator<char> > *') .

I managed to find the item that goes first in the output, but now I just need a bit of help getting the rest of the user input to display as intended. To recap, the program needs to read in that many single-word names (one for each Pokemon) from the user and store the names in an array. After the user has finished entering their names, the program should display the list of all the pokemons the user entered, but in alphabetical order. The farthest I've got for the output was:

Welcome! How many Pokemon do you own?

4

Ok, enter the names!

Pikachu

Snorlax

Ekans

Squirtle

Output:

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

Here is my code:

#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++ is all about not re-inventing wheels. It was specifically designed for writing libraries and generic code like the STL (Standard Template Library).

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

Alright, I'm going to give you a plain and simple answer that'll be very easy to understand.

  1. Start using STL .It'll have most of the pre build functions that might come handy all the time.
  2. Instead of using #include<iostream> use #include<bits/stdc++.h> .

Now coming to your question, I'll tweak your code and make it simpler and will become easy to understand.

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

Let me know if you don't understand anything.

//Got IT!!!!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM