C++ 如何从函数返回值
我使用一个函数来计算我在地图中初始化的某些指令的信息,如下所示C++ 如何从函数返回值,c++,function,return-value,C++,Function,Return Value,我使用一个函数来计算我在地图中初始化的某些指令的信息,如下所示 void get_objectcode(char*&token1,const int &y) { map<string,int> operations; operations["ADD"] = 18; operations["AND"] = 40; operations["COMP"] = 28; operations["DIV"] = 24; operations
void get_objectcode(char*&token1,const int &y)
{
map<string,int> operations;
operations["ADD"] = 18;
operations["AND"] = 40;
operations["COMP"] = 28;
operations["DIV"] = 24;
operations["J"] = 0X3c;
operations["JEQ"] =30;
operations["JGT"] =34;
operations["JLT"] =38;
operations["JSUB"] =48;
operations["LDA"] =00;
operations["LDCH"] =50;
operations["LDL"] =55;
operations["LDX"] =04;
operations["MUL"] =20;
operations["OR"] =44;
operations["RD"] =0xd8;
operations["RSUB"] =0x4c;
operations["STA"] =0x0c;
operations["STCH"] =54;
operations["STL"] =14;
operations["STSW"] =0xe8;
operations["STX"] =10;
operations["SUB"] =0x1c;
operations["TD"] =0xe0;
operations["TIX"] =0x2c;
operations["WD"] =0xdc;
if ((operations.find("ADD")->first==token1)||(operations.find("AND")->first==token1)||(operations.find("COMP")->first==token1)
||(operations.find("DIV")->first==token1)||(operations.find("J")->first==token1)||(operations.find("JEQ")->first==token1)
||(operations.find("JGT")->first==token1)||(operations.find("JLT")->first==token1)||(operations.find("JSUB")->first==token1)
||(operations.find("LDA")->first==token1)||(operations.find("LDCH")->first==token1)||(operations.find("LDL")->first==token1)
||(operations.find("LDX")->first==token1)||(operations.find("MUL")->first==token1)||(operations.find("OR")->first==token1)
||(operations.find("RD")->first==token1)||(operations.find("RSUB")->first==token1)||(operations.find("STA")->first==token1)||(operations.find("STCH")->first==token1)||(operations.find("STCH")->first==token1)||(operations.find("STL")->first==token1)
||(operations.find("STSW")->first==token1)||(operations.find("STX")->first==token1)||(operations.find("SUB")->first==token1)
||(operations.find("TD")->first==token1)||(operations.find("TIX")->first==token1)||(operations.find("WD")->first==token1))
{
int y=operations.find(token1)->second;
//cout<<hex<<y<<endl;
}
return ;
}
void get_objectcode(字符*&token1,常量int&y)
{
地图操作;
操作[“添加”]=18;
操作[“和”]=40;
操作[“COMP”]=28;
操作[“DIV”]=24;
操作[“J”]=0X3c;
操作[“JEQ”]=30;
运营[“JGT”]=34;
操作[“JLT”]=38;
操作[“JSUB”]=48;
操作[“LDA”]=00;
操作[“LDCH”]=50;
操作[“LDL”]=55;
操作[“LDX”]=04;
操作[“MUL”]=20;
操作[“或”]=44;
操作[“RD”]=0xd8;
操作[“RSUB”]=0x4c;
操作[“STA”]=0x0c;
操作[“STCH”]=54;
操作[“STL”]=14;
操作[“STSW”]=0xe8;
操作[“STX”]=10;
操作[“子”]=0x1c;
操作[“TD”]=0xe0;
操作[“TIX”]=0x2c;
操作[“WD”]=0xdc;
if((operations.find(“ADD”)->first==token1)| |(operations.find(“AND”)->first==token1)| |(operations.find(“COMP”)->first==token1)
||(operations.find(“DIV”)->first==token1)| |(operations.find(“J”)->first==token1)| |(operations.find(“JEQ”)->first==token1)
||(operations.find(“JGT”)->first==token1)| |(operations.find(“JLT”)->first==token1)| |(operations.find(“JSUB”)->first==token1)
||(operations.find(“LDA”)->first==token1)| |(operations.find(“LDCH”)->first==token1)| |(operations.find(“LDL”)->first==token1)
||(operations.find(“LDX”)->first==token1)| |(operations.find(“MUL”)->first==token1)| |(operations.find(“OR”)->first==token1)
||(operations.find(“RD”)->first==token1)| |(operations.find(“RSUB”)->first==token1)| |(operations.find(“STA”)->first==token1)| |(operations.find(“STCH”)->first==token1)|(operations.find(“STL”)->first==token1)
||(operations.find(“STSW”)->first==token1)| |(operations.find(“STX”)->first==token1)| |(operations.find(“SUB”)->first==token1)
||(operations.find(“TD”)->first==token1)| |(operations.find(“TIX”)->first==token1)| |(operations.find(“WD”)->first==token1))
{
int y=operations.find(token1)->second;
//cout函数中的第二个参数是常量引用。请尝试替换-
void get_objectcode(char*&token1,const int &y)
与
在if条件下,删除新的y声明并替换为-
y=operations.find(token1)->second;
希望这有帮助!函数中的第二个参数是常量引用。请尝试替换-
void get_objectcode(char*&token1,const int &y)
与
在if条件下,删除新的y声明并替换为-
y=operations.find(token1)->second;
希望这有帮助!这可能更接近您想要的:
void get_objectcode(const char *token, int &y)
{
typedef std::map<std::string,int> OpMap;
OpMap operations;
operations["ADD" ] = 18;
operations["AND" ] = 40;
operations["COMP"] = 28;
operations["DIV" ] = 24;
// etc.
operations["SUB" ] = 0x1c;
operations["TD" ] = 0xe0;
operations["TIX" ] = 0x2c;
operations["WD" ] = 0xdc;
OpMap::iterator result = operations.find(token);
// note: assigns 0 to y if token is not found
y = (result == operations.end()) ? 0 : result->second;
//std::cout << std::hex << y << std::endl;
}
void get_objectcode(const char*token,int&y)
{
typedef std::map OpMap;
OpMap操作;
操作[“添加”]=18;
操作[“和”]=40;
操作[“COMP”]=28;
操作[“DIV”]=24;
//等等。
操作[“子”]=0x1c;
操作[“TD”]=0xe0;
操作[“TIX”]=0x2c;
操作[“WD”]=0xdc;
迭代器结果=operations.find(令牌);
//注意:如果找不到令牌,则将0分配给y
y=(result==operations.end())?0:result->second;
//std::cout这可能更接近您想要的:
void get_objectcode(const char *token, int &y)
{
typedef std::map<std::string,int> OpMap;
OpMap operations;
operations["ADD" ] = 18;
operations["AND" ] = 40;
operations["COMP"] = 28;
operations["DIV" ] = 24;
// etc.
operations["SUB" ] = 0x1c;
operations["TD" ] = 0xe0;
operations["TIX" ] = 0x2c;
operations["WD" ] = 0xdc;
OpMap::iterator result = operations.find(token);
// note: assigns 0 to y if token is not found
y = (result == operations.end()) ? 0 : result->second;
//std::cout << std::hex << y << std::endl;
}
void get_objectcode(const char*token,int&y)
{
typedef std::map OpMap;
OpMap操作;
操作[“添加”]=18;
操作[“和”]=40;
操作[“COMP”]=28;
操作[“DIV”]=24;
//等等。
操作[“子”]=0x1c;
操作[“TD”]=0xe0;
操作[“TIX”]=0x2c;
操作[“WD”]=0xdc;
迭代器结果=operations.find(令牌);
//注意:如果找不到令牌,则将0分配给y
y=(result==operations.end())?0:result->second;
//std::cout查看boost assign map\u list\u of。它可用于分配映射。然后使用映射中的find方法(参考是)查看boost assign map\u list\u of。它可用于分配映射。然后使用映射中的find方法(参考是)您的映射仅存在于该函数中。因此,元素仅存在于该函数中。如果在调用该函数的位置,您正在使用y
初始化引用,则该引用将是对不再存在的元素的引用
您不应该每次调用函数时都真正创建映射,至少最好只是正常地从函数返回值:
std::map<std::string,int> operations;
operations["ADD"] = 18;
operations["AND"] = 40;
operations["COMP"] = 28;
operations["DIV"] = 24;
operations["J"] = 0X3c;
operations["JEQ"] = 30;
operations["JGT"] = 34;
operations["JLT"] = 38;
operations["JSUB"] = 48;
operations["LDA"] = 00;
operations["LDCH"] = 50;
operations["LDL"] = 55;
operations["LDX"] = 04;
operations["MUL"] = 20;
operations["OR"] = 44;
operations["RD"] = 0xd8;
operations["RSUB"] = 0x4c;
operations["STA"] = 0x0c;
operations["STCH"] = 54;
operations["STL"] = 14;
operations["STSW"] = 0xe8;
operations["STX"] = 10;
operations["SUB"] = 0x1c;
operations["TD"] = 0xe0;
operations["TIX"] = 0x2c;
operations["WD"] = 0xdc;
int get_objectcode(const std::string& key)
{
std::map<std::string, int>::iterator it = operations.find(key);
if (it == operations.end())
return -1;
else
return it->second;
}
std::map操作;
操作[“添加”]=18;
操作[“和”]=40;
操作[“COMP”]=28;
操作[“DIV”]=24;
操作[“J”]=0X3c;
操作[“JEQ”]=30;
运营[“JGT”]=34;
操作[“JLT”]=38;
操作[“JSUB”]=48;
操作[“LDA”]=00;
操作[“LDCH”]=50;
操作[“LDL”]=55;
操作[“LDX”]=04;
操作[“MUL”]=20;
操作[“或”]=44;
操作[“RD”]=0xd8;
操作[“RSUB”]=0x4c;
操作[“STA”]=0x0c;
操作[“STCH”]=54;
操作[“STL”]=14;
操作[“STSW”]=0xe8;
操作[“STX”]=10;
操作[“子”]=0x1c;
操作[“TD”]=0xe0;
操作[“TIX”]=0x2c;
操作[“WD”]=0xdc;
int get_对象代码(const std::string和key)
{
std::map::iterator it=operations.find(键);
if(it==operations.end())
返回-1;
其他的
返回->秒;
}
您的映射仅存在于该函数中。因此,元素仅存在于该函数中。如果在调用该函数的位置,您正在使用y
初始化引用,则该引用将是对不再存在的元素的引用
您不应该每次调用函数时都真正创建映射,至少最好只是正常地从函数返回值:
std::map<std::string,int> operations;
operations["ADD"] = 18;
operations["AND"] = 40;
operations["COMP"] = 28;
operations["DIV"] = 24;
operations["J"] = 0X3c;
operations["JEQ"] = 30;
operations["JGT"] = 34;
operations["JLT"] = 38;
operations["JSUB"] = 48;
operations["LDA"] = 00;
operations["LDCH"] = 50;
operations["LDL"] = 55;
operations["LDX"] = 04;
operations["MUL"] = 20;
operations["OR"] = 44;
operations["RD"] = 0xd8;
operations["RSUB"] = 0x4c;
operations["STA"] = 0x0c;
operations["STCH"] = 54;
operations["STL"] = 14;
operations["STSW"] = 0xe8;
operations["STX"] = 10;
operations["SUB"] = 0x1c;
operations["TD"] = 0xe0;
operations["TIX"] = 0x2c;
operations["WD"] = 0xdc;
int get_objectcode(const std::string& key)
{
std::map<std::string, int>::iterator it = operations.find(key);
if (it == operations.end())
return -1;
else
return it->second;
}
std::map操作;
操作[“添加”]=18;
操作[“和”]=40;
操作[“COMP”]=28;
操作[“DI