在c++程序中将Map中的值复制到256x256数组
Copying values from a Map to a 256x256 array in a C++ program
我正在研究一个最大二部匹配算法。我有麻烦弄清楚如何设置值在一个数组基于键/值在地图。
最后我需要遍历行,这些行对应于映射mymap中的键。然后我需要根据mymap中的值将相应的列设置为true(1)。最终会是这样的
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}
};
目前我的算法是这样的,你可以看到我被如何遍历映射以在数组中设置适当的值难住了:
inline void keep_window_open() {char};cin>> ch;}//测试上述函数的驱动程序
int main()
{
ifstream myfile("H:\School\CSC-718\paths.txt");
std::map<int, int> mymap; // Create the map
pair<int,int> me; // Define the pair
char commas; // Address the comma in the file
int a, b;
vector<int> v;
while (myfile >> a >> commas >> b)
{
mymap[a] = b; // Transfer ints to the map
}
mymap;
//以上代码有效
bool bpGraph[M][N]; // Define my graph array
for (int i = 0; i <mymap.count; i++) // the plan is to iterate through the keys M
and set the appropriate value true on N
{
bool bpGraph[M][N] = {
};
}
cout << "Maximum number networks "
<< maxBPM(bpGraph); // run it through the BPM algorithim
keep_window_open();
return 0;
}
不能使用索引访问map的元素。您需要使用迭代器。在这种情况下,你可以使用更短的"for each"样式循环:
for (const auto &val: mymap) {
bpGraph[val.first][val.second] = true;
}
在执行此循环之前,必须将bpGraph
数组初始化为false。
相关文章:
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 使用strcpy将char数组的元素复制到另一个数组
- 是否可以初始化不可复制类型的成员变量(或基类)
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 复制列表初始化的隐式转换的等级是多少
- 当从函数参数中的临时值调用复制构造函数时
- 有可能在Armadillo中复制MATLAB circshift方法吗
- 复制几乎为空的数组的最快方法
- 以下示例中如何避免代码复制?C++/库达
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 为什么需要复制构造函数,在哪些情况下它们非常有用
- 不能将复制初始化与隐式转换的多个步骤一起使用
- 当有分配器意识的容器被复制/移动时,反弹分配器是否被复制/移走
- 为什么复制而不是移动数据元素?
- 文件系统:复制功能的速度秘诀是什么
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- 为什么类中的ostringstream类型的成员会导致";调用隐含删除复制构造函数";错误
- 在c++程序中将Map中的值复制到256x256数组