在const方法中使用map,list等 [英] using map, list etc. in const methods

查看:82
本文介绍了在const方法中使用map,list等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我对C ++很陌生,所以也许我想念的东西。

我写了一个简单的棋盘游戏。它有一个董事会类。这个类有一个

方法,它返回玩家在棋盘上的棋子数。由于

这个函数不会改变类中的任何内容,我将其声明为

const。为了计算给定颜色的所有部分,函数通过地图迭代

。 CNode指针。 " CNode"另一个类是与这个问题无关的



这里有一个函数:( EColor是一个颜色的枚举,这里不重要)


int

CBoard :: countPieces(EColor color)const

{

//" nodes"是一个私人类成员

//类型为map< string,CNode *>

int count = 0;

for(map< string,CNode *> :: iterator i = nodes.begin();

i!= nodes.end();

i ++)

{

//做某事(* i)

// ...

}

返回计算;

}


编译器拒绝编译代码,因为我违反了行中函数的

常量我在哪里声明迭代器。

是否有正确的方法(或解决方法)让函数const?


另一点:我听说过,由于

所有权问题(?),将地址,列表,矢量等stl容器中的指针存储到
对象是不好的。你知道一个关于

这个主题的好网页或教程吗?我想学习如何以正确的方式去做。

STL中有很多课程,我想知道如何使用他们的全部力量。目前我正在使用列表,向量和

地图缺乏一本好书或在线教程详细讨论STL。


谢谢提前预订!


-

问候,

Christof Krueger


删除" donotspam"如果你想给我发电子邮件。

Hello,

I''m quite new to C++ so maybe there''s something I miss.
I write a simple board game. It has a board class. This class has a
method that returns the count of pieces a player has on the board. Since
this function does not change anything in the class I declared it as
const. To count all pieces of a given color the functions iterates
through a "map" of CNode-pointers. "CNode" is another class that is
irrelevant to the problem.

Here comes the function: (EColor is an enum of colors, not important here)

int
CBoard::countPieces(EColor color) const
{
// "nodes" is a private class member
// with type map<string,CNode*>
int count = 0;
for (map<string,CNode*>::iterator i=nodes.begin();
i != nodes.end();
i++)
{
// do something with (*i)
// ...
}
return count;
}

The compiler refuses to compile the code, because I violate the
constness of the function in the line where I declare the iterator.
Is there a proper way (or workaround) to leave the function const?

Another point: I''ve heard, that it is not good to store pointers to
objects in stl-containers like map, list, vector etc. because of
ownership problems (?). Do you know a good webpage or tutorial about
this topic? I would like to learn how to do it the right way.
There are a lot of classes in the STL and I would like to know how to
use their whole power. At the moment I''m just using lists, vectors and
maps in lack of a good book or online tutorial discussing the STL in detail.

Thanks in advance!

--
Regards,
Christof Krueger

Remove "donotspam" if you want to email me.

推荐答案

在文章< bu ************* @ news.t -online.com>,Christof Krueger写道:
In article <bu*************@news.t-online.com>, Christof Krueger wrote:
你好,

我对C ++很陌生,所以也许我想念的东西。
我写了一个简单的棋盘游戏。它有一个董事会类。这个类有一个
方法,可以返回玩家在棋盘上的棋子数。由于这个函数不会改变类中的任何内容,我将其声明为
const。为了计算给定颜色的所有部分,函数通过地图迭代。 CNode指针。 " CNode"是另一个与问题无关的课程。

这里有一个功能:( EColor是一个颜色的枚举,这里不重要)

int
CBoard :: countPieces(EColor color)const
{
//节点是一个私有类成员
//类型为map< string,CNode *>
int count = 0;
for(map< string,CNode *> :: iterator i = nodes .begin();
我!= nodes.end();
我++)
//
//用(* i)
//做什么。
}
返回计数;


编译器拒绝编译代码,因为我违反了函数行的
constness我声明了迭代器。
有没有正确的方法(或解决方法)让函数const?


map< string,CNode *> :: const_iterator


顺便说一句,你可以用


count_if(themap.begin(),themap.end(),isWhite); //如果作品为白色,则isWhite返回true

另一点:我听说,在地图,列表等stl容器中存储指向
对象的指针并不好因为
所有权问题(?)。


这不一定是坏事。但你需要确保你理解拥有的人是谁?b $ b一个给定的指针,你需要确保地图不会对死亡指针持有




这里是'我的问题:你的指针是如何分配和删除的?谁拥有它们?

你知道一个关于这个话题的好网页或教程吗?我想学习如何以正确的方式做到这一点。


