0

My question is pretty specific, I think. My program is supposed to store 10 names, and their corresponding phone numbers into an array. I want to sort the names/numbers by phone numbers, ascending order, and then output the array in the sorted order. Thanks for the help! Any and all help is appreciated. What I have so far:

using namespace std;

main()
{
    string name[10][2];
    int x;

    cout << "Please input 10 names";
    for(int i = 0; i < 10; i++){
        cin >> name[i][x];
    }
    int i = 0;
    cout << "Please input their corresponding numbers";
    for(x = 0; x < 10; x++){
        cin >> name[i][x];
    }
}

EDIT: Would it be possible(not as hassling) to instead do this but with a parallel array storing a string(name) and an int(number) and sort it by the int?(Of course, while keeping the names by their corresponding number) If so, how could I change it from a two-dimensional to a parallel array?(Or just pointing me in the right direction would be greatly appreciated) :)

  • Have you considered using a map instead? Seems like it would be perfect for you problem if I understand it correctly – TheQAGuy Apr 30 '17 at 01:07
  • I would write a class that holds the name, phone number and put the objects into a vector. Then use std::sort to sort the vectors based on a function that compares phone number. You can write logic to sort your 2d array but to me it's unwieldy. – SomeDude Apr 30 '17 at 01:13
  • No, I have not. I'm going through my C++ book and am currently on two-dimensional arrays. (Though it does not explain sorting well enough for me to understand) I haven't learned about maps yet, and am trying to strengthen my knowledge of arrays. – Griffinflame Apr 30 '17 at 01:15
  • In order to use std::sort on a list of objects it needs to have begin and end functions – SomeDude Apr 30 '17 at 01:16
  • You need to work out how to construct your array properly before trying to sort it. The first loop has undefined behaviour, since `x` is uninitialised but is being used as an array index. An uninitialised `int` is not (necessarily) automatically set to zero. – Peter Apr 30 '17 at 01:17

1 Answers1

1

You will want to use a vector of pairs. This is in this case more convenient than a map because you want to sort by value rather than by key. There are also methods to sort a map by value (see question Sorting std::map using value), but for this purpose a vector of pairs seems appropriate.

If you want to have a constant memory array instead of a dynamic memory array (std::vector) then use std::array<std::pair<std::string,int>, N>, where N is the number of elements.

Needs C++14 because of the template lambda.

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

int main()
{
  std::vector<std::pair<std::string,int>> phonebook;
  phonebook.reserve(10);

  std::cout << "Please input a name and a number\n";
  for (int i = 0; i < 10; ++i) {
    std::cout << "Name: ";
    std::string name;
    std::cin >> name;

    std::cout << "Number: ";
    int number;
    std::cin >> number;

    phonebook.push_back({name, number});
  }

  std::sort( std::begin(phonebook),
             std::end(phonebook),
             [] (auto a, auto b) { return a.second < b.second; });

  for (auto const& entry : phonebook)
  {
    std::cout << entry.first << ' ' << entry.second << '\n';
  }
}
Henri Menke
  • 10,705
  • 1
  • 24
  • 42