【问题标题】:How can I change struct name to an integer?如何将结构名称更改为整数?
【发布时间】:2018-06-03 10:48:44
【问题描述】:

我一直在使用 struct 一段时间,但我总是有问题。请参阅此示例:- `

#include <iostream>
struct Employee
{
    short id;
    int age;
    double wage;
};

void printInformation(Employee employee)
{
    std::cout << "ID:   " << employee.id << "\n";
    std::cout << "Age:  " << employee.age << "\n";
    std::cout << "Wage: " << employee.wage << "\n";
}

int main()
{
    Employee joe = { 14, 32, 24.15 };
    Employee frank = { 15, 28, 18.27 };
    // Print Joe's information
    printInformation(joe);

    std::cout << "\n";

    // Print Frank's information
    printInformation(frank);

    return 0;
}`

这段代码工作得很好,但是什么时候我将使用字符串而不是“Joe”和“Frank”。我试过但失败了。 这是我正在处理的代码。

 '#include <bits/stdc++.h>
 using namespace std;
 struct People{
   string name;
   int id;
   int age;
   int wage;
   };                
int main(){

string iname;
int iid = 0;
int iage; 
int iwage;     
while(1){
 iid++;
 cout << "ID No." << iid << endl <<"Enter Name";
 std::getline(std::cin,iname);
 cout << "Enter your Age:-";
 cin >> iage;       
  cout << "Enter your Wage :-";
  cin >> iwage; 
  cout << "See your details."<<endl <<"Name"<<iname<< endl<< "ID."<< iid << 
   endl << "Age" << iage << endl<< "Wage" << iwage << endl;
  People a = static_cast<People>(iid);
    People a={iname,iid,iage,iwage};  
    std::cout << "Name:" << a.name << "\n";
 std::cout << "ID:   " << a.id << "\n";
 std::cout << "Age:  " << a.age << "\n";
std::cout << "Wage: " <<  a.wage << "\n";
    }
    return 0;
} 

用户在此处输入其数据。我想以 struct 的形式保存太多数据,所以我使用了“a”。据我说,它必须计算 1.age=(bla..),3.age=(bla..) 请帮帮我。

【问题讨论】:

  • 你说的是变量名吗?发布一个您想要工作但没有工作的代码示例。

标签: c++ string struct


【解决方案1】:

我认为您正在寻找类似 std::map 的东西

http://en.cppreference.com/w/cpp/container/map

示例:

std::map<std::string, Employee> employees;
Employee joe = { 14, 32, 24.15 };
employees["joe"] = joe;

printInformation(employees["joe"]);

【讨论】:

    【解决方案2】:

    您还可以为每个员工提供另一个变量string name;,然后将员工存储在一个向量中,然后在函数中循环遍历员工,直到找到具有该名称的员工:

    #include <iostream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    struct Employee{
        string name;
        short id;
        int age;
        double wage;
    };
    
    vector<Employee> Employees;
    void printInformation(string employee){
        for(int i = 0; i < Employees.size(); i++){
            if(Employees[i].name == employee){
                cout << "Name: " << Employees[i].name << "\n";
                cout << "ID:   " << Employees[i].id << "\n";
                cout << "Age:  " << Employees[i].age << "\n";
                cout << "Wage: " << Employees[i].wage << "\n";
            }
        }
     }
    
    int main(){
        Employee joe = {"joe", 14, 32, 24.15 };
        Employee frank = {"frank", 15, 28, 18.27 };
        Employees.push_back(joe);
        Employees.push_back(frank);
        printInformation("joe");
        printInformation("frank");
    
        return 0;
    }
    

    我认为它比使用地图更好,因为你只写printInformation("joe"),当使用地图时你写printInformation(employees["joe"])。由你决定。

    【讨论】:

    • 我的问题不是这个我的问题是,我怎样才能让乔和弗兰克成为一个整数。这里只能使用两次,但这里我使用的是一个大约 16 名员工的程序,他们的数据是要输入的。
    猜你喜欢
    • 2018-11-14
    • 2023-01-27
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 1970-01-01
    • 2020-04-10
    相关资源
    最近更新 更多