{"title":"C++ Unordered Set Initialization Error: Fixing "in instantiation of default function argument expression" Issue","description":"This article provides a solution for a common error encountered when initializing an unordered_set in C++, specifically the "in instantiation of default function argument expression" error. It explains the cause of the error and demonstrates how to fix it by including the correct header file and namespace.","keywords":"C++, unordered_set, initialization, error, default function argument expression, header file, namespace, std","content":"This error typically occurs when using the default constructor of a container, often due to missing header files or namespaces. In this case, you need to include the <unordered_set> header file and ensure you're using the std namespace. Here's the corrected code:

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

class Solution {
public:
    int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
        int x_dir=0,y_dir=1,pos_s=0,pos_y=0;
        int maxway=0;
        unordered_set<vector<int>>hash_set{obstacles.begin(),obstacles.end()};
        for(int i=0;i<commands.size();i++){
            if(commands[i]<0){
                if(x_dir==0&&y_dir==1){
                    if(commands[i]==-1){x_dir=1,y_dir=0;}
                    if(commands[i]==-2){x_dir=-1,y_dir=0;}
                    }
                if(x_dir==1&&y_dir==0){
                    if(commands[i]==-1){x_dir=0,y_dir=-1;}
                    if(commands[i]==-2){x_dir=0,y_dir=1;}
                    }
                if(x_dir==0&&y_dir==-1){
                    if(commands[i]==-1){x_dir=-1,y_dir=0;}
                    if(commands[i]==-2){x_dir=1,y_dir=0;}
                    }
                if(x_dir==-1&&y_dir==0){
                    if(commands[i]==-1){x_dir=0,y_dir=-1;}
                    if(commands[i]==-2){x_dir=0,y_dir=1;}
                    }
            }
            else{
                while(commands[i]>0){
                    pos_s+=x_dir;pos_y+=y_dir;
                    if(hash_set.find({pos_s,pos_y})!=hash_set.end()){pos_s-=x_dir;pos_y-=y_dir;break;}
                    commands[i]--;
                }
                maxway=max(maxway,pos_s*pos_s+pos_y*pos_y);
            }
        }
        return maxway;
    }
};

By including the necessary header file and namespace, you'll resolve the "in instantiation of default function argument expression" error and successfully initialize your unordered_set.

C++ Unordered Set Initialization Error: Fixing

原文地址: https://www.cveoy.top/t/topic/pVo6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录