【问题标题】:How to link elements from different vectors with each other?如何将来自不同向量的元素相互链接?
【发布时间】:2019-01-09 07:46:59
【问题描述】:

我想编写一个程序来读取向量中的名称。之后,它应该将年龄读入另一个向量。 (完成) name-vector 的第一个元素应该连接到 age-vector 的第一个元素,所以如果我在 name-vector 上使用任何类型的 sort() 函数,age-vector 也会被排序。 有什么方法可以轻松实现这一点?

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

【问题讨论】:

  • 实现这一点的明显方法是使用包含年龄和名称的某个结构的单个向量。
  • 也许您想创建一个对象向量,每个对象包含一个名称和一个年龄。
  • int 是更适合年龄的类型吗?或者你不喜欢用年数来计算?
  • @schorsch312 我可以在int 中更改年龄,但想法是您可以输入“10,5”岁。
  • @MatteoItalia 是的,使用 1 个向量会更容易,但我想使用 2 个向量。

标签: c++ vector


【解决方案1】:

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

struct Name_pair {
     double age;
     string name;
};

你可以使用 std::vector&lt;Name_pair&gt; 并使用 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

【讨论】:

  • 谢谢你的回答,我试试看!
【解决方案2】:

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

以下只是一个例子:

#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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-04
    • 2018-04-02
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多