在c++中创建两个向量之间的链接
Creating a link between two vectors in c++
这是一个概念性问题,因此我不提供"工作代码"。
假设有两个不同类型和不同数量实体的std::vector,例如:
vector <int> A;
vector <string> B;
一个人有一组规则,根据这些规则可以将a的任何成员与b的一些(或没有)成员关联起来
是否有一种方法来"存储"这个连接?
我认为这样做的方法之一是拥有vector <map <int, vector <string> > >
或vector <map <int, vector <string*> > >
,但这种解决方案对我来说似乎不可靠(例如,如果a包含两个相同的数字),我认为在那里有更优雅的解决方案。
您可以实现一些数据库技术:索引。将您的数据放入单个vector
中,然后为您希望索引数据或关联数据的每种方式创建std::map
。
不是2个向量,而是一个结构向量:
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;
索引表为您提供了在数据库中查找内容的快速方法,而不必对数据库进行排序。
std::pair
s的std::multiset
将能够将多个int*
s映射到零个或多个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
------------------------------
根据你的评论,似乎你想要一个实际的映射(如在数学中,从集合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)
}
现在,From
值到To
向量的映射(用数学术语来说)是"to"中可达的(isMapped
)范围:
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;
这将返回From
值在To
向量中映射到的范围,如果它们在该范围中出现多次,则返回重复的范围。另一种选择(可能更好)是迭代索引而不是范围内的值,并返回一个长度为range
的布尔向量,其中true
在From映射到的索引中。
由此得出,A到B的映射是长度为A的向量(定义域)的长度为B的向量(定义域)的向量,其相关指标为True/False。
当然还有更多的可能性
您可以使用Boost实现双向映射—这将允许您使用任何一个值作为键。下面是如何使用它的一个例子。但是,简而言之:(仅限用法,没有定义)
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
}
- 如何检查两个 std::向量在小于 O(n) 的时间复杂度内是否相等
- 查找两个排序向量中共有的元素
- 如何从文件中读取两个字符串和数字数组,并将它们存储在对象向量中
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- 在类 A 中创建类型为 B 类的向量 - 访问数据 [C++] [成员在两个类中都是私有的]
- 如何根据两个因素组织向量:id 和数量?(C++)
- 在C++中查找两个向量之间最相似的值
- 如何在C++中从两个向量生成所有可能的对?
- 返回两个向量 – 使用引用还是元组?
- 使用 std::vector::swap 方法在C++中交换两个不同的向量是否安全?
- 检查两个向量是否并行的最有效方法
- 有没有办法将两个或多个不同的类链接到一个类中(稍后在向量上使用)?
- 我有一个返回字符串向量的函数.它需要两个字符串,并且返回一个字符串中缺少的字符串
- 如何获得比较两个向量对的子集
- C++将两个不同类型的向量的属性连接到新的向量中
- 如何组合两个整数向量
- 在两个类实例之间共享向量
- 如何在 c++ 中对两个向量进行线性搜索?
- 如何在 c++ 中从两个向量创建 JSON 对象?
- 为什么具有两个元素的发起器语法将一个元素而不是两个元素放入字符串向量中?