C++ 用变量替换结构成员名称
有没有办法用类似于C++ 用变量替换结构成员名称,c++,struct,C++,Struct,有没有办法用类似于schema.($variable)的东西来替换schema.liczby和schema.studenci?在这种情况下,最好避免重复的代码只对不同的结构执行相同的操作 string getFieldType(string field_name, SCHEMA schema, string table) { if (table == "liczby") { for (int i = 0; i < schema.liczby.integers
schema.($variable)
的东西来替换schema.liczby
和schema.studenci
?在这种情况下,最好避免重复的代码只对不同的结构执行相同的操作
string getFieldType(string field_name, SCHEMA schema, string table)
{
if (table == "liczby")
{
for (int i = 0; i < schema.liczby.integers.size(); i++)
{
if (schema.liczby.integers[i] == field_name)
{
return "int";
}
}
for (int i = 0; i < schema.liczby.strings.size(); i++)
{
if (schema.liczby.strings[i] == field_name)
{
return "string";
}
}
for (int i = 0; i < schema.liczby.doubles.size(); i++)
{
if (schema.liczby.doubles[i] == field_name)
{
return "double";
}
}
for (int i = 0; i < schema.liczby.booleans.size(); i++)
{
if (schema.liczby.booleans[i] == field_name)
{
return "double";
}
}
}
else if (table == "studenci")
{
for (int i = 0; i < schema.studenci.integers.size(); i++)
{
if (schema.studenci.integers[i] == field_name)
{
return "int";
}
}
for (int i = 0; i < schema.studenci.strings.size(); i++)
{
if (schema.studenci.strings[i] == field_name)
{
return "string";
}
}
for (int i = 0; i < schema.studenci.doubles.size(); i++)
{
if (schema.studenci.doubles[i] == field_name)
{
return "double";
}
}
for (int i = 0; i < schema.studenci.booleans.size(); i++)
{
if (schema.studenci.booleans[i] == field_name)
{
return "double";
}
}
}
}
string getFieldType(字符串字段\名称、架构、字符串表)
{
如果(表==“liczby”)
{
对于(int i=0;i
没问题。您的LICZBY_模式
、STUDENCI_模式
、przedmitoy_模式
和SALE_模式
都是相同的,因此我们将它们全部替换为BASIC_模式
。然后代码就变得简单多了:
struct BASIC_SCHEMA
{
vector<string> columns;
vector<string> integers;
vector<string> strings;
vector<string> booleans;
vector<string> doubles;
};
struct SCHEMA
{
BASIC_SCHEMA liczby;
BASIC_SCHEMA studenci;
BASIC_SCHEMA przedmioty;
BASIC_SCHEMA sale;
};
string getFieldType(string field_name, SCHEMA schema, string table)
{
BASIC_SCHEMA subSchema;
if (table == "liczby")
subSchema = schema.liczby;
else if (table == "studenci")
subSchema = schema.studenci;
else
return("");
for (unsigned int i = 0; i < subSchema.integers.size(); i++)
{
if (subSchema.integers[i] == field_name)
{
return "int";
}
}
...
}
struct基本模式
{
向量列;
向量整数;
向量字符串;
向量布尔;
向量加倍;
};
结构模式
{
基本图式;
基础图式研究;
基本模式管理;
基本销售模式;
};
string getFieldType(字符串字段\名称、架构、字符串表)
{
基本模式子模式;
如果(表==“liczby”)
subSchema=schema.liczby;
否则如果(表==“studenci”)
子模式=schema.studenci;
其他的
返回(“”);
for(无符号整数i=0;i
例如,我们可以使用
查找(…)
,使其更加简洁,但这是另一回事。我们可以大量重构您的代码。有很多方法可以剥这只猫的皮。我认为std::map使事情变得非常简单,但是YMMV。我认为您应该更改第二个映射的类型,这样您就可以存储类型+值(boost:variant
或多态设置可能会很好地工作。)这段代码只是一个起点
#include <string>
#include <map>
#include <vector>
typedef std::map<std::string, std::map<std::string, std::string>> SCHEMA;
int main() {
SCHEMA schema;
// Schema table
schema["liczby"]["wartosc"] = "column";
schema["liczby"]["wartość"] = "int";
schema["studenci"]["indeks"] = "column";
schema["studenci"]["imie"] = "column";
schema["studenci"]["nazwisko"] = "column";
schema["przedmioty"]["id"] = "column";
schema["przedmioty"]["nazwa"] = "column";
schema["przedmioty"]["semestr"] = "column";
schema["sale"]["nazwa"] = "column";
schema["sale"]["rozmiar"] = "column";
schema["sale"]["projektor"] = "column";
schema["sale"]["powierzchnia"] = "column";
}
std::string getFieldType(std::string field_name, SCHEMA& schema, std::string table) {
return schema[table][field_name];
}
#包括
#包括
#包括
typedef-std::映射模式;
int main(){
图式;
//模式表
模式[“liczby”][“wartosc”]=“列”;
模式[“liczby”][“wartość”]=“int”;
模式[“studenci”][“indeks”]=“column”;
模式[“studenci”][“imie”]=“column”;
模式[“研究”][“纳兹维斯科”]=“列”;
架构[“przedmoty”][“id”]=“column”;
模式[“przedmioty”][“nazwa”]=“列”;
架构[“przedmoty”][“semestr”]=“column”;
模式[“销售”][“纳兹瓦”]=“列”;
模式[“销售”][“rozmiar”]=“列”;
架构[“销售”][“项目执行器”]=“列”;
模式[“销售”][“Povierzchnia”]=“列”;
}
std::string getFieldType(std::string字段名称、模式和模式、std::string表){
返回模式[表][字段名称];
}
在不提供反射的语言(如C++)中,您无法完成所需的操作。可能有一种方法可以完成您想要的操作,但不是按照您建议的方式。你能告诉我们模式的定义吗?这闻起来有点像。C++有很多类型化的功能,但不是通过这样的反射。