唯一对象和stl容器 [英] unique objects and stl containers

查看:72
本文介绍了唯一对象和stl容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在STL容器中存储唯一对象?


假设C类对象是唯一的:


class C

{

public:

C(){}

~C(){}

private:

C(const C&);

operator =(const C&);

};


另一个班级管理和操作这些物品的集合:


class用户

{

public:

void addC(const C& c)

{

m_lstCs.push_back(c); //错误!

}

受保护:

std :: list< C> m_lstCs;

};


这导致错误,因为std :: list< C> :: push_back()调用C'的

复制构造函数。是一个指针列表唯一的解决方案,还是有更好的方式

a?


-

Claudio Jolowicz

计算机系

180 Queen's Gate

南肯辛顿校区

帝国理工学院>
LONDON SW7 2AZ
http ://www.doc.ic.ac.uk/~cj603

Is it possible to store unique objects in an STL container?

Suppose an object of class C is unique:

class C
{
public:
C() {}
~C() {}
private:
C(const C&);
operator=(const C&);
};

Another class manages and operates on collections of these objects:

class User
{
public:
void addC(const C& c)
{
m_lstCs.push_back(c);//error!
}
protected:
std::list<C> m_lstCs;
};

This leads to an error, because std::list<C>::push_back() calls C''s
copy constructor. Is a list of pointers the only solution, or is there
a better way?

--
Claudio Jolowicz

Department of Computing
180 Queen''s Gate
South Kensington campus
Imperial College
LONDON SW7 2AZ

http://www.doc.ic.ac.uk/~cj603

推荐答案



Claudio Jolowicz < CJ *** @ doc.ic.ac.uk>在消息中写道

新闻:Pi ******************************* @ kiwi.doc。 ic .ac.uk ...

"Claudio Jolowicz" <cj***@doc.ic.ac.uk> wrote in message
news:Pi*******************************@kiwi.doc.ic .ac.uk...
是否可以在STL容器中存储唯一对象?

假设C类对象是唯一的:
< C级
{
公开:
C(){}
~C(){}
私人:
C(const C& );
operator =(const C&);
};

另一个类管理和操作这些对象的集合:

类User
{
公众:
void addC(const C& c)
{
m_lstCs.push_back(c); //错误!
}
protected:
std :: list< C> m_lstCs;
};

这会导致错误,因为std :: list< C> :: push_back()调用C'的复制构造函数。指针列表是唯一的解决方案,还是有更好的方法?
Is it possible to store unique objects in an STL container?

Suppose an object of class C is unique:

class C
{
public:
C() {}
~C() {}
private:
C(const C&);
operator=(const C&);
};

Another class manages and operates on collections of these objects:

class User
{
public:
void addC(const C& c)
{
m_lstCs.push_back(c);//error!
}
protected:
std::list<C> m_lstCs;
};

This leads to an error, because std::list<C>::push_back() calls C''s
copy constructor. Is a list of pointers the only solution, or is there
a better way?




容器的要求之一就是对象

存储在它们中必须是''可复制''和''可分配''。

所以是的,指针是唯一的方法。


-Mike



One of the requirements of containers is that objects
stored in them must be both ''copyable'' and ''assignable''.
So yes, a pointer would be the only way.

-Mike




" Claudio Jolowicz" < CJ *** @ doc.ic.ac.uk>在消息中写道

新闻:Pi ******************************* @ kiwi.doc。 ic .ac.uk ...

"Claudio Jolowicz" <cj***@doc.ic.ac.uk> wrote in message
news:Pi*******************************@kiwi.doc.ic .ac.uk...
是否可以在STL容器中存储唯一对象?

假设C类对象是唯一的:
< C级
{
公开:
C(){}
~C(){}
私人:
C(const C& );
operator =(const C&);
};

另一个类管理和操作这些对象的集合:

类User
{
公众:
void addC(const C& c)
{
m_lstCs.push_back(c); //错误!
}
protected:
std :: list< C> m_lstCs;
};

