可变大小的数组 [英] Arrays of Variable Sizes
问题描述
对于学校作业,我需要编写一个班级来使用
代码。 IntArray b(-3,6)基本上意味着我需要
产生一个整数值数组,其索引从-3到
6.我完全失去了我应该如何创建那个数组。任何推动
正确的方向将是赞赏。
void test2()
{
系统(cls);
cout<< >> 2。用两个整数声明的数组:IntArray b(-3,6); <<
endl<< endl;
csis<< >> 2。用两个整数声明的数组:IntArray b(-3,6); <<
endl<< endl;
IntArray b(-3,6);
for(int i = b.low(); i< = b.high(); i ++)
b [i] = i * 10;
b.setName(''b'');
cout<< b<< endl;
csis<< b<< endl;
wait();
}
- 史蒂夫 - 写道:< blockquote class =post_quotes>对于学校作业,我需要编写一个类来使用
以下代码。 IntArray b(-3,6)基本上意味着我需要生成一个整数值数组,其索引从-3到
6.我完全迷失了我的本意创建该数组。任何在正确方向上的推动都会受到赞赏。
void test2()
{
系统(cls);
cout< < >> 2。用两个整数声明的数组:IntArray b(-3,6); <<
endl<< endl;
csis<< >> 2。用两个整数声明的数组:IntArray b(-3,6); <<
endl<< endl;
IntArray b(-3,6);
for(int i = b.low(); i< = b.high(); i ++)
b [i] = i * 10;
b.setName(''b'');
cout<< b<< endl;
csis<< b<< endl;
wait();
}
您的IntArray类需要重载operator [] ...?
尝试创建一个类并发布结果....
2003年7月25日19:56:06 -0700,< a href =mailto:se **** @ foundation.sdsu.edu> se **** @ foundation.sdsu.edu ( - 史蒂夫 - )写道:
对于学校作业,我需要编写一个类来使用
以下代码。 IntArray b(-3,6)基本上意味着我需要生成一个整数值数组,其索引从-3到
6。我完全迷失了我应该如何创建该数组。任何推动正确的方向将是赞赏。
(0)
std :: vector是代表存储的好主意。
在课堂上包裹它。
void test2()
{
system(" cls");
(1)
不是个好主意。仅适用于具有cls命令的系统。
无法重定向到文件。
cout<< >> 2。用两个整数声明的数组:IntArray b(-3,6); <<
endl<< endl;
csis<< >> 2。用两个整数声明的数组:IntArray b(-3,6); <<
(2)
什么是'csis"?
大概是一些日志文件。
为什么不直接重定向标准输出?
(3)
重复的文字文本,使用命名常量。
endl<< endl;
IntArray b(-3,6);
(4)
这告诉你需要一个带有两个参数的构造函数。
for(int i = b.low(); i< = b.high(); i ++)
(5)
这告诉你需要访问者称为低的方法并且
高。
b [i] = i * 10;
(6)
这告诉你需要一个运算符[]。请参阅TCPPPL以获取
示例。例如。
b.setName(''b'');
(7)
这告诉您需要一个名为setName的修饰符方法。
cout<< b<< ENDL;
(8)
这告诉你需要一些东西使得它成为可能
使用IntArray作为参数输出流。它
可以是一个自定义运算符<<。它可以是一个操作符const char *。
csis<< b<< endl;
wait();
(9)
假设等待等待用户输入绝对不是一个好的
想法,因为这意味着你无法自动化测试。
}
> std :: vector是表示存储的好主意。在类中包装它。
这不是有点矫枉过正吗?根据我的理解,创建的数组
不必自己变量。只是创建它们的索引。
你能创建一个类,它有一个int start,并且int end,一个int
a [end-开始]然后是一个函数,getInt(arrayIndex),它可能在开始和结束之间都是
(我确定你也可以对它进行检查)。
>
所以你会去
MyClass m = myClass(-3,6);
m.getInt(-1)
" Alf P. Steinbach" <人*** @ start.no>在消息中写道
新闻:3f **************** @ News.CIS.DFN.DE ... 2003年7月25日19:56:06 -0700, se****@foundation.sdsu.edu ( - 史蒂夫 - )
写道:< blockquote class =post_quotes>对于学校作业,我需要编写一个类来使用
以下代码。 IntArray b(-3,6)基本上意味着我需要生成一个整数值数组,其索引从-3到
6。我完全迷失了我应该如何创建该数组。任何推动
正确的方向将是赞赏。
(0)
void test2()
{
系统(cls);
(1)
不是个好主意。仅适用于具有cls命令的系统。
无法重定向到文件。
cout<< >> 2。用两个整数声明的数组:IntArray b(-3,6); <<
endl<< endl;
csis<< >> 2。用两个整数声明的数组:IntArray b(-3,6); <
(2)
什么是csis?
大概是一些日志文件。
为什么不重定向标准输出?
(3)
重复的文字文本,使用命名常量。
endl<< endl;
IntArray b(-3,6);
(4)
这告诉你需要一个带有两个参数的构造函数。
for(int i = b.low(); i< = b.high(); i ++)
(5)
这告诉您需要名为低的访问器方法。和
高。
b [i] = i * 10;
(6)
这告诉你需要一个操作符[]。有关
示例,请参阅TCPPPL。例如。
b.setName(''b'');
(7)
这告诉您需要一个名为setName的修饰符方法。
cout<< b<< endl;
(8)
这告诉您需要一些能够使用IntArray作为输出流参数的东西。它可以是一个自定义运算符<<它可以是一个运算符const char *。
csis<< b<< endl;
wait();
(9)
假设等待等待用户输入绝对不是一个好主意,因为这意味着你无法自动化测试。
}
For a school assignment I need to write a class to work with the
following code. The IntArray b(-3, 6) basically means that I need to
produce an array of integer values that has an index going from -3 to
6. I''m completely lost on how I should create that array. Any shoves
in the right direction would be appreciated.
void test2()
{
system("cls");
cout << "2. Array declared with two integers: IntArray b(-3, 6);" <<
endl << endl;
csis << "2. Array declared with two integers: IntArray b(-3, 6);" <<
endl << endl;
IntArray b(-3, 6);
for(int i = b.low(); i <= b.high(); i++)
b[i] = i * 10;
b.setName(''b'');
cout << b << endl;
csis << b << endl;
wait();
}
- Steve - wrote:For a school assignment I need to write a class to work with the
following code. The IntArray b(-3, 6) basically means that I need to
produce an array of integer values that has an index going from -3 to
6. I''m completely lost on how I should create that array. Any shoves
in the right direction would be appreciated.
void test2()
{
system("cls");
cout << "2. Array declared with two integers: IntArray b(-3, 6);" <<
endl << endl;
csis << "2. Array declared with two integers: IntArray b(-3, 6);" <<
endl << endl;
IntArray b(-3, 6);
for(int i = b.low(); i <= b.high(); i++)
b[i] = i * 10;
b.setName(''b'');
cout << b << endl;
csis << b << endl;
wait();
}
your IntArray class needs to overload operator [] ... ?
Try creating a class and post the results ....
On 25 Jul 2003 19:56:06 -0700, se****@foundation.sdsu.edu (- Steve -) wrote:
For a school assignment I need to write a class to work with the
following code. The IntArray b(-3, 6) basically means that I need to
produce an array of integer values that has an index going from -3 to
6. I''m completely lost on how I should create that array. Any shoves
in the right direction would be appreciated.
(0)
A std::vector be a good idea for representing the storage.
Wrap that in a class.
void test2()
{
system("cls");
(1)
Not a good idea. Only works on a system with cls command.
Cannot be redirected to a file.
cout << "2. Array declared with two integers: IntArray b(-3, 6);" <<
endl << endl;
csis << "2. Array declared with two integers: IntArray b(-3, 6);" <<
(2)
What''s "csis"?
Presumably some log file.
Why not just redirect standard output?
(3)
Duplicated literal text, use a named constant.
endl << endl;
IntArray b(-3, 6);
(4)
This tells you that you need a constructor with two arguments.
for(int i = b.low(); i <= b.high(); i++)
(5)
This tells you that you need accessor methods called "low" and
"high".
b[i] = i * 10;
(6)
This tells you that you need an operator[]. See TCPPPL for an
example. For example.
b.setName(''b'');
(7)
This tells you that you need a modifier method called "setName".
cout << b << endl;
(8)
This tells you that you need something that makes it possible
to use an IntArray as an argument to an output stream. It
could be a custom operator<<. It could be an operator const char*.
csis << b << endl;
wait();
(9)
Assuming "wait" waits for user input that is ABSOLUTELY NOT a good
idea because it means you cannot automate the testing.
}
> A std::vector be a good idea for representing the storage.Wrap that in a class.
Isn''t that a bit of overkill? From what i understand, the arrays created
don''t have to be variable themselves. Just the index which created them.
Could you just create a class, that has a int start, and and int end, an int
a[end-start] and then a function, getInt(arrayIndex) which would presumably
be between the start and end (i''m sure you could also do checks for this).
so then you would go
MyClass m = myClass(-3, 6);
m.getInt(-1)
"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE... On 25 Jul 2003 19:56:06 -0700, se****@foundation.sdsu.edu (- Steve -) wrote:For a school assignment I need to write a class to work with the
following code. The IntArray b(-3, 6) basically means that I need to
produce an array of integer values that has an index going from -3 to
6. I''m completely lost on how I should create that array. Any shoves
in the right direction would be appreciated.
(0)
void test2()
{
system("cls");
(1)
Not a good idea. Only works on a system with cls command.
Cannot be redirected to a file.cout << "2. Array declared with two integers: IntArray b(-3, 6);" <<
endl << endl;
csis << "2. Array declared with two integers: IntArray b(-3, 6);" <<
(2)
What''s "csis"?
Presumably some log file.
Why not just redirect standard output?
(3)
Duplicated literal text, use a named constant.endl << endl;
IntArray b(-3, 6);
(4)
This tells you that you need a constructor with two arguments.for(int i = b.low(); i <= b.high(); i++)
(5)
This tells you that you need accessor methods called "low" and
"high".b[i] = i * 10;
(6)
This tells you that you need an operator[]. See TCPPPL for an
example. For example.b.setName(''b'');
(7)
This tells you that you need a modifier method called "setName".cout << b << endl;
(8)
This tells you that you need something that makes it possible
to use an IntArray as an argument to an output stream. It
could be a custom operator<<. It could be an operator const char*.csis << b << endl;
wait();
(9)
Assuming "wait" waits for user input that is ABSOLUTELY NOT a good
idea because it means you cannot automate the testing.}
这篇关于可变大小的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!