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

C++ 如何将来自不同向量的元素相互链接?,c++,vector,C++,Vector,我想写一个程序,读取向量中的名字。之后,它应该将年龄读入另一个向量。(完成了) 名称向量的第一个元素应该连接到年龄向量的第一个元素,因此如果我对名称向量使用任何类型的sort()函数,年龄向量也会被排序。 有什么方法可以轻松实现这一点吗 class Name_pairs { public: //member functions int read_names(); int read_ages(); int print(); private: vector<double

我想写一个程序,读取向量中的名字。之后,它应该将年龄读入另一个向量。(完成了) 名称向量的第一个元素应该连接到年龄向量的第一个元素,因此如果我对名称向量使用任何类型的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:\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;
}
类名\u对{
公众:
//成员函数
int read_names();
int read_ages();
int print();
私人:
媒介年龄;
载体名称;
};
int Name_pairs::read_names(){
cout>名称;){
名称。推回(名称);
}
cin.clear();

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

struct Name_pair {
     double age;
     string name;
};
然后可以使用
std::vector
和lambda

auto comparator = [](const Name_pair& first, const Name_pair& second){return first.age <second.age;};
下面是一个工作示例

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

如果您想通过使用单独的向量而不是单个类向量来实现,那么可以使用索引向量并对其进行排序

以下只是一个例子:

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

#include.

实现这一点的明显方法是使用包含年龄和名称的某个结构的单个向量。也许你想创建一个对象的单个向量,每个对象包含一个名称和一个年龄。and
int
是年龄的更好类型吗?或者你不想按年计算吗?@schorsch312我可以更改年龄在
int
中,但您可以输入您的“10,5”岁。@MatteoItalia是的,使用1个向量会更容易,但我想使用2个向量。谢谢您的回答,我会尝试的!
Georg
Hallo
Welt
#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();
}