如何将来自不同向量的元素相互链接

How to link elements from different vectors with each other?

本文关键字:向量 元素 链接 何将来 将来      更新时间:2023-10-16

我想编写一个在向量中读取名称的程序。之后,它应该将年龄读入另一个向量。(完了)名称向量的第一个元素应该连接到年龄向量的第一个元素,因此如果我在名称向量上使用任何类型的sort()函数,年龄向量也会被排序。有没有办法以简单的方式实现这一点?

 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:nt";
  for (int i = 0; i < name.size(); i++) {
    cout << name[i] << " nt"; 
  }
  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;
}

我认为你需要一个耦合类型的std::vector

struct Name_pair {
     double age;
     string name;
};

比你可以使用std::vector<Name_pair>和使用lambda

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

std::sort 对向量进行排序。

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

这是一个工作示例。

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

它打印

Georg
Hallo
Welt

如果要通过使用单独的向量而不是单个类向量来实现面向数据的设计,则可以使用 indeces 向量并对其进行排序。

以下只是一个示例:

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

可在此处测试。