【问题标题】:Access to the struct elements. Is it possible to access like a vector?访问结构元素。是否可以像矢量一样访问?
【发布时间】:2014-07-16 22:26:20
【问题描述】:

我有以下使用结构的示例(简化):

#include <iostream>
#include <algorithm>
#include <time.h>
using namespace std;

struct s_str
{
    int a=1,b=2,c=3;
};

int main(void)
{
    s_str str;
    int sel;    
    srand(time(NULL));                 //initialize random seed
    sel = rand() % (3);                //generate a random number between 0 and 2

    cout << "sel: " << sel << endl;     
    cout << "str: " << str.??? << endl;//I was wondering to output a, b or c 
    return 0;                          //depending whether sel=0,1,2respectively.           
 }

当结构体“str”被定义后,我们可以使用操作符“.”来访问每个元素。后跟元素的名称。例如“str.c”会给我们数字 3。

但是在这个例子中,我们不知道编程时要输出的“str”元素,因为它是由 sel 随机选择的。

我不知道如何输出“str.???”从 sel 编号,即如果 sel=0,则 str.a,如果 sel=1,则 str.b,如果 sel=3,则 str.c。

我尝试了类似“str.[sel]”的方法,但没有成功。你能帮助我吗?

PD:我不想太麻烦,但是如何解决同样的问题,但现在假设 a、b 和 c 具有不同的变量类型。例如:

int a=1,b=2;
string c="hola";  

我尝试用两个运算符来做,但由于它们被重载,它没有编译。

【问题讨论】:

  • 如果没有将索引映射到成员,则不可移植。
  • 谢谢大家的回答!

标签: c++ vector struct


【解决方案1】:

如前所述,如果不提供特定的映射和索引运算符,您将无法做到这一点。以下should work well

struct s_str
{
    int a=1,b=2,c=3;
    int& operator[](int index) {
        switch(index) {
            case 0:
                return a;
            case 1:
                return b;
            case 2:
                return c;
            default:
                throw std::out_of_range("s_str: Index out of range.");
            break;
        }   
    }
};

int main() {
    s_str s;
    cout << s[0] << ", " << s[1] << ", " << s[2] << endl;
    // cout << s[42] << endl; // Uncomment to see it fail.
    return 0;
}

【讨论】:

  • 我的 C++ 生锈了,但我相信如果你定义 operator [] 以返回 int&amp; 那么你也可以在赋值的左侧使用它。
  • 相当不错!我同意这是最通用和最强大的解决方案。衷心感谢=)
  • @ilovemistaking De Nada! ;)
【解决方案2】:

一般来说,不会。

如果结构元素的唯一区别特征是它们的索引,请在结构中定义一个向量或数组。

如果您有时想按名称有时按位置引用元素,请为结构定义 operator []( int )

【讨论】:

    【解决方案3】:

    如果你的结构中只有几个整数,最简单的方法是:

    struct s_str
    {
        int a = 1, b = 2, c = 3;
        int& operator[] (size_t t) {
            assert(t<3); // assumption for the following to return a meaningful value
            return (t == 0 ? a : (t == 1 ? b : c));
        }
    };
    

    您可以使用

       cout << "str: " << str[sel] << endl;
    

    你甚至可以使用 int 来分配,因为它是通过引用:

    str[sel] = 9; 
    cout << "a,b,c=" << str.a << "," << str.b << "," << str.c << endl;
    

    【讨论】:

    • 只是为了把所有东西都放在一条线上。当然,if 链或 switch 也可以是替代方案,尤其是当您有更多变量时。
    • 条件运算符:“如果第二个和第三个操作数是相同值类别的glvalues并且具有相同的类型,则结果是该类型和值类别”(Std 5.16 pt.4)跨度>
    • @Christophe 请注意,对于out_of_range 情况,您的解决方案总是返回c
    • @πάνταῥεῖ 是的!我的信息不是绝对使用条件运算符,而是使用返回引用的 operator[]。正如我在上面的评论中所说,对于更复杂的要求,如果链和开关可能更合适。
    • :-) 没有对错之分。要求是:“sel编号,即sel=0时str.a,sel=1时str.b,sel=3时str.c。” .但是我可以提出一个断言来明确假设。
    猜你喜欢
    • 1970-01-01
    • 2011-11-13
    • 2021-01-26
    • 1970-01-01
    • 2019-08-10
    • 2017-04-23
    • 2013-06-26
    • 1970-01-01
    • 2016-10-16
    相关资源
    最近更新 更多