C++ 在c+中的两个向量之间创建链接+;

C++ 在c+中的两个向量之间创建链接+;,c++,design-patterns,vector,C++,Design Patterns,Vector,这是一个概念性的问题,因此我不会为此提供“工作代码” 假设有两个不同类型和不同实体数的std::vector,例如: vector <int> A; vector <string> B; 向量A; 载体B; 一个人有一套规则,可以根据这些规则将a的任何成员与B的一些成员(或没有)关联起来 有没有办法“存储”这个连接 我认为这样做的方法之一是使用向量或向量,但在我看来,这种解决方案似乎不可靠(例如,如果a包含两个相同的数字)我假设那里有更优雅的解决方案。std::pai

这是一个概念性的问题,因此我不会为此提供“工作代码”

假设有两个不同类型和不同实体数的std::vector,例如:

vector <int> A;
vector <string> B;
向量A;
载体B;
一个人有一套规则,可以根据这些规则将a的任何成员与B的一些成员(或没有)关联起来

有没有办法“存储”这个连接


我认为这样做的方法之一是使用
向量
向量
,但在我看来,这种解决方案似乎不可靠(例如,如果a包含两个相同的数字)我假设那里有更优雅的解决方案。
std::pair
s的
std::multiset
可以将多个
int*
s映射到零或更多
std::string*
s:

std::multiset < std::pair<int*, std::vector<std::string*>>> map_A_to_B;

根据您的评论,您似乎想要一个通用(而不是一对一或一对一)的实际映射(如在数学中,从集合a到集合B)。首先,你必须从概念上理解你想要什么。首先,您需要类a(在您的示例中是int)到B(string)之间的映射。让我们用这个模板:

template <class From, class To>
bool isMapped(From A,To B) {
    return (//Code representing mapping,say check if A=int->char is in B=string)
}
这将返回
值在
向量中映射到的范围,如果重复项在该范围内出现多次,则返回重复项。另一个选项(可能更好)是迭代索引,而不是范围中的值,并返回长度为
range
的布尔向量,其中From映射到的索引中的
true

同样,您需要定义相反的映射。也许你不能让它完全通用,甚至可能模板也不能简单地适应它——你需要给出更多的细节

因此,从A到B的映射将是长度为B(域)的向量的长度为A(域)的向量,在相关索引中为真/假


当然,还有更多的可能性。

您可以使用Boost实现一个-这将允许您使用其中一个值作为键。但是,简而言之:(仅限用法,不含定义)

struct from{};//助推标签
typedef双向_映射::type bi_映射;
biu映射值;
插入(biu映射::value类型(123,“{”);
// ...
// ...
biu-map::迭代器it=values.get().find(123);
if(it!=values.end()){
您可以实现一些数据库技术:索引。将数据放入单个
向量
,然后为您想要索引数据或关联数据的每种方式创建
std::map

不是两个向量,而是一个结构向量:

struct Datum
{
  int value;
  string text;
};

// The database
std::vector<Datum> database;

// An index table by integer
std::map<int, // Key
         unsigned int vector_index> index_by_value;

// An index table, by text
std::map<std::string, // Key
         unsigned int index_into_vector> index_by text;
结构基准面
{
int值;
字符串文本;
};
//数据库
向量数据库;
//整数索引表
std::映射索引值;
//按文本的索引表
std::通过文本映射索引;

索引表为您提供了在数据库中查找内容的快速方法,而无需对数据库进行排序。

正如您所描述的,
std::map
似乎更合适。@πάνταῥεῖ , 这是一个实际需要的问题-快速检索a->b或b->a。因此,可能更复杂的解决方案会派上用场。此外,没有什么可以阻止我们在b中也有相同的对象:)提供更多信息-如果a(b)中有重复项,它们将如何映射到b(a)?或者实际上是指将向量位置/索引关联起来?这是一个关于函数(数学函数)的问题,然后是代码。@例如,每个字母都可以表示为一个数字。然后根据a中的数字是否包含特定字母(不区分大小写)将a中的数字链接到B中的字符串。vise versa操作(B->a)这样也会有意义
template <class From, class To>
bool isMapped(From A,To B) {
    return (//Code representing mapping,say check if A=int->char is in B=string)
}
template<class From, class To>
List<To>& getRange(From value, To range) {
    List<To> result();
    for (const auto& toValue : range) {
         if(isMapped(value,toValue) 
             result.push_back(toValue);
    return result;
struct from {}; // tag for boost

typedef bidirectional_map<int, std::string>::type bi_map;

bi_map values;
values.insert(bi_map::value_type(123, "{"));

// ...
// ...

bi_map::iterator it = values.get<from>().find(123);
if (it != values.end()) {
    cout << "Char #123 is " << it->second << endl;
    // and in the opposite case, where "it" is the result of:
    // values.get<to>().find("{")
    // it->second would be 123, so you have access to both items
}
struct Datum
{
  int value;
  string text;
};

// The database
std::vector<Datum> database;

// An index table by integer
std::map<int, // Key
         unsigned int vector_index> index_by_value;

// An index table, by text
std::map<std::string, // Key
         unsigned int index_into_vector> index_by text;