Dan*_*nte 2 c++ dictionary iterator
我收到此编译错误:
错误:一元
*'(have'int')的类型参数无效
_M_insert_unique_(end(), *__first);
我已经尝试过使用(*cols_it).first和(*cols_it).second我能想到的所有其他排列方式,但无法对其进行编译。我应该写什么?
这是一些代码:
#include <map>
#include <vector>
using std::map;
using std::vector;
void setZeroes(vector<vector<int> > &A) {
map<int,int> rows;
map<int,int> cols;
for (unsigned int x = 0; x < A[0].size(); x++) {
for (unsigned int y = 0; y < A.size(); y++) {
if (A[x][y] == 0) {
rows.insert(y,y); // error reported here
cols.insert(x,x);
}
}
}
map<int,int>::iterator rows_it = rows.begin();
map<int,int>::iterator cols_it = cols.begin();
while (rows_it != rows.end()) {
for (unsigned int i = 0; i < A[0].size(); i++) {
int val = rows_it->second;
A[val][i] = 0;
}
rows_it++;
}
while (cols_it != cols.end()) {
for (unsigned int i = 0; i < A.size(); i++) {
int val = cols_it->second;
A[i][val] = 0;
}
cols_it++;
}
}
Run Code Online (Sandbox Code Playgroud)
rows.insert(y,y);并期望它cols.insert(x,x);会奏效。std::map::insertstd::pair<>
你可以:
rows.insert(std::make_pair(y,y));
cols.insert(std::make_pair(x,x));
Run Code Online (Sandbox Code Playgroud)
或使用列表初始化(自C ++ 11起):
rows.insert({y,y});
cols.insert({x,x});
Run Code Online (Sandbox Code Playgroud)
或使用std :: map :: emplace(自C ++ 11起)代替:
rows.emplace(y,y);
cols.emplace(x,x);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1337 次 |
| 最近记录: |