数组列表? [英] Array List?

查看:60
本文介绍了数组列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自C和Java的* nix我用CList和MSVC ++搞砸了我的元素,但我认为我的问题主要是语法上的。

我有一个我称之为''varray'的ADT,可以在给定索引的数组中返回指向

arbirary大小元素的指针,它将分配

内存在必要时支持它:

struct varray * tests = varray_new(sizeof(struct test));

struct test * t =(struct test *)varray_get(tests,50));


重点是我不必为* t分配内存。支持数组中的每个元素都是sizeof(struct test)。


我应该使用C ++中的等效ADT吗?


谢谢,

迈克


ref: http://www.ioplex.com/~miallen/libmba/dl/src/varray.c

解决方案

" Alf P. Steinbach" <人*** @ start.no>在消息中写道

news:3f **************** @ News.CIS.DFN.DE ...

< blockquote class =post_quotes>我有一个我称之为''varray'的ADT,可以在给定索引的数组中返回指向
anarbirary大小元素的指针,它将分配
thememory to如有必要,请回复:

struct varray * tests = varray_new(sizeof(struct test));
struct test * t =(struct test *)varray_get(tests,50));

关键是我没有为* t分配内存。支持数组中的每个元素
都是sizeof(struct test)。

我应该使用C ++中的等效ADT吗?



签出标准容器,std :: vector,std :: list等。




你的意思是这样的?这是合法的吗?


#include< iostream>

#include< list>

using namespace std;


struct foo {

int i;

short s;

};


main()

{

list< struct foo> L;

struct foo f;


fi = 10;

fs = 5;

L.push_back(f);

fi = 30;

fs = 15;

L.push_front(f);

fi = 100;

fs = 50;

L.insert(++ L.begin(),f);

fi = 101;

fs = 51;

L.push_back(f);

fi = 102;

fs = 52;

L.push_back(f);


list< struct foo> :: iterator i;


for(i = L.begin(); i!= L.end(); ++ i)cout<< " I =" << i-> i<< ",S =" << i-> s

<< " " ;;

cout<<结束;

返回0;

}


c:\ miallen \p> list

i = 30,s = 15 i = 100,s = 50 i = 10,s = 5 i = 101,s = 51 i = 102,s = 52


在2003年9月8日星期一14:10:16 -0700,Michael B. Allen < MB ***** @ ioplex.com>写道:

" Alf P. Steinbach" <人*** @ start.no>在消息中写道
新闻:3f **************** @ News.CIS.DFN.DE ...

>我有一个我称之为''varray'的ADT,可以在给定索引的数组中返回一个指向> arbirary大小元素的指针,如果需要,它将分配>内存来支持它:
>
> struct varray * tests = varray_new(sizeof(struct test));
> struct test * t =(struct test *)varray_get(tests,50));
>
>重点是我没有为* t分配内存。每个元素>支持数组是sizeof(结构测试)。
>
>我应该使用C ++中的等效ADT吗?
检查标准容器,std: :vector,std :: list等。



你的意思是这样的吗?




好​​。我只会评论改进的机会。


这是合法的吗?


是的。

#include< iostream>
#include< list>
使用namespace std;


防止你出于自己的目的使用''list'这样的名字。

struct foo {
int i;
短片;
};


构造函数可以大大减轻事情,避免例如假人

变量如下面的''f'',以及作业。


main()


''main''必须有返回类型''int''。


见[ http://www.research.att.com/~bs/bs_faq2.html#void-main]

{
list< struct foo>升;


无需重复单词''struct''(与C相反,在C ++中a />
''struct''类似于任何类型其他)。


避免使用所有大写名称可以帮助避免与

宏发生名称冲突。


struct foo f ;

fi = 10;
fs = 5;
L.push_back(f);
fi = 30;
fs = 15;
L.push_front(f);
fi = 100;
fs = 50;
L.insert(++ L.begin(),f);
fi = 101;
fs = 51;
L.push_back(f);
fi = 102;
fs = 52;
L.push_back(f);

list< struct foo> :: iterator i;

for(i = L.begin(); i!= L.end(); ++ i)cout< < " I =" << i-> i<< ",S =" << i-> s
<< " " ;;
cout<<结束;
返回0;
}



考虑

#include< iostream>

#include< list>


struct Foo

{

int i;

短片;


Foo(int iValue,短sValue):i(iValue),s(sValue){}

};


int main()

{

std :: list< Foo>列表;


list.push_back(Foo(10,5));

list.push_front(Foo(30,15));

list.insert(++ list.begin(),Foo(100,50));

list.push_back(Foo(101,51));

list.push_back(Foo(102,52));


std :: list< Foo> :: iterator i;


for(i = list.begin(); i!= list.end(); ++ i)

{

std :: cout<< " I =" << i-> i<< ",S =" << i-> s<< " " << std :: endl;

}

}


c:\ miallen \p> list
i = 30,s = 15 i = 100,s = 50 i = 10,s = 5 i = 101,s = 51 i = 102,s = 52



< br>



" Alf P. Steinbach" <人*** @ start.no>在留言中写道

news:3f **************** @ News.CIS.DFN.DE ...

#包括< iostream>
#include< list>

struct Foo
{
int i;
短片;

Foo(int iValue,short sValue):i(iValue),s(sValue){}
};

int main()
{
std ::列表<富> list;

list.push_back(Foo(10,5));



"

有趣。那是构造函数吗?这个和

之间的关系是一个类的构造函数吗?这会退化成一个类还是这个

只是一些特定于结构的额外符号?


在实践中,人们最终是否完全使用类结构?或者是

使用像这样的结构有什么优势?

让我对C ++感到困扰的一件事就是你不能确定内存中的内容

(例如你不能序列化,检查或部分复制占用的内存<由于害怕破坏它而被一个班级
)。明确的记忆操作是什么

使C很棒(是的,也很危险)。


谢谢,

Mike


Coming from C and Java on *nix I''m a little out of my element messing around
with CList and MSVC++ but I think my issues are largely syntactic.

I have an ADT that I use called a ''varray'' that can return a pointer to an
arbirary sized element in an array given an index and it will allocate the
memory to back it if necessary:

struct varray *tests = varray_new(sizeof(struct test));
struct test *t = (struct test *)varray_get(tests, 50));

The point being I don''t have to allocate the memory for *t. Each element in
the backing array is sizeof(struct test).

Is there an equivalent ADT in C++ that I should use?

Thanks,
Mike

ref: http://www.ioplex.com/~miallen/libmba/dl/src/varray.c

解决方案

"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE...

I have an ADT that I use called a ''varray'' that can return a pointer to anarbirary sized element in an array given an index and it will allocate thememory to back it if necessary:

struct varray *tests = varray_new(sizeof(struct test));
struct test *t = (struct test *)varray_get(tests, 50));

