数组类型的数据结构在C++里面定义和使用

CLASS CPP

//
//  date_oper.cpp
//  test_string_array
//
//  Created by  on 7/23/14.
//  Copyright (c) 2014 edu. All rights reserved.
//

#include "date_oper.h"

CDate_Oper::CDate_Oper()
{
}


CDate_Oper::~CDate_Oper()
{
    
}
std::string CDate_Oper::School_All_Teacher(vector<One_Teacher> &vect_class_teachers,const int start_no,const int count)
{
    
    int len_vect_txt_context =0;
	len_vect_txt_context = vect_class_teachers.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_Teacher a_teacher;
        a_teacher.no = i+1;
        
        
        id_str=new char[1024];
        sprintf(id_str,"Teacher Name%03d",i+1);
        a_teacher.name.assign(id_str,strlen(id_str));
      
        table.append(id_str);
        table.append("||");
        
        vect_class_teachers.push_back(a_teacher);
      }
    return table;
    
  

    
}

const char* CDate_Oper::Class_All_Student(vector<One_Student> &vect_class_students,const int start_no,const int count)
{
    int len_vect_txt_context =0;
	len_vect_txt_context = vect_class_students.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_Student a_student;
        a_student.no = i+1;
        
        
        id_str=new char[1024];
        sprintf(id_str,"Student Name%03d",i+1);
        id_string.assign(id_str,strlen(id_str));
        
        id_grade_str=new char[1024];
        sprintf(id_grade_str,"Student Grade%03d",i+1);
        id_grade_string.assign(id_grade_str,strlen(id_grade_str));
        
         a_student.name =  new char[strlen(id_string.c_str())+1];
         a_student.grade =  new char[strlen(id_grade_string.c_str())+1];
        
        strcpy(a_student.name ,id_string.c_str());
        strcpy(a_student.grade ,id_grade_string.c_str());
        
        table.append(id_str);
        table.append("||");
        
        vect_class_students.push_back(a_student);
        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;
}


CLASS H


//
//  date_oper.h
//  test_string_array
//
//  Created by  on 7/23/14.
//  Copyright (c) 2014 edu. All rights reserved.
//

#ifndef __test_string_array__date_oper__
#define __test_string_array__date_oper__

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

class CDate_Oper
{
public:
    typedef struct STUDENT
	{
		char* name;
		char* grade;
		int no; 
        
	} One_Student;
    
    
    typedef struct TEACHER
	{
        std::string name;
		int no;
        
	} One_Teacher;
    
    CDate_Oper();
    
  
    virtual ~CDate_Oper();

    const char* Class_All_Student(vector<One_Student> &vect_class_students,const int start_no,const int count);
    
    std::string School_All_Teacher(vector<One_Teacher> &vect_class_teachers,const int start_no,const int count);
    
};
#endif /* defined(__test_string_array__date_oper__) */


MAIN.CPP


//
//  main.cpp
//  test_string_array
//
//  Created by  on 7/22/14.
//  Copyright (c) 2014 edu. All rights reserved.
//

#include <iostream>

#include "date_oper.h"


void student()
{
    std::cout<<"---------Student String---------------"<<endl;
    CDate_Oper *my_date_oper = new CDate_Oper();
    
    vector<CDate_Oper::One_Student> a_class_students;
    
    int iCount=0;
    
    std::string my_students;
    my_students.append( my_date_oper->Class_All_Student(a_class_students,0,2));
    
    iCount = a_class_students.size();
    my_students.append( my_date_oper->Class_All_Student(a_class_students,iCount,3));
    
    
    iCount = a_class_students.size();
    my_students.append( my_date_oper->Class_All_Student(a_class_students,iCount,2));
    
    std::cout<<my_students.c_str()<<endl;
    
    for (int i=0; i<a_class_students.size(); i++)
    {
        std::cout<<a_class_students.at(i).no<<endl;
        std::cout<<a_class_students.at(i).name<<endl;
        std::cout<<a_class_students.at(i).grade<<endl;
        std::cout<<"------------------------"<<endl;
    }
    
    // CDate_Oper *my_date_oper = new CDate_Oper();
}

void teacher()
{
    CDate_Oper *my_date_oper = new CDate_Oper();
    
    vector<CDate_Oper::One_Student> a_class_students;
    
    int iCount=0;
    
    std::string my_students;
    
    std::cout<<"---------TEACHER String---------------"<<endl;
    vector<CDate_Oper::One_Teacher> a_school_teachers;
    
    iCount=0;
    
    std::string my_teachers;
    my_teachers.append( my_date_oper->School_All_Teacher(a_school_teachers,0,2));
    
    iCount = a_school_teachers.size();
    my_teachers.append( my_date_oper->School_All_Teacher(a_school_teachers,iCount,3));
    
    std::cout<<my_teachers.c_str()<<endl;
    
    for (int i=0; i<a_school_teachers.size(); i++)
    {
        std::cout<<a_school_teachers.at(i).no<<endl;
        std::cout<<a_school_teachers.at(i).name<<endl;
        std::cout<<"------------------------"<<endl;
    }
    
  //  std::cin.get();
}

int main(int argc, const char * argv[])
{
    // insert code here...
   
    student();
    
    //std::cin.get();
    
    teacher();
    
    std::cin.get();
    
    return 0;
}



the end!

test OK!

test result:

---------Student String---------------

Student Name001||Student Name002||Student Name003||Student Name004||Student Name005||Student Name006||Student Name007||

1

Student Name001

Student Grade001

------------------------

2

Student Name002

Student Grade002

------------------------

3

Student Name003

Student Grade003

------------------------

4

Student Name004

Student Grade004

------------------------

5

Student Name005

Student Grade005

------------------------

6

Student Name006

Student Grade006

------------------------

7

Student Name007

Student Grade007

------------------------

---------TEACHER String---------------

Teacher Name001||Teacher Name002||Teacher Name003||Teacher Name004||Teacher Name005||

1

Teacher Name001

------------------------

2

Teacher Name002

------------------------

3

Teacher Name003

------------------------

4

Teacher Name004

------------------------

5

Teacher Name005

------------------------


在XCODE里面使用

需要把m修改为mm,后缀名。

然后:

//
//  eduDocument.m
//  test_vector_struct
//
//  Created by  on 7/23/14.
//  Copyright (c) 2014 edu. All rights reserved.
//

#import "eduDocument.h"
#include "date_oper.h"
@implementation eduDocument
@synthesize m_LB1;

- (id)init
{
    self = [super init];
    if (self) {
        // Add your subclass-specific initialization here.
    }
    return self;
}

- (NSString *)windowNibName
{
    // Override returning the nib file name of the document
    // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
    return @"eduDocument";
}

- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
    [super windowControllerDidLoadNib:aController];
    // Add any code here that needs to be executed once the windowController has loaded the document's window.
}

+ (BOOL)autosavesInPlace
{
    return YES;
}
- (IBAction)OnBT_test1:(id)sender {
    
    std::cout<<"---------Student String---------------"<<endl;
    CDate_Oper *my_date_oper = new CDate_Oper();
    
    vector<CDate_Oper::One_Student> a_class_students;
    
    int iCount=0;
    
    std::string my_students;
    my_students.append( my_date_oper->Class_All_Student(a_class_students,0,2));
    
    iCount = a_class_students.size();
    my_students.append( my_date_oper->Class_All_Student(a_class_students,iCount,3));
    
    
    iCount = a_class_students.size();
    my_students.append( my_date_oper->Class_All_Student(a_class_students,iCount,2));
    
    std::cout<<my_students.c_str()<<endl;
    
    for (int i=0; i<a_class_students.size(); i++)
    {
        std::cout<<a_class_students.at(i).no<<endl;
        std::cout<<a_class_students.at(i).name<<endl;
        std::cout<<a_class_students.at(i).grade<<endl;
        std::cout<<"------------------------"<<endl;
    }
    NSString* str;
    str = [NSString stringWithUTF8String:my_students.c_str()];
    m_LB1.stringValue = str;
    
}

@end


测试OK!


如图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值