这个主题太复杂了,无法在网页中详尽讨论。你需要阅读一些处理内存管理问题的好书。

STL中有很多课程,我想知道如何<使用他们的全部力量。目前我正在使用列表,向量和
地图缺乏一本好书或在线教程详细讨论STL。
Hello,

I''m quite new to C++ so maybe there''s something I miss.
I write a simple board game. It has a board class. This class has a
method that returns the count of pieces a player has on the board. Since
this function does not change anything in the class I declared it as
const. To count all pieces of a given color the functions iterates
through a "map" of CNode-pointers. "CNode" is another class that is
irrelevant to the problem.

Here comes the function: (EColor is an enum of colors, not important here)

int
CBoard::countPieces(EColor color) const
{
// "nodes" is a private class member
// with type map<string,CNode*>
int count = 0;
for (map<string,CNode*>::iterator i=nodes.begin();
i != nodes.end();
i++)
{
// do something with (*i)
// ...
}
return count;
}

The compiler refuses to compile the code, because I violate the
constness of the function in the line where I declare the iterator.
Is there a proper way (or workaround) to leave the function const?
map<string,CNode*>::const_iterator

btw, you can do this using

count_if(themap.begin(),themap.end(),isWhite); // isWhite returns true if the piece is white
Another point: I''ve heard, that it is not good to store pointers to
objects in stl-containers like map, list, vector etc. because of
ownership problems (?).
It''s not necessarily a bad thing. But you need to make sure that you understand
who "owns" a given pointer, and you need to make sure that the map doesn''t hold
on to "dead pointers".

Here''s my question: how are your pointers allocated and deleted ? Who owns them ?
Do you know a good webpage or tutorial about
this topic? I would like to learn how to do it the right way.
This topic is too complicated to be discussed exhaustively in "a webpage". You
need to read a couple of good books that deal with memory management issues.
There are a lot of classes in the STL and I would like to know how to
use their whole power. At the moment I''m just using lists, vectors and
maps in lack of a good book or online tutorial discussing the STL in detail.




两关于C ++标准库的好书:


" Accelerated C ++" Koenig和Moo

标准C ++库A教程和参考 Josuttis


干杯,

-

Donovan Rebbechi
http://pegasus.rutgers.edu/~elflord/


Christof Krueger写道:
Christof Krueger wrote:
你好,

我对C ++很陌生,所以也许我想念的东西。
我写了一个简单的板子游戏。它有一个董事会类。这个类有一个
方法,可以返回玩家在棋盘上的棋子数。由于这个函数不会改变类中的任何内容,我将其声明为
const。为了计算给定颜色的所有部分,函数通过地图迭代。 CNode指针。 " CNode"是另一个与问题无关的课程。

这里有一个功能:( EColor是一个颜色的枚举,这里不重要)

int
CBoard :: countPieces(EColor color)const
{
//节点是一个私有类成员
//类型为map< string,CNode *>
int count = 0;
for(map< string,CNode *> :: iterator i = nodes .begin();
我!= nodes.end();
我++)
//
//用(* i)
//做什么。
}
返回计数;


编译器拒绝编译代码,因为我违反了函数行的
constness我声明了迭代器。
有没有正确的方法(或解决方法)让函数const?

另一点:我听说,存储指针是不好的由于
所有权问题(?),stl-containers中的对象如map,list,vector等。你知道一个关于这个主题的好网页或教程吗?我想学习如何以正确的方式去做。
STL中有很多课程,我想知道如何使用它们的全部功能。目前我正在使用列表,向量和
地图缺乏一本好书或在线教程讨论STL的详细信息。

提前致谢!
Hello,

I''m quite new to C++ so maybe there''s something I miss.
I write a simple board game. It has a board class. This class has a
method that returns the count of pieces a player has on the board. Since
this function does not change anything in the class I declared it as
const. To count all pieces of a given color the functions iterates
through a "map" of CNode-pointers. "CNode" is another class that is
irrelevant to the problem.

Here comes the function: (EColor is an enum of colors, not important here)

int
CBoard::countPieces(EColor color) const
{
// "nodes" is a private class member
// with type map<string,CNode*>
int count = 0;
for (map<string,CNode*>::iterator i=nodes.begin();
i != nodes.end();
i++)
{
// do something with (*i)
// ...
}
return count;
}

The compiler refuses to compile the code, because I violate the
constness of the function in the line where I declare the iterator.
Is there a proper way (or workaround) to leave the function const?

Another point: I''ve heard, that it is not good to store pointers to
objects in stl-containers like map, list, vector etc. because of
ownership problems (?). Do you know a good webpage or tutorial about
this topic? I would like to learn how to do it the right way.
There are a lot of classes in the STL and I would like to know how to
use their whole power. At the moment I''m just using lists, vectors and
maps in lack of a good book or online tutorial discussing the STL in
detail.

Thanks in advance!




使用const_iterator。


for(map< string,CNode *> :: const_iterator i = nodes.begin() ; ...


另外,你应该使用++ i而不是i ++,出于风格和效率的原因(不需要复制+)旧的迭代器状态。


您可能还想研究使用std :: for_each()


例如:


class my_counter:public std :: unary_function<

std :: pair< string,CNode *>,

void>

{

private:

mutable int count;

public:

my_counter ():count(0){}

void operator()(const std :: pair< string,CNode *>& val)const

{

//在这里做一些操纵计数的事情

}

int get_count()const {return count; }

};


int CBoard :: countPieces(EColor颜色)

{

返回std :: for_each(nodes.begin(),

nodes.end(),

my_counter())。get_count();

}



use const_iterator.

for (map<string,CNode*>::const_iterator i = nodes.begin(); ...

Also, you should use ++i instead of i++, for reasons of style and
efficiency (no need to copy the "old" state of the iterator).

You might also want to look into using std::for_each()

e.g.:

class my_counter : public std::unary_function<
std::pair<string,CNode*>,
void >
{
private:
mutable int count;
public:
my_counter() : count(0) { }
void operator()(const std::pair<string,CNode*>& val) const
{
// do something here that manipulates count
}
int get_count() const { return count; }
};

int CBoard::countPieces(EColor color)
{
return std::for_each(nodes.begin(),
nodes.end(),
my_counter()).get_count();
}


Christof Krueger在新闻中写道:bu ************* @ news.t-online.com:


[snip]
Christof Krueger wrote in news:bu*************@news.t-online.com:

[snip]

int
CBoard :: countPieces(EColor color)const
{
/ /节点是一个私有类成员
//类型为map< string,CNode *>
int count = 0;
for(map< string,CNode *> :: iterator i = nodes .begin();


for(map< string,CNode *> :: const_iterator i = nodes.begin();

i!= nodes。结束();
我++)
//做某事(* i)
// ...
}
返回计数; <编译器拒绝编译代码,因为我在声明迭代器的行中违反了函数的常量。
是否有正确的方法(或解决方法)离开函数const?


你的问题是当你调用nodes.begin()时你的调用

返回一个const版本const_iterator。一个const_iterator

像指向const的指针不允许你修改项目

它的迭代(指向)。
所有权问题(?),在stl-containers中的对象如map,list,vector等。


查看智能指针,特别是boost :: shared_ptr:
http://www.boost.org/libs/smart_ptr/shared_ptr.htm


你知道的好吗?关于这个主题的网页或教程?我想学习如何以正确的方式去做。
STL中有很多课程,我想知道如何使用它们的全部功能。目前我正在使用列表,向量和
地图缺乏一本好书或在线教程讨论STL的详细信息。

int
CBoard::countPieces(EColor color) const
{
// "nodes" is a private class member
// with type map<string,CNode*>
int count = 0;
for (map<string,CNode*>::iterator i=nodes.begin();
for ( map<string,CNode*>::const_iterator i=nodes.begin();
i != nodes.end();
i++)
{
// do something with (*i)
// ...
}
return count;
}

The compiler refuses to compile the code, because I violate the
constness of the function in the line where I declare the iterator.
Is there a proper way (or workaround) to leave the function const?
Your problem is that when you call nodes.begin() your calling
the const version that returns a const_iterator. a const_iterator
like a pointer to a const doesn''t allow you to modify the item
that its iterating (pointing) to.

Another point: I''ve heard, that it is not good to store pointers to
objects in stl-containers like map, list, vector etc. because of
ownership problems (?).
Look into smart pointers, in particular boost::shared_ptr:
http://www.boost.org/libs/smart_ptr/shared_ptr.htm

Do you know a good webpage or tutorial about
this topic? I would like to learn how to do it the right way.
There are a lot of classes in the STL and I would like to know how to
use their whole power. At the moment I''m just using lists, vectors and
maps in lack of a good book or online tutorial discussing the STL in
detail.




不,大多数人听到推荐给一本好书。

加速C ++(Koenig& moo)是最推荐的书。


Rob。

-
http ://www.victim-prime.dsl.pipex.com/


这篇关于在const方法中使用map,list等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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