如何将对象插入集合中? [英] How are objects inserted into a set?

查看:92
本文介绍了如何将对象插入集合中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个用于std :: set的红黑树。


在C ++标准中,为
$ b指定了3种不同的插入方法$ b关联容器。但是正如我所看到的那样,插入会根据一个键将一个对象添加到集合

。但密钥来自哪里?


如果我创建自己的对象,我没有指定插入使用

来放置对象的任何唯一键。


每次插入

对象时,插入调用是否会生成一些唯一的键?

I have implemented a red-black tree which is used for std::set.

In the C++ standard 3 different insert methods are specified for the
associative container. But as i see it insert adds an object to the set
based on a key. But where does the key come from?

If I make my own object I don''t specify any unique key that insert uses
to place the object.

Does the insert call generate some unique key that it uses each time an
object is inserted?

推荐答案

desktop写道:
desktop wrote:

我实现了一个用于std :: set的红黑树。


在C ++标准中,为

关联容器指定了3种不同的插入方法。但是正如我所看到的那样,插入会根据一个键为

设置一个对象。但关键来自哪里?
I have implemented a red-black tree which is used for std::set.

In the C++ standard 3 different insert methods are specified for the
associative container. But as i see it insert adds an object to the
set based on a key. But where does the key come from?



它与价值相同。难道你不读这本书吗

''std :: set''告诉你那个?

It''s the same as the value. Doesn''t the book you''re reading about
''std::set'' tell you that?


如果我制作我自己的对象我没有指定任何插入

用于放置对象的唯一键。
If I make my own object I don''t specify any unique key that insert
uses to place the object.



你这样做。通过为对象提供值。

You do. By supplying an object with a value.


插入调用是否会生成一些它每次使用的唯一键

是否插入了一个对象?
Does the insert call generate some unique key that it uses each time
an object is inserted?



Nope。


V

-

请在通过电子邮件回复时删除资本''A'

我没有回复最热门的回复,请不要问

Nope.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Victor Bazarov写道:
Victor Bazarov wrote:

desktop写道:
desktop wrote:

>我实施了一个红色 - 用于std :: set的黑树。

在C ++标准中,为
关联容器指定了3种不同的插入方法。但是正如我所看到的那样,插入会根据一个键向
集添加一个对象。但关键来自哪里?
>I have implemented a red-black tree which is used for std::set.

In the C++ standard 3 different insert methods are specified for the
associative container. But as i see it insert adds an object to the
set based on a key. But where does the key come from?



它与价值相同。你读的这本书不是很好吗

''std :: set''告诉你那个?


It''s the same as the value. Doesn''t the book you''re reading about
''std::set'' tell you that?


>如果我创建自己的对象,我没有指定任何插入
用于放置对象的唯一键。
>If I make my own object I don''t specify any unique key that insert
uses to place the object.



你这样做。通过为对象提供值。


You do. By supplying an object with a value.


>插入调用是否会生成一些它每次插入对象时使用的唯一键?
>Does the insert call generate some unique key that it uses each time
an object is inserted?



Nope。


V


Nope.

V



似乎我不明白集合。插入向量工作正常:


类测试{

public:

int getpp(){return pp;}

void setpp(int i){pp = i;}

private:

int pp;

};

int main(){

std :: vector< testhh;

test t1;

hh.push_back (T1); //工作正常

std :: set< testmy_set;

const test& tref = t1; //见*

my_set.insert(tref); //失败并出现错误:不匹配

?运算符<? in?__ x< __y?


}

如果我的班级''测试''定义''<''和
正确的其他一些运营商?


我仍​​然没有看到插入是如何从''测试'获得密钥所以它可以把它放到/>
树中的正确位置。


*根据 http://www.cppreference.com/cppset/insert.html

It seems that I don''t understand set. Inserting into a vector works fine:

class test {
public:
int getpp(){return pp;}
void setpp(int i){pp = i;}
private:
int pp;
};
int main() {
std::vector<testhh;
test t1;
hh.push_back(t1); // Works fine
std::set<testmy_set;
const test& tref = t1; // see *
my_set.insert(tref); // fails with error: no match for
?operator<? in ?__x < __y?

}
Can I only insert into std::set if my class ''test'' define ''<'' and
properly some of the other operators?

I still don''t see how insert gets the key from ''test'' so it can put it
the right place in the tree.

* According to http://www.cppreference.com/cppset/insert.html


desktop写道:
desktop wrote:

Victor Bazarov写道:
Victor Bazarov wrote:

> desktop写道:
>desktop wrote:

>>我已经实现了一个用于std :: set的红黑树。

在C ++标准中,为<指定了3种不同的插入方法br />关联容器。但是正如我所看到的那样,插入会根据一个键向
集添加一个对象。但关键来自哪里?
>>I have implemented a red-black tree which is used for std::set.

In the C++ standard 3 different insert methods are specified for the
associative container. But as i see it insert adds an object to the
set based on a key. But where does the key come from?


它与价值相同。难道你读的这本书不是关于
''std :: set''告诉你的吗?


It''s the same as the value. Doesn''t the book you''re reading about
''std::set'' tell you that?


>>如果我制作我自己的对象我没有指定任何插入
用于放置对象的唯一键。
>>If I make my own object I don''t specify any unique key that insert
uses to place the object.


你这样做。通过为对象提供值。


You do. By supplying an object with a value.


>>插入调用是否会生成一些它每次插入对象时使用的唯一键?
>>Does the insert call generate some unique key that it uses each time
an object is inserted?


没有。

V


Nope.

V



似乎我不理解套装。


It seems that I don''t understand set.



嗯...是的。你了解std :: map吗? std :: set非常类似于

std :: map。基本上''std :: set< T>''就像''std :: map< const T,T>''。

Hmm... Yes. Do you understand std::map? std::set is very similar to
std::map. Essentially ''std::set<T>'' is just like ''std::map<const T,T>''.


插入向量作品

罚款:

班级考试{

public:

int getpp(){return pp;}

void setpp(int i){pp = i;}

private:

int pp;

};


int main(){

std :: vector< testhh;

test t1;

hh.push_back(t1); //工作正常


std :: set< testmy_set;

const test& tref = t1; //见*

my_set.insert(tref); //失败并出现错误:不匹配

?运算符<? in?__ x< __y?


}


如果我的班级''测试''定义''<'我只能插入到std :: set中'和

适当的其他一些运营商?
Inserting into a vector works
fine:
class test {
public:
int getpp(){return pp;}
void setpp(int i){pp = i;}
private:
int pp;
};
int main() {
std::vector<testhh;
test t1;
hh.push_back(t1); // Works fine
std::set<testmy_set;
const test& tref = t1; // see *
my_set.insert(tref); // fails with error: no match for
?operator<? in ?__x < __y?

}
Can I only insert into std::set if my class ''test'' define ''<'' and
properly some of the other operators?



正确?是的,要使用默认排序机制,您的类

需要有运算符<为它定义。你可以把它变成一个会员或者你可以把它变成一个独立的功能。


你不需要有运营商<定义如果你在你的集合中使用自定义排序

仿函数。

"properly"? Yes, to use the default sorting mechanism your class
needs to have operator< defined for it. You can make it a member or
you can make it a stand-alone function.

You don''t have to have operator< defined if you use custom sorting
functor in your set.


我仍​​然没有看到插入如何从''获取密钥测试''所以它可以把它

放在树上的正确位置。
I still don''t see how insert gets the key from ''test'' so it can put it
the right place in the tree.



你读的什么书并没有解释

对象的排序是如何工作的?

What book are you reading that doesn''t explain how sorting of
objects works?


*根据 http://www.cppreference。 com / cppset / insert.html



V

-

请删除大写''''' s通过电子邮件回复

我没有回复最热门的回复,请不要问

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于如何将对象插入集合中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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