使用vector建立结构体变量的数组,动态进行增加删除和显示

H


#ifndef __learn_struct_array__Employee__
#define __learn_struct_array__Employee__

#include <iostream>
#include <vector>
using namespace std; //must be for vector!  

class Employee
{
public:
    
    Employee();
    
    virtual ~Employee();
    
    typedef struct Employee_STRUCT_CHAR
    {
        char* name;
        char* tel;
        int no;
        
    } One_Employee_Char;
    
    
    typedef struct Employee_STRUCT_STRING
    {
        std::string name;
        std::string tel;
        int no;
        
    } One_Employee_String;
    
    const char* Get_All_Employee_ByChar(vector<One_Employee_Char> &vect_employee,const int start_no,const int count);
    
    std::string Get_All_Employee_ByString(vector<One_Employee_String> &vect_employee,const int start_no,const int count);
    

};





#endif /* defined(__learn_struct_array__Employee__) */


CPP

#include "Employee.h"

Employee::Employee()
{
    
}

Employee::~Employee()
{
    
}

std::string Employee::Get_All_Employee_ByString(vector<One_Employee_String> &vect_employee,const int start_no,const int count)
{
    
    int len_vect_txt_context =0;
    len_vect_txt_context = vect_employee.size();
    
    char* id_str = NULL;
    std::string id_string;
    std::string table="";
    
    char* id_grade_str = NULL;
    std::string id_grade_string;
    
    for (int i=start_no; i<start_no+count; i++)
    {
        One_Employee_String a_teacher;
        a_teacher.no = i+1;        
        
        id_str=new char[1024];
        sprintf(id_str,"Name%03d",i+1);
        a_teacher.name.assign(id_str,strlen(id_str));
        
        sprintf(id_str,"Tel%011d",i+1);
        a_teacher.tel.assign(id_str,strlen(id_str));
        
        table.append(a_teacher.name.c_str());
        table.append("\n");
        
        vect_employee.push_back(a_teacher);
    }
    return table;
}

const char* Employee::Get_All_Employee_ByChar(vector<One_Employee_Char> &vect_employee,const int start_no,const int count)
{
    int len_vect_txt_context =0;
    len_vect_txt_context = vect_employee.size();
    
    char* id_str = NULL;
    std::string id_string;
    std::string table="";
    
    char* id_grade_str = NULL;
    std::string id_grade_string;
    
    for (int i=start_no; i<start_no+count; i++)
    {
        One_Employee_Char a_employee;
        a_employee.no = i+1;
        
        id_str=new char[1024];
        sprintf(id_str,"Name%03d",i+1);
        id_string.assign(id_str,strlen(id_str));
        
        id_grade_str=new char[1024];
        sprintf(id_grade_str,"tel%03d",i+1);
        id_grade_string.assign(id_grade_str,strlen(id_grade_str));
        
        a_employee.name =  new char[strlen(id_string.c_str())+1];
        a_employee.tel =  new char[strlen(id_grade_string.c_str())+1];
        
        strcpy(a_employee.name ,id_string.c_str());
        strcpy(a_employee.tel ,id_grade_string.c_str());
        
        table.append(id_str);
        table.append("||");
        
        vect_employee.push_back(a_employee);
        delete [] id_str;        
    }
    
    static char *str = NULL;                   /* this only happens once */
    delete [] str;                            /* delete previous cached version */
    str = new char[strlen(table.c_str()) + 1]; /* allocate space for the string and it's NUL terminator */
    strcpy(str, table.c_str());
    return str;
}


M

调用

void Employee_Char()
{
    std::cout << "Employee Char!\n";
    Employee *myEmployee = new Employee();
    
    vector<Employee::One_Employee_Char> employees;
    
    std::string rs="";
    int start_index=0;
    rs.append(myEmployee->Get_All_Employee_ByChar(employees, start_index, 3));
    
    start_index = (int)employees.size();
    rs.append(myEmployee->Get_All_Employee_ByChar(employees, start_index, 4));
    
    start_index = (int)employees.size();
    rs.append(myEmployee->Get_All_Employee_ByChar(employees, start_index, 2));
    
    std::cout<<rs.c_str()<<std::endl;
    
    for (int i=0; i<employees.size(); i++)
    {
        std::cout<<employees.at(i).no<<std::endl;
        std::cout<<employees.at(i).name<<std::endl;
        std::cout<<employees.at(i).tel<<std::endl;
        std::cout<<"-------------"<<std::endl;
        
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值