我正在尝试将指针存储在数组中。
我指向类对象的指针是:
classType **ClassObject;
所以我知道我可以使用 new 运算符来分配它,如下所示:
ClassObject = new *classType[ 100 ] = {};
我正在阅读一个带有标点符号的文本文件,这是我到目前为止所得到的:
// 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 循环中将指针存储在类对象数组中?
如果您使用 C++,请使用Boost 多维数组库 http://www.boost.org/doc/libs/1_38_0/libs/multi_array/doc/user.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)