如何将不同向量中的元素相互链接?

时间:2019-01-09 07:46:59

标签: c++ vector

我想编写一个程序来读取向量中的名称。之后,它应该将年龄读入另一个向量。 (完成) 名称向量的第一个元素应连接到年龄向量的第一个元素,因此,如果我在名称向量上使用任何种类的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;
}

2 个答案:

答案 0 :(得分:4)

我认为您需要一个耦合类型的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

答案 1 :(得分:1)

如果您想通过使用单独的向量而不是单个类向量来实现data-oriented design,则可以使用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();
}

可测试的HERE