2

I want to write a program which reads names in a vector. After that it should read ages into another vector. (that's done) The first element of the name-vector should be connected to the first element of the age-vector, so if I use any kind of sort() function on the name-vector the age-vector gets sorted as well. Is there any way to realize this in an easy way?

 class Name_pairs {
public:
  //member functions
  int read_names();
  int read_ages();
  int print();

private:
  vector<double> age;
  vector<string> name;
};

int Name_pairs::read_names() {
  cout << "Please enter different names, you want to store in a vector:\n";
  for (string names; cin >> names;) {
    name.push_back(names);  
  }
  cin.clear();
  cout << "You entered following names:\n\t";
  for (int i = 0; i < name.size(); i++) {
    cout << name[i] << " \n\t"; 
  }
  return 0;
}

int Name_pairs::read_ages() {
  cout << "\nPlease enter an age for every name in the vector.\n";
  for (double ages; cin >> ages;) {
    age.push_back(ages);
  }
  return 0;
}
vveil
  • 321
  • 1
  • 9

2 Answers2

4

I think you need a std::vector of a coupled type.

struct Name_pair {
     double age;
     string name;
};

Than you can use std::vector<Name_pair> and use a lambda

auto comparator = [](const Name_pair& first, const Name_pair& second){return first.age <second.age;};

to sort your vector with std::sort.

Name_pairs = std::vector<Name_pair>;
// fill vector
std::sort(Name_pairs.begin(), Name_pairs.end(), comparator);

Here is a working example.

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

struct Name_pair {
    double age;
    std::string name;
};

int main() {
    std::vector<Name_pair> Name_pairs{{13, "Hallo"}, {32, "Welt"}, {1, "Georg"}};
    auto comparator = [](const Name_pair& first, const Name_pair& second) { return first.age < second.age; };
    std::sort(Name_pairs.begin(), Name_pairs.end(), comparator);

    for (const auto np : Name_pairs) {
        std::cout << np.name << "\n";
    }
}

It prints

Georg
Hallo
Welt
schorsch312
  • 5,553
  • 5
  • 28
  • 57
1

If you want to implement a data-oriented design by using separate vectors instead of a single vector of classes, you could use a vector of indeces and sort it.

The following is just an example:

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

class Customers
{
    std::vector<double> ages_;
    std::vector<std::string> names_;

    template <typename Comparator>
    auto make_indeces(Comparator &&comp)
    {
        std::vector<size_t> indeces(names_.size());
        std::iota(indeces.begin(), indeces.end(), 0);
        std::sort(indeces.begin(), indeces.end(), comp);
        return indeces;
    }

    template <typename Type>
    void reorder_vector(std::vector<Type> &src, std::vector<size_t> const &indeces)
    {
        std::vector<Type> tmp;
        tmp.reserve(src.size());
        std::generate_n(
            std::back_inserter(tmp), src.size(),
            [&src, idx = indeces.cbegin()] () mutable {
                return src[*(idx++)];
        });
        src = std::move(tmp);
    }

public:
    void add(std::string const &name, double age)
    {
        names_.push_back(name);
        ages_.push_back(age);
    }

    void sort_by_names()
    {
        auto indeces = make_indeces([this] (size_t i, size_t j) {
            return names_[i] < names_[j];
        });

        reorder_vector(names_, indeces);
        reorder_vector(ages_, indeces);
    }

    void show_sorted_by_ages()
    {
        auto indeces = make_indeces([this] (size_t i, size_t j) {
            return ages_[i] < ages_[j];
        });

        for (auto i : indeces)
            std::cout << names_[i] << ' ' << ages_[i] << '\n';
    }

    void show()
    {
        for (size_t i = 0; i < names_.size(); ++i)
            std::cout << names_[i] << ' ' << ages_[i] << '\n';
    }
};

int main(void)
{
    Customers c;
    c.add("Adam", 23);
    c.add("Eve", 21);
    c.add("Snake", 66.6);
    c.add("Apple", 3.14);

    std::cout << "Sorted by ages (doesn't modify the internal order):\n";
    c.show_sorted_by_ages();
    std::cout << "\nInternal order:\n";
    c.show();
    c.sort_by_names();
    std::cout << "\nInternal order after sorting by names:\n";
    c.show();
}

Testable HERE.

Bob__
  • 12,361
  • 3
  • 28
  • 42