繁体   English   中英

如何在 C++ 的 main 中使用结构的数组数据成员?

[英]how to use array data member of struct in main in C++?

如果我有一个结构 Emp 有 2 个数据成员 no_of_emp[5]; 和部门名称我如何在 main 中使用 no emp 数组我有点困惑请帮助我。 可能是我的问题不好。

struct emp
{
  int no_of_emp[5];
  string department_name;
 
}

您可以在 main 中使用大括号初始化器:

std::vector<emp> employers =
{
    {
        {0, 1, 2, 3, 4},
        "department1"
    },
    {
        {5, 6, 7, 8, 9},
        "department2"
    }
};

那么您可以使用您的雇主列表,例如:

for (const auto& x: employers)
{
    for (const auto num: x.no_of_emp)
        cout << num << " ";

    cout << x.department_name << endl;
}

这里我们只是使用了一点向量的概念:

#include <iostream>
#include <vector>

struct emp {
    int no_of_emp[5]; // it'll store 5 elements in each struct array
    std::string department_name;
};

int main(void) {
    std::vector<emp> e;
    emp temp; // to accept values first and then add them later together
    const int max = 40; // set the maximum employee to iterate till

    // getting the values
    for (int i = 0; i < max; i++) {
        std::cout << "=== Employee " << i + 1 << std::endl;

        for (int i = 0; i < 5; i++) { // accept 5 numbers for each
            std::cout << "No. of employees: ";
            std::cin >> temp.no_of_emp[i];
        }

        std::cout << "Department name: ";
        fseek(stdin, 0, SEEK_END); // to avoid skipping line

        std::getline(std::cin, temp.department_name);

        e.push_back(temp); // pushing the values containing in the initialized struct
    }

    std::cout << std::endl;

    for (int i = 0; i < max; i++) {
        std::cout << "--- Employee " << i + 1 << std::endl;

        for (const auto i : e[i].no_of_emp) // getting those 5 elements of each array
            std::cout << i << ' ';
        
        std::cout << e[i].department_name << std::endl;
    }

    return 0;
}

我们使用了一个临时的 struct temp从用户那里检索所有需要的值,然后将它们一起推回向量中,直到max 之后,我们只需打印每个结构。

然后你应该得到类似的 output:

$ g++ -o main main.cpp && ./main

=== Employee 1              // getting values
No. of employees: 3 2 3 4 3
Department name: Something
=== Employee 2
No. of employees: 1 2 4 3 3
Department name: Else

--- Employee 1             // displaying values
3 2 3 4 3 Something        // by retrieving from arrays of struct
--- Employee 2             // using vectors (format: x x x x x department)
1 2 4 3 3 Else             // till 40 times (as per of the requirement)

像这样的东西:

struct emp e; //declare a variable of type struct emp

e.no_of_emp[0] = 25; //set the value of the first element of no_of_emp array

for (int i = 0; i < 5; i++){ //or set all the elements in no_of_emp array
    e.no_of_emp[i] = i + 1; //prints 1
}

printf("%d ", e.no_of_emp[0]); // print the first element no_of_emp array

for (int i = 0; i < 5; i++){ //or print all the elements no_of_emp array
    printf("%d ", e.no_of_emp[i]); //prints 1 2 3 4 5
}

for (auto& i : e.no_of_emp){ //or with a range based loop
    printf("%d", i);
}

C++ 提供了一个固定大小的数组容器,您可以使用std::array代替 C 样式数组:

struct emp
{
  std::array<int, 5> no_of_emp;
  string department_name;
 
};

访问和设置元素的代码可以相同。


或者,如果您需要一个可变长度数组,您可以使用std::vector

这里元素的设置有点不同:

struct emp
{
  std::vector<int> no_of_emp;
  string department_name;
 
};
e.no_of_emp.push_back(25); //set the value of no_of_emp of the first element of the array

for (int i = 0; i < 5; i++){ //or set all the no_of_emp of the structs in the array
    e.no_of_emp.push_back(i + 1);
}

访问可以与第一个示例代码中所示的 C 样式数组相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM