如何将字符串数组复制到结构中
How to copy an array of strings to a 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;
}
必须初始化data->namesRec = new string[size];
,因为它是指针!!
不要使用原始指针来表示数组。
不要试图自己管理new
和delete
,正如您的代码所证明的那样,这很容易出错。
请使用适当的容器类,如std::vector
,或者使用c++标准类库提供的智能指针。
如果你的教授或助教否认这样做,那就离开课程,他们不是在教c++。
试试这个代码:
#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);
}
只是一个提示:
#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];
}
相关文章:
- C++将结构复制到另一个程序的资源
- 使用 aligned_malloc 和 memcpy 多次将结构复制到指针
- 多线程和共享资源:使用C++定期将数据从缓冲区(数据结构)复制到文件
- 将带有字符数组的结构复制到字符数组
- 错误将结构复制到同一类型的另一种
- 将包含位字段和动态数据的结构复制到 Char 数组缓冲区中
- 将通用值从结构复制到可变C
- C++:我想将数据从结构复制/存储到单独的数组中
- 如何使用OpenCV将向量向量的结构复制到另一个向量C++
- 使用VS2008编译的C/C++中存在结构复制问题
- 从一个结构复制到另一个不同类型的结构
- 将庞大的数组结构复制到 GPU
- 将带有函数指针数组的结构复制到设备
- 是结构复制标准
- 将所有成员从一个结构复制到另一个结构
- 复制(非浅层或深层)用于C/C++/Java等同物中的结构.复制到所有成员都指向完全相同内存的变量
- 在C++中将结构复制到数组中
- 将结构复制到char[]缓冲区
- 将一个结构复制到另一个结构以及改变一个结构对另一个结构的影响
- 尝试将结构复制到设备内存时 CUDA 参数无效 (cudaMemcpy)