甲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