甲std::pair
的std::multiset
s就能够多int*
点地图到零个或多个std::string*
S:
std::multiset < std::pair<int*, std::vector<std::string*>>> map_A_to_B;
实施例:
#include <set>
#include <vector>
#include <string>
#include <utility>
#include <iostream>
int main()
{
std::vector<int> A{3,3,1,5};
std::vector<std::string> B{"three a", "three b", "one", "five", "unknown"};
std::multiset < std::pair<int*, std::vector<std::string*>>> map_A_to_B{
{&A[0],{&B[0],&B[1]}},
{&A[1],{&B[0],&B[1],&B[4]}},
{&A[2],{&B[2]}},
{&A[3],{&B[3]}},
};
for(auto e : map_A_to_B) {
for(auto s : e.second) {
std::cout << *e.first << " linked to " << *s << '\n';
}
std::cout << "------------------------------\n";
}
}
生产:
3 linked to three a
3 linked to three b
------------------------------
3 linked to three a
3 linked to three b
3 linked to unknown
------------------------------
1 linked to one
------------------------------
5 linked to five
------------------------------
正如你所描述的,一个'std :: map>似乎更合适。 –
@πάνταῥεῖ,这是一个实际需要的事情 - 快速检索a-> b或b-> a。因此,更复杂的解决方案将派上用场。 此外,没有什么能阻止我们在B中拥有相同的对象:) – greyxray
提供更多信息 - 如果A(B)中有重复,他们将如何映射到B(A)?或者实际上是否意味着你将矢量位置/索引关联起来?这是一个关于函数(数学)然后是代码的更多问题。例如 – kabanus