The point being I don''t have to allocate the memory for *t. Each element inthe backing array is sizeof(struct test).

Is there an equivalent ADT in C++ that I should use?



Check out the standard containers, std::vector, std::list and so on.



You mean like this? Is this legit?

#include <iostream>
#include <list>
using namespace std;

struct foo {
int i;
short s;
};

main()
{
list<struct foo> L;
struct foo f;

f.i = 10;
f.s = 5;
L.push_back(f);
f.i = 30;
f.s = 15;
L.push_front(f);
f.i = 100;
f.s = 50;
L.insert(++L.begin(),f);
f.i = 101;
f.s = 51;
L.push_back(f);
f.i = 102;
f.s = 52;
L.push_back(f);

list<struct foo>::iterator i;

for(i=L.begin(); i != L.end(); ++i) cout << "i=" << i->i << ",s=" << i->s
<< " ";
cout << endl;
return 0;
}

c:\miallen\p>list
i=30,s=15 i=100,s=50 i=10,s=5 i=101,s=51 i=102,s=52


On Mon, 8 Sep 2003 14:10:16 -0700, "Michael B. Allen" <mb*****@ioplex.com> wrote:

"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE...

>I have an ADT that I use called a ''varray'' that can return a pointer toan >arbirary sized element in an array given an index and it will allocatethe >memory to back it if necessary:
>
> struct varray *tests = varray_new(sizeof(struct test));
> struct test *t = (struct test *)varray_get(tests, 50));
>
>The point being I don''t have to allocate the memory for *t. Each elementin >the backing array is sizeof(struct test).
>
>Is there an equivalent ADT in C++ that I should use?
Check out the standard containers, std::vector, std::list and so on.



You mean like this?



Good. I''ll comment only on opportunities for improvement.

Is this legit?
Yep.
#include <iostream>
#include <list>
using namespace std;
Prevents you from using names like ''list'' for your own purposes.
struct foo {
int i;
short s;
};
A constructor can ease things considerably, avoiding e.g. dummy
variables like ''f'' below, as well as the assignments.

main()
''main'' must have return type ''int''.

See [http://www.research.att.com/~bs/bs_faq2.html#void-main].
{
list<struct foo> L;
No need to repeat the word ''struct'' (in contrast to C, in C++ a
''struct'' is a type like any others).

Avoiding all uppercase names can help avoid name conflicts with
macros.

struct foo f;

f.i = 10;
f.s = 5;
L.push_back(f);
f.i = 30;
f.s = 15;
L.push_front(f);
f.i = 100;
f.s = 50;
L.insert(++L.begin(),f);
f.i = 101;
f.s = 51;
L.push_back(f);
f.i = 102;
f.s = 52;
L.push_back(f);

list<struct foo>::iterator i;

for(i=L.begin(); i != L.end(); ++i) cout << "i=" << i->i << ",s=" << i->s
<< " ";
cout << endl;
return 0;
}

Consider
#include <iostream>
#include <list>

struct Foo
{
int i;
short s;

Foo( int iValue, short sValue ): i( iValue ), s( sValue ) {}
};

int main()
{
std::list<Foo> list;

list.push_back( Foo( 10, 5 ) );
list.push_front( Foo( 30, 15 ) );
list.insert( ++list.begin(), Foo( 100, 50 ) );
list.push_back( Foo( 101, 51 ) );
list.push_back( Foo( 102, 52 ) );

std::list<Foo>::iterator i;

for( i = list.begin(); i != list.end(); ++i )
{
std::cout << "i=" << i->i << ",s=" << i->s << " " << std::endl;
}
}

c:\miallen\p>list
i=30,s=15 i=100,s=50 i=10,s=5 i=101,s=51 i=102,s=52





"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE...

#include <iostream>
#include <list>

struct Foo
{
int i;
short s;

Foo( int iValue, short sValue ): i( iValue ), s( sValue ) {}
};

int main()
{
std::list<Foo> list;

list.push_back( Foo( 10, 5 ) );


"
Interesting. Is that a constructor? Is there a relationship between this and
the constructor of a class? Does this degenerate into a class or is this
just some extra notation specific to structures?

In practice do folks just end up using classes over structs entirely? Or is
there an advantage to using a struct like this? One of the things that
bothered me about C++ that you cannot not be certain what was in memory
(e.g. you cannot serialize, inspect, or partially copy the memory occupied
by a class for fear of breaking it). Explicit memory manipluation is what
makes C great (yes, and dangerous).

Thanks,
Mike


这篇关于数组列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