Since the main program is missing, the program is difficult to test. If the solution is empty, the program will certainly raise exceptions here.
int row = grid.size();
int col = grid[0].size();
This could easily be fixed by increasing the container grid to a minimum size. The same applies to the container visited.
int row = grid.size();
if (row == 0)
grid.resize(1);
int col = grid[0].size();
It is noticeable that the containers grid and visited always have to be the same size and maybe also belong in the solution.
There you could keep them consistent and always have access and size appropriate. The fields do not have to be transferred or created anew each time.
So I propose a member function InitGrid().
class Solution {
private:
vector<vector<char>> grid;
vector<vector<bool>> visited;
public:
vector<pair<int, int>> const movements = { { 1,0 },{ -1,0 },{ 0,1 },{ 0,-1 } };
bool InitGrid(int row, int col);
bool isValid(int i, int j, int n, int m);
int numIslands();
};
bool Solution::InitGrid(int row, int col)
{
...
return true;
}
For the isValid function, Rick's suggestion makes sense. Otherwise the program would be useless, but it wouldn't crash either.
Since the function declares a return value of type bool, it should also return true or false.