问题描述
我正在学习cpp,而我正在进行的练习基本上就是
如下:
1)创建一个包含4个成员的结构类型(char,char,char,int)。
2)创建一个数组,例如结构的3个实例,并填充它们
包含数据。
3)来自用户的1,2,3或4个
4)如果用户选择比方说2,显示数组中每个结构的第二个数据
成员的内容。同样适用于1,3和4.
我正试图找到一种优雅的方式来实现4)。
它'使用一组ifs / for循环很容易实现功能(如果
输入= 1,那么循环通过struct数组,cout<<
stuctInstance [i] .FirstMember ....)
但这看起来非常不优雅,因为我基本上写的是相同的
for循环代码4次 - 每次输入可能性一次。我想
NO ifs,只有一个for循环,其主要陈述直观地看起来像
喜欢:
cout< < structInstance [i] .whicheverMemberTheUserSelected
我想要做什么才有意义?有没有标准/优雅的
方式实现这一目标?如果我没有意义,我会发布
" illlegant"解决方案我有...
tia!
cdj
Hi,
I''m just learning cpp, and the exercise I''m working on is basically as
follows:
1) Create a struct type with 4 members (char, char, char, int).
2) Create an array of, say 3 instances of the struct, and populate them
with data.
3) cin 1, 2, 3, or 4 from the user
4) If the user selected, say, 2, display the contents of the 2nd data
member from each of the structs in the array. Similarly for 1,3, and 4.
I''m trying to find an elegant way to achieve 4).
It''s easy to achieve the functionality with a set of ifs/for loops (if
input=1, then for loop thru the struct array, cout <<
stuctInstance[i].FirstMember ....)
But this seems grossly inelegant, as I''m essentially writing the same
for-loop code 4 times - once for each input possibility. I would like
NO ifs, and just ONE for loop, whose key statement intuitively looks
like:
cout << structInstance[i].whicheverMemberTheUserSelected
Does it make sense what I''m looking to do? Is there a standard/elegant
way to achieve this? If I''m not making sense, I''ll just post the
"inelegant" solution I have...
tia!
cdj
推荐答案
这和你想要的相似吗? br />
Pavel
#include< iostream>
#include< sstream>
using namespace std;
struct S {char a,b,c; int d; };
static S s_;
string formatChar(void * p){
char a [] =" c" ;;
* a = *(char *)p;
返回a;
}
string formatInt (void * p){
ostringstream os;
os<< *(int *)p;
返回os.str();
}
typedef string(* Formatter)( void *);
struct FormatMeta {
int fieldOff;
Formatter formatter;
};
static FormatMeta SFM [] = {
{(char *)(& s_.a) - (char *)(& s_),formatChar},
{(char *)(& s_.b) - (char *)(& s_),formatChar},
{(char *)(& s_.c) - (char *)(& s_),formatChar},
{(char *)(& s_.d) - (char *)(& s_),formatInt }
};
#define ARR_SIZE(a)(sizeof(a)/ sizeof((a)[0]))
int main(int argc,char * argv []){
cout<< 输入一个整数:=> <<刷新;
int i;
if(!(cin>> i).good()|| i< 0 || i> = ARR_SIZE( SFM)){
cout<< 抱歉\ n;
返回1;
}
S sa [] = {{''''',' 'b'',''c'',1},{''d'','e'',''f'',2}};
for(int ii = 0; ii< ARR_SIZE(sa); ++ ii){
cout<< (SFM [i] .formatter)((char *)(& sa [ii])+ SFM [i] .fieldOff)
<<结束;
}
返回0;
}
Is this similar to what you want?
Pavel
#include <iostream>
#include <sstream>
using namespace std;
struct S { char a, b, c; int d; };
static S s_;
string formatChar(void *p) {
char a[] = "c";
*a = *(char *)p;
return a;
}
string formatInt(void *p) {
ostringstream os;
os << *(int*)p;
return os.str();
}
typedef string (*Formatter)(void *);
struct FormatMeta {
int fieldOff;
Formatter formatter;
};
static FormatMeta SFM[] = {
{ (char*)(&s_.a) - (char*)(&s_), formatChar },
{ (char*)(&s_.b) - (char*)(&s_), formatChar },
{ (char*)(&s_.c) - (char*)(&s_), formatChar },
{ (char*)(&s_.d) - (char*)(&s_), formatInt }
};
#define ARR_SIZE(a) (sizeof(a)/sizeof((a)[0]))
int main(int argc, char *argv[]) {
cout << "Enter an integer: =>" << flush;
int i;
if(!(cin >> i).good() || i < 0 || i >= ARR_SIZE(SFM)) {
cout << "Sorry\n";
return 1;
}
S sa[] = { {''a'', ''b'', ''c'', 1}, {''d'', ''e'', ''f'', 2} };
for(int ii=0; ii < ARR_SIZE(sa); ++ii) {
cout << (SFM[i].formatter)((char*)(&sa[ii]) + SFM[i].fieldOff)
<< endl;
}
return 0;
}
这与你想要的相似吗?
Pavel
#include< iostream>
#include< sstream>
使用命名空间std;
struct S {char a,b,c; int d; };
静态S s_;
字符串formatChar(void * p){
char a [] =" c";
* a = *(char *)p;
返回一个;
}
字符串formatInt(void * p){
ostringstream os;
os<< *(int *)p;
返回os.str();
}
typedef string(* Formatter)(void *);
struct FormatMeta {
int fieldOff;
Formatter formatter;
};
静态FormatMeta SFM [] = {
{(char *)(& s_.a) - (char *)(& s_),formatChar},
{(char *)(& s_.b) - (char *)(& s_),formatChar},
{( char *)(& s_.c) - (char *)(& s_),formatChar},
{(char *)(& s_.d) - (char *)(& s_) ,formatInt}
};
#define ARR_SIZE(a)(sizeof(a)/ sizeof((a)[0]))
cout<< 输入一个整数:=> << flush;
int i;
if(!(cin>> i).good()|| i< 0 || i> = ARR_SIZE(SFM)){
cout<< 抱歉\ n;
返回1;
}
S sa [] = {{'''',''b'',''c'',1 },{''d'',''e'',''f'',2}};
for(int ii = 0; ii< ARR_SIZE(sa); ++ ii){
cout<< (SFM [i] .formatter)((char *)(& sa [ii])+ SFM [i] .fieldOff)
<<返回0;
}
Is this similar to what you want?
Pavel
#include <iostream>
#include <sstream>
using namespace std;
struct S { char a, b, c; int d; };
static S s_;
string formatChar(void *p) {
char a[] = "c";
*a = *(char *)p;
return a;
}
string formatInt(void *p) {
ostringstream os;
os << *(int*)p;
return os.str();
}
typedef string (*Formatter)(void *);
struct FormatMeta {
int fieldOff;
Formatter formatter;
};
static FormatMeta SFM[] = {
{ (char*)(&s_.a) - (char*)(&s_), formatChar },
{ (char*)(&s_.b) - (char*)(&s_), formatChar },
{ (char*)(&s_.c) - (char*)(&s_), formatChar },
{ (char*)(&s_.d) - (char*)(&s_), formatInt }
};
#define ARR_SIZE(a) (sizeof(a)/sizeof((a)[0]))
int main(int argc, char *argv[]) {
cout << "Enter an integer: =>" << flush;
int i;
if(!(cin >> i).good() || i < 0 || i >= ARR_SIZE(SFM)) {
cout << "Sorry\n";
return 1;
}
S sa[] = { {''a'', ''b'', ''c'', 1}, {''d'', ''e'', ''f'', 2} };
for(int ii=0; ii < ARR_SIZE(sa); ++ii) {
cout << (SFM[i].formatter)((char*)(&sa[ii]) + SFM[i].fieldOff)
<< endl;
}
return 0;
}
这些偏移是未定义的行为或接近。如果您使用
offsetof()宏,则会定义其行为。
-
Phlip
< - 不是博客!!!
Those offsets are either undefined behavior or darn close. If you use the
offsetof() macro, then their behavior is defined.
--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
这篇关于访问结构数组的变量成员的最佳/最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!