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;
}
}