-1

Is this wrong method of declaring vector of vector

vector<vector<bool>> visited(100, vector<bool>(100,false));

getting error as:

 Line 5: Char 34: error: expected identifier before numeric constant
 vector<vector<bool>> visited(100, vector<bool>(100,false));

i declared it in starting of solution class

in below program

#include<iostream>
#include<vector>
using namespace std;


class Solution {
public:
    int nr,nc;
    //vector<vector<bool>> visited(1000,vector<bool>(1000,false)) ;
    vector<vector<bool>> visited(100, vector<bool>(100,false));
    bool dfs(vector<vector<char>>& board,string word,int i,int j,int cr){
        visited[i][j]=true;
        if(cr==word.length())
            return true;
        if(cr>word.length())
            return false;
        if(j+1<nc && board[i][j+1]==word[cr] && !visted[i][j+1])
            return dfs(board,word,i,j+1,cr+1);
        if(j-1>=0 && board[i][j-1]==word[cr] && !visited[i][j-1])
            return dfs(board,word,i,j-1,cr+1);
        if(i+1<nr && board[i+1][j]==word[cr] && !visited[i+1][j])
            return dfs(board,word,i+1,j,cr+1);
        if(i-1>=0 && board[i-1][j]==word[cr] && !visited[i-1][j])
            return dfs(board,word,i-1,j,cr+1);
        return false;
    }
   bool dfs_help(vector<vector<char>> &board,string word){
       for(int i=0;i<board.size();i++){
            for(int j=0;j<board[0].size();j++){
                    visited[i][j]=false;
                }
            }
        }



       for(int i=0;i<board.size();i++){
            for(int j=0;j<board[0].size();j++){
                if(board[i][j]==word[0]){
                    visited[i][j]=true;
                    if(dfs(board,word,i,j,1))
                        return true;
                }
            }
        }
    return false;
    }
    vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
        nr = board.size();
        nc = board[0].size();
        vector<string> ans;
        for(string v : words){
            if(dfs_help(board,v))
                ans.push_back(v);
        }
        sort(ans.begin(),ans.end());
        return ans;
    }
};
int main(){

}
humblefool
  • 31
  • 4
  • 1
    Please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – πάντα ῥεῖ May 02 '19 at 06:30
  • If you're going to use `std::vector`, then `#include `, not some other header. – PaulMcKenzie May 02 '19 at 06:31
  • only while competitive programming @πάνταῥεῖ – humblefool May 02 '19 at 06:32
  • There's no such thing like _"competitive programming"_. – πάντα ῥεῖ May 02 '19 at 06:32
  • 1
    @humblefool -- You want us to duplicate the issue -- well there is no such header as `` in standard C++, and it doesn't exist in Visual Studio, which is what is being used by many programmers. – PaulMcKenzie May 02 '19 at 06:33
  • @humblefool: Do you still get the error after replacing the headers? – P.W May 02 '19 at 06:40
  • Now I changed But problem still persist – humblefool May 02 '19 at 06:40
  • @humblefool Hint -- you do not construct member variables (vectors) like that. "Competitive programming" doesn't teach basic C++ skills. Use the [member initialization list](https://en.cppreference.com/w/cpp/language/initializer_list) – PaulMcKenzie May 02 '19 at 06:40
  • 1
    When you believe the type is relevant, try something similar with a primitive type before jumping to conclusions. You would encounter the same issue if you wrote `class A { int x(0);};`. – molbdnilo May 02 '19 at 07:33

1 Answers1

1

If you want to construct a member variable such as std::vector with sizing arguments, the way you do that is to use the member initialization list of the Solution constructor.

To do this, you should remove the line that is giving the error, and instead add a default constructor to Solution so that the member(s) can be initialized:

class Solution
{
   //...
   Solution() : visited(100, std::vector<bool>(100,false)), nr(0), nc(0) {}
   //...
};
PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45