这会导致错误,因为std :: list< C> :: push_back()调用C'的复制构造函数。指针列表是唯一的解决方案,还是有更好的方法?
Is it possible to store unique objects in an STL container?

Suppose an object of class C is unique:

class C
{
public:
C() {}
~C() {}
private:
C(const C&);
operator=(const C&);
};

Another class manages and operates on collections of these objects:

class User
{
public:
void addC(const C& c)
{
m_lstCs.push_back(c);//error!
}
protected:
std::list<C> m_lstCs;
};

This leads to an error, because std::list<C>::push_back() calls C''s
copy constructor. Is a list of pointers the only solution, or is there
a better way?




容器的要求之一就是对象

存储在它们中必须是''可复制''和''可分配''。

所以是的,指针是唯一的方法。


-Mike



One of the requirements of containers is that objects
stored in them must be both ''copyable'' and ''assignable''.
So yes, a pointer would be the only way.

-Mike


2004年4月9日星期五03:34:09 +0100,Claudio Jolowicz< cj *** @ doc.ic.ac.uk>

写道:
On Fri, 9 Apr 2004 03:34:09 +0100, Claudio Jolowicz <cj***@doc.ic.ac.uk>
wrote:
是否可以在STL容器中存储唯一对象?


是独特的我还没有遇到一个技术术语?有

" Singletons",但根据定义你不会有多个,所以我不能为你的生命
想象为什么你'/ /希望/能够将它们存储在

容器中。


唯一的另一个可能含义将在是否可以使用所有对象具有唯一值的STL容器中。当然,那是什么std :: set是一个具有独特价值的对象的集合。

假设C类的对象是唯一的:

C班
{
公开:
C(){}
~C(){}
私人:
C(const C& ;);
operator =(const C&);
};


Cs是什么使它们独一无二?它们不能复制构造

或分配,但这并不能阻止你拥有它们......

它们是否具有相同的值或没有。
另一个班级管理和操作这些对象的集合:

类用户
公共:
void addC(const C& c)
{
m_lstCs.push_back(c); //错误!
}
受保护:
std :: list< C> m_lstCs;
};

这会导致错误,因为std :: list< C> :: push_back()调用C'的复制构造函数。


这是将任何对象存储在STL容器中的基本要求:

它可以被复制。

是一个指针列表唯一的解决方案,还是有更好的方法?
Is it possible to store unique objects in an STL container?
Is "unique" a technical term I haven''t encountered yet? There are
"Singletons", but by definition you wouldn''t have more than one, so I can''t
for the life of me imagine why you''d /want/ to be able to store them in a
container.

Another possible meaning of "unique" would be as in "Is it possible to have
an STL container in which all objects have unique values". Sure, that''s
what std::set is, a collection of uniquely-valued objects.

Suppose an object of class C is unique:

class C
{
public:
C() {}
~C() {}
private:
C(const C&);
operator=(const C&);
};
What is it about Cs that make them "unique"? They can''t be copy constructed
or assigned, but that wouldn''t stop you from having a bunch of them...
whether they have equivalent values or not.
Another class manages and operates on collections of these objects:

class User
{
public:
void addC(const C& c)
{
m_lstCs.push_back(c);//error!
}
protected:
std::list<C> m_lstCs;
};

This leads to an error, because std::list<C>::push_back() calls C''s
copy constructor.
That''s a basic requirement for storing any object in an STL container: that
it can be copied.
Is a list of pointers the only solution, or is there
a better way?




无论你想做什么,一系列指针(智能或

否则)将用于收集

STL容器中的不可复制对象。我不能告诉你是否有更好的方式,因为我不能理解你想要做什么。

-leor

-

Leor Zolman --- BD软件--- www.bdsoft.com

C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD软件的免费STL错误消息解密器位于:
www.bdsoft .com / tools / stlfilt.html


这篇关于唯一对象和stl容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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