简体   繁体   English

多维数组(C ++)

[英]Multi-Dimensional Array ( C++ )

I'm trying to store a pointer in an array. 我正在尝试将指针存储在数组中。

My pointer to a pointer is class object is: 我的指针是类对象:

classType **ClassObject;

So i know i can allocate it by using the new operator like this: 所以我知道我可以通过使用new运算符来分配它:

ClassObject = new *classType[ 100 ] = {};

I'm reading a text file, with punctuation and here is what i have so far: 我正在阅读带有标点符号的文本文件,这是我到目前为止的内容:

// included libraries
// main function
// defined varaibles

classType **ClassObject; // global object
const int NELEMENTS = 100; // global index


wrdCount = 1;  // start this at 1 for the 1st word
while ( !inFile.eof() )  
{
   getline( inFile, str, '\n' );  // read all data into a string varaible
   str = removePunct(str);  // User Defined Function to remove all punctuation.
   for ( unsigned x = 0; x < str.length(); x++ )
   {
       if ( str[x] == ' ' ) 
       {
          wrdCount++;  // Incrementing at each space
          ClassObject[x] = new *classType[x];
       // What i want to do here is allocate space for each word read from the file.

       }
   }
}
// this function just replaces all punctionation with a space
string removePunct(string &str) 
{ 
    for ( unsigned x = 0; x < str.length(); x++ )
        if ( ispunct( str[x] ) )
            str[x] = ' ';
  return str;
}

// Thats about it.

I guess my questions are: 我想我的问题是:

  • Have I allocated space for each word in the file? 我是否为文件中的每个单词分配了空间?
  • How would i store a pointer in the ClassObject array within my while/for loop? 我如何在while / for循环内的ClassObject数组中存储指针?

如果您使用的是C ++,请使用Boost多维数组库

Hmm, I'm not sure what you want to do (especially new *classType[x] -- does this even compile?) 嗯,我不确定您要做什么(尤其是新的* classType [x]甚至可以编译吗?)

If you want a new classType for every word, then you can just go 如果您希望每个单词都使用一个新的classType,那么您可以

ClassObject[x] = new classType; //create a _single_ classType
ClassObject[x]->doSomething();

provided that ClassObject is initialized (as you said). 只要ClassObject已初始化(如您所说)。

You say you want a 2D array - if you want to do that, then the syntax is: 您说您想要一个2D数组-如果您想要这样做,则语法为:

ClassObject[x] = new classType[y]; //create an array of classType of size y
ClassObject[0][0].doSomething(); //note that [] dereferences automatically

However, I'm also not sure of what you mean by new *classType[ 100 ] = {}; 但是,我也不确定new * classType [100] = {};的含义。 - what are the curly braces doing there? -花括号在那里做什么? It seems like it should be 似乎应该

classType** classObject = new classType*[100];

I highly suggest you use something else, though, as this is really nasty (and you have to take care of deletion... ugh) 我强烈建议您使用其他方法,因为这确实很讨厌(而且您必须小心删除... ugh)

Use vector<>s or as the above poster suggested, the boost libraries. 使用vector <> s或如上述建议那样使用boost库。

Your code is perfectly fine excepting one line: ClassObject[x] = new *classType[x]; 您的代码非常好,只需要一行: ClassObject[x] = new *classType[x]; The star * needs to go away, and what you're probably trying to say is that you want ClassObject to be indexed to word count rather than x. 星号*需要消失,您可能想说的是您希望ClassObject被索引为字数而不是x。

Replace that line with: ClassObject[wrdCount] = new classType[x]; 将该行替换为: ClassObject[wrdCount] = new classType[x];

Hope that helps, Billy3 希望有帮助,比利3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM