从映射中取出Int值并将其迭代到二分图中
Taking Int values out of a map and iterating them into a bipartite graph C++
这对我来说相当复杂,希望对其他人来说不是这样。
我在一个映射中有一组256
键/值项。我通过ifstream
从文本文件中获取了地图上的数据。现在,我需要从映射中获取键/值对,我需要使用这些数据点来创建一个二部映射图。
我需要做的是迭代每个键和值,并将它们放入bpGraph
格式:
bool bpGraph[V][V] = { {0, 0, 0, 1, 0, ect.... 0}, //Key 1 Value 4
{0, 2, 0, 0, 0, ect.... 0}, // Key 2 Value 2
{0, 0},
{0, 0},
{0, 0},
{0, 0},
{0, 0}
};
基本上,我将查看映射并设置这个256x256数组中的所有256个值,并将正确的值设置为true,其中键为水平行,值为垂直行。
这是我当前的代码:
int main(){
ifstream myfile("H:\School\CSC-718\paths.txt");
std::map<int, int> mymap;
pair<int,int> me;
char commas;
int a, b;
vector<int> v;
while (myfile >> a >> commas >> b){
mymap[a] = b;
}
mymap;
bool bpGraph[M][N] = {
//...
};
cout << "Maximum number networks "
<< maxBPM(bpGraph);
return 0;
}
问题:
能给我一些关于实施的建议吗?
可以使用迭代器迭代映射。从迭代器中,你可以分别使用'first'和'second'成员提取键和值。然后你可以把这些值放到你的数组中。
对于下面的代码,迭代映射并打印每个键和相关的值:
void MapExample(void)
{
std::map<int, int> mymap;
mymap[1] = 11;
mymap[2] = 22;
mymap[3] = 33;
for (std::map<int,int>::iterator it=mymap.begin();it!=mymap.end(); ++it)
{
std::cout << it->first << "->" << it->second << "n";
}
}
上面打印
1 - 11>2 - 22>
3 - 33> 编辑:我不确定我是否理解正确,但如果我这样做,你想建立一个等效的数据表示,使用256x256数组而不是映射。
迭代映射你会这样做:
for (std::map<int,int>::iterator it=mymap.begin();it!=mymap.end(); ++it)
{
int key = it->first;
int value = it->second;
bpGraph[key-1][value-1] = 1; //the indexes to the array are zero based, so you need to subtract 1, if your keys and values belong to {1, ... ,256}
}
注意,需要将所有数组元素初始化为0。for循环执行后,只有当key (i+1)与value (j+1)相关联时,V[i][j]才=1。
相关文章:
- 基于函数而不是集合的二分搜索或迭代器?
- 使用迭代深度优先搜索算法的未加权图的最短路径
- 迭代提升反向图中的边缘
- C++提升图:如何通过迭代add_vertex
- 使用Vertex_descriptor boost图在顶点上打印或迭代
- C 通过模板图迭代
- boost图库图的构建;迭代地添加边属性
- 通过使用boost spirit qi解析器迭代填充BGL图
- 为什么Boost图库在删除顶点时会使所有迭代器无效
- 迭代多个多重贴图
- 包含向量的迭代器的无序列图 - 迭代器不可取消引用C++
- 柠檬图迭代边缘
- 在boost图lib中,如何在不迭代顶点的所有外边缘的情况下获得该顶点的特定外边缘
- C++指针、迭代器、图的邻接列表表示的问题
- 从映射中取出Int值并将其迭代到二分图中
- 用于枚举图中所有节点值的假迭代器操作
- 使用基于范围的for迭代图的边缘
- 二分搜索算法采用迭代和递归
- 迭代没有访问者的boost图
- 如何在一个完整图的每条边上迭代一次