【问题标题】:Making an array of pointers to structs or objects in C++在 C++ 中创建指向结构或对象的指针数组
【发布时间】:2011-04-16 02:44:29
【问题描述】:

所以我基本上只是尝试接收一些文件输入,然后将这些数据放入几个结构中。我遇到的唯一问题是指向结构的指针的命名。结构本身应该代表学生,我想将每个指针设置为他们的名字之一,而不是任意变量。我试图以一种我认为语法错误的方式来做到这一点,因为它不起作用。在下面的代码中,我使用 temp 数组递增 for 循环,因为每个第 4 个位置都是一个新学生。关于如何解决这个问题的任何想法?

#include<iostream>
#include<iomanip>
#include"student.h"
#include"creditcard.h"
#include<fstream>
using namespace std;

int main ()
{
    string creditcards[20];
    int i;
    int x;
    int amount;
    string temp[20];
    ifstream infile;
    string filename;
    int count;
    int numstudents;
    string newstring="";
    string pointers[20];

    cout<<"enter the file name of which you've stored your"<<endl
        <<"credit card infomation"<<endl;

    getline(cin,filename,'\n');
    infile.open(filename.c_str());

    count=0;
    getline(infile,temp[count],'\n');
    while(! infile.eof())
    {
        count++;
        getline(infile,temp[count],'\n');          

        numstudents= (count/4);
        if(numstudents < 1 || count%4 != 0)
        {
            cout<<"incorrect data file"<<endl;
        }
    }

    cout<<numstudents<<endl;

    for(i=0,x=0; i<numstudents;i++,x+4)
    {
        student *temp[x];
        temp[x] = new student;
        pointers[i] = temp[x];
    }

    for(i=0;i<numstudents;i+4)
    {
        cout<<temp[i]<<endl;
    }

    return 0;
}

【问题讨论】:

  • 亲爱的耶稣,请正确格式化您的代码(我会修复它,但在未来,缩进四个空格以使这对每个人都不完全可怕!谢谢)。 daringfireball.net/projects/markdown/syntax
  • 看起来@peachykeen 打败了我! :) 干杯
  • 目前尚不清楚您要在这里实现什么。你是什​​么意思“获取数据并将其放入多个结构中”?什么结构?输入在文件中的样子如何?

标签: c++ pointers struct


【解决方案1】:

好的,让我们从顶部开始。

您的代码(在我重新格式化之前)是一团糟。凌乱的代码更难阅读,更容易出现错误。

您有 3 个数组,每个数组包含 20 个字符串。为什么需要这么多?

其中一个被命名为temp;必须将其用作变量名是一个很好的指标,表明您在某处错误处理数据。

您相对较早地声明了int count,然后将其初始化为0。虽然不一定是坏事,但这不是最好的方法(在需要时同时进行)。

你可以在一行中声明多个局部变量,但你不需要在函数的顶部声明它们。这在 C++ 中不是必需的。

int main ()
{
    string creditcards[20];
    int i = 0, x = 0, amount = 0;

(合法,但可能不需要)

通常最好在需要变量之前同时声明和初始化变量:

int count = 0;

getline(infile, temp[count], '\n');

我记得看到不建议在您点击 eof 之前阅读,尽管我对此并不完全确定。你可能想改变这个:

while ( !infile.eof() )
{

现在,我在这里看到的第一个实际错误是你读了一行,增加了count,然后在行动之前读了另一行。这是故意的,如果是,为什么有必要?执行getline 并在循环内递增将更具可读性并且可能更可靠。

    count++;
    getline(infile, temp[count], '\n');          

我认为这一行是一个错误:

 for(i=0,x=0; i<numstudents;i++,x+4)

最后一段是i++, x+4。它不会改变x

之后的下一个循环处理 i 的方式与此循环使用 x 的方式相同,因此您可以将这两者结合起来。

现在,最重要的是,大量临时数组不是解决这个问题(或任何其他我能想到的)的方法。

要存储此类数据,您需要查看std::map&lt;std::string, student*&gt;std::vector&lt;student*&gt;。向量将允许您在必要时将新的学生结构推到后面,映射将允许您根据名称键入它们并稍后检索,如下所示:

typdef map<string, student*> studentmap;
studentmap students;

studentmap::iterator iter = students.find("Bob");
if ( iter != students.end() )
{
    student * bob = iter->second;
    // Work with data
}

这是一种更好的处理方式,并且可以消除您现在所做的大量猜测工作。

【讨论】:

    【解决方案2】:

    如果您希望能够按姓名引用学生,请考虑使用map&lt;string, student&gt;map&lt;string, student*&gt;

    这将允许您通过students["Jack"]students["Jill"] 推荐个别学生。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多