如何将字符串数组复制到结构中

时间:2015-11-29 21:54:03

标签: c++ arrays string struct

如何将字符串数组的内容复制到结构中?获取错误,它无法将类型字符串转换为类型字符串。最后一个循环是我遇到麻烦的地方。我是否还需要在堆上为字符串数组分配空间?我为分数分配了它。我认为字符串实际上是一个字符数组,所以我很困惑如何使用指针来引用和传输它们。

    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;

    struct StudentRecords
    {
     string* namesRec;
     int** examsptr;
     };


    void main()
    {


const int NG = 4;

string names[] = { "Amy Adams", "Bob Barr", "Carla Carr",
                     "Dan Dobbs", "Elena Evans" };

int exams[][NG] = 
{ 
    { 98,87,93,88 },
    { 78,86,82,91 },
    { 66,71,85,94 },
    { 72,63,77,69 },
    { 91,83,76,60 }
};

string *nameHolder = nullptr;


StudentRecords *data = new StudentRecords();
data->examsptr = new int*[NG];

for (int i = 0; i < NG; ++i) 
{
    data->examsptr[i] = new int[NG];
}

for (int count = 0; count < NG; count++)
{
    for (int count2 = 0; count2 < NG; count2++)
        {
         (*data).examsptr[count][count2] = exams[count][count2];
         cout << (*data).examsptr[count][count2] << "         " << exams[count][count2] << endl;
        }
     }

    for (int count3 = 0; count3 < 5; count3++)
    {
   *nameHolder = names[count3];
   (*data).namesRec[count3] = *nameHolder;
    cout << (*data).namesRec[count3] << endl;
     }

4 个答案:

答案 0 :(得分:1)

你必须初始化data->namesRec = new string[size];因为是指针!!

答案 1 :(得分:0)

不要使用原始指针来表示数组。

请勿尝试自行管理newdelete,只是从您的相关代码中证明了这一点。

使用c ++标准类库提供的相应容器类,如std::vectorsmart pointers

如果你的教授或TA否认这样做,只要离开课程,他们就不会教c ++。

答案 2 :(得分:0)

试试这段代码:

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

struct StudentRecords
{
    char* namesRec;
    int** examsptr;
};


void main(void)
{
    const int NG = 4;

    char *names[] = { "Amy Adams", "Bob Barr", "Carla Carr",
                        "Dan Dobbs", "Elena Evans" };

    int exams[][NG] = 
    { 
        { 98,87,93,88 },
        { 78,86,82,91 },
        { 66,71,85,94 },
        { 72,63,77,69 },
        { 91,83,76,60 },
    };

    char *nameHolder = nullptr;
    nameHolder = (char*)malloc(50*sizeof(char));

    struct StudentRecords *data = new StudentRecords();
    data->examsptr = new int*[NG];

    for (int i = 0; i < NG; i++) 
    {
        data->examsptr[i] = new int[NG];
    }

    for (int count = 0; count < NG; count++)
    {
        for (int count2 = 0; count2 < NG; count2++)
        {
            data->examsptr[count][count2] = exams[count][count2];
            printf("%d ",data->examsptr[count][count2]);
        }
        printf("\n");
    }
    printf("\n");
    for (int count3 = 0; count3 < 5; count3++)
    {
        strcpy(nameHolder,names[count3]);
        data->namesRec = (char*)malloc(30*sizeof(char));
        strcpy(data->namesRec,names[count3]);
        printf("%s ",data->namesRec);
        free(data->namesRec);
    }

    for (int i = 0; i < NG;i++) 
    {
        delete data->examsptr[i];
    }
    delete data;
    free(nameHolder);
}

答案 3 :(得分:0)

只是一个提示:

#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

struct Student {
    std::string name;
    std::vector<int> exams;
};


int main() {
    std::vector<Student> sList{ {"Amy Adams",   { 98,87,93,88 }},
                                {"Bob Barr",    { 78,86,82,91 }},
                                {"Carla Carr",  { 66,71,85,94 }},
                                {"Dan Dobbs",   { 72,63,77,69 }}};

    std::string sname = "Elena Evans";    // Just arrived...
    int exms[] = { 91,83,76,60 };         // if you need to use an array
    sList.push_back(Student{sname, {exms,exms+sizeof(exms)/sizeof(int)}}); 

    // show students
    for ( auto & s : sList ) {       // for every element of the vector
        std::cout << s.name << ": \t";
        for ( auto & e : s.exams ) std::cout << e << "  ";
        std::cout << std::endl;
    }

    return 0;
}

如果由于某种原因不允许您使用标准库容器和设施,您至少可以尝试模仿其功能并编写一些函数来管理副本,分配和解除分配:

std::string * _AllocNames( size_t n ) {
    std::string * new_ptr;
    try {
        new_ptr = new std::string[n];
        return new_ptr;
    }
    catch (std::bad_alloc &ba) {       // Something bad happened
        std::cerr << "Error, exception caught: " << ba.what() << '\n';
        exit(1);
    }
}

void _DeAllocNames( std::string * names ) {
    if ( names ) {
        for (size_t i = 0; i < nStd; i++) names[i].clear();
        delete[] names;
    }
}

int ** _AllocExams( size_t n, size_t m ) {
    int ** new_ptr;
    try {
         new_ptr = new int*[n];
         for ( size_t i = 0; i < n; i++ ) new_ptr[i] = new int[m];
         return new_ptr;
    }
    catch (std::bad_alloc &ba) {       // Something bad happened
         std::cerr << "Error, exception caught: " << ba.what() << '\n';
         exit(1);
    }
}


void _DeAllocExams( int ** exams ) {
     if ( exams ) {
         for (size_t i = 0; i < nStd; i++) delete[] exams[i];
         delete[] exams;
     }
}

对于副本,您需要记住int **与int [] [4]不同。

void _Copy( const std::string * source, size_t n, std::string * dest) {
    for ( size_t i = 0; i < n ; i++ ) dest[i] = source[i];  
}

template < size_t K >
    void _Copy( const int source[][K], size_t n, int ** dest) {
        for ( size_t i = 0; i < n ; i++ )
            for ( size_t j = 0; j < K; j++ )
                dest[i][j] = source[i][j];
}

void _Copy( int ** source, size_t n, size_t m, int ** dest) {
        for ( size_t i = 0; i < n ; i++ )
            for ( size_t j = 0; j < m; j++ )
                dest[i][j] = source[i][j];
}