多维数组(C ++) [英] Multi-Dimensional Array ( C++ )
本文介绍了多维数组(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:
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.
我想我的问题是:
- 我是否为文件中的每个单词分配了空间?
- 我如何在while/for循环内的ClassObject数组中存储指针?
推荐答案
If you are using C++ use the Boost Multidimensional Array Library
这篇关于多维数组(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文