返回STL映射对象 [英] Returning a STL Map Object

查看:71
本文介绍了返回STL映射对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个STL新手试图建立一个DUTBus类,它有一张地图

对象。在我的成员函数中,我尝试返回一个Map对象,但是,

它似乎不起作用。我指的是Bus getBus()

函数。


我需要这样做,因为要添加设备引脚(通过

registerPin()函数。换句话说,我需要通过在对象中插入新的(键,值)对来修改地图。

。但是,当我试图通过返回它来获取总线对象时,它不起作用!


请帮忙!


谢谢

Anand


#include< string>

typedef map< int,string> Bus;

typedef Bus :: iterator BusIterator;


class DUTBus

{

private:

Bus _bus;

public:

DUTBus(){}

Bus getBus();

inline int getBusSize(){return(_bus.size());}

void registerPin(int,string);


};

巴士DUTBus :: getBus()

{

返回(_bus);

}


void DUTBus :: registerPin(int _idx,string _pin)

{

Bus myBus = this-> getBus();

BusIterator iter;

iter = myBus .begin();

myBus.insert(pair< int,string>(_ idx,_pin));

cout<< Inserted Bus [" << _idx<< "] \t = \t" << _pin;

}


main(无效)

{

巴士myBus ;

DUTBus * DataBus = new DUTBus();

DataBus-> registerPin(1," T_x_p_ad1");

DataBus- > registerPin(2," T_x_p_ad2");

DataBus-> registerPin(3," T_x_p_ad3");


myBus = DataBus- > getBus();

cout<< 这辆公共汽车的大小为 << myBus.getBusSize()<< endl;

}

[见 http://www.gotw.ca/resources/clcm.htm 了解有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

I am an STL newbie trying to build a class DUTBus that has a map
object. In my member function, I try to return a Map Object, however,
it does not seem to work. I am referring to "Bus getBus()"
function.

I need to do this because in order to "add" device pins (via
registerPin() function. In other words, I need to modify the map by
inserting new (key,value) pairs into the object. However, when I try to
access the bus object by returning it, it doesnt work!

Please help!

Thanks
Anand

#include <string>
typedef map<int, string> Bus;
typedef Bus::iterator BusIterator;

class DUTBus
{
private:
Bus _bus;
public:
DUTBus() { }
Bus getBus();
inline int getBusSize() { return (_bus.size()); }
void registerPin(int, string);

};
Bus DUTBus::getBus()
{
return(_bus);
}

void DUTBus::registerPin(int _idx, string _pin)
{
Bus myBus = this->getBus();
BusIterator iter;
iter = myBus.begin();
myBus.insert(pair<int, string>(_idx,_pin));
cout << "Inserted Bus[" << _idx << "]\t=\t" << _pin;
}


main(void)
{
Bus myBus;
DUTBus* DataBus = new DUTBus();
DataBus->registerPin(1,"T_x_p_ad1");
DataBus->registerPin(2,"T_x_p_ad2");
DataBus->registerPin(3,"T_x_p_ad3");

myBus = DataBus->getBus();
cout << "This bus has a size of " << myBus.getBusSize() << endl;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

推荐答案

" anand" < WR ******** @ gmail.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...
"anand" <wr********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
我是一个STL新手试图建立一个具有地图对象的DUTBus类。在我的成员函数中,我尝试返回一个Map对象,但是,它似乎不起作用。我指的是Bus getBus()功能。

我需要这样做,因为要添加。设备引脚(通过
registerPin()函数。换句话说,我需要通过在对象中插入新的(键,值)对来修改地图。但是,当我尝试
时通过返回来访问公共汽车对象,它不起作用!

请帮助!

感谢
Anand

#include< string> ;
typedef map< int,string> Bus;
typedef Bus :: iterator BusIterator;

class DUTBus
{
私人:
总线_bus;
注意:标识符名称中最好避免使用前导下划线

(它们在某些情况下保留用于实现)。

尾随下划线(bus_)因此通常是首选。

public:
DUTBus(){}
Bus getBus();
问题:在C ++中,这会返回(深层)副本存储的_bus

对象。如果要返回对象引用,则需要

明确地这样做:

Bus& getBus() {return _bus;}

Howe ver,后者违背了拥有私人

数据成员的目的,因此最好避免使用。

一个有时有用的替代方案,它仍然允许班级

控制其不变量,是:

Bus const& getBus()const {return _bus; } $ / $
inline int getBusSize(){return(_bus.size()); }
注意:内联在这里是多余的,''const''会更好:

int getBusSize()const {return(_bus.size()); }

void registerPin(int,string);

};

Bus DUTBus :: getBus()
{
return(_bus);
}

void DUTBus :: registerPin(int _idx,string _pin)同样,最好避免使用前导下划线。在这种情况下使用它们是一种非常罕见的C ++风格。

{
Bus myBus = this-> getBus();
这将创建_bus对象的本地副本。然后修改并销毁本地

副本,而_bus数据

成员保持不变。 ***这就是导致你的''bug'的原因'***

直接使用_bus数据成员。或者,如果您想要对数据成员进行本地

引用,请使用:

Bus& myBus = _bus;

BusIterator iter;
iter = myBus.begin();
首选组合声明和实例:

BusIterator const iter = myBus.begin();

但无论如何,此函数不需要''iter'' 。

myBus.insert(pair< int,string>(_ idx,_pin));
cout<< Inserted Bus [" << _idx<< "] \t = \t" << _pin;
}
如果用以下内容替换它的主体,该函数将正常工作:

_bus.insert(pair< int,string>(_ idx,_pin));

甚至更容易,因为你似乎并不关心一个已经存在相同数字的销钉



_bus [_idx] = _pin;

main(无效)
{
巴士myBus;
最好在首次使用时声明局部变量... DUTBus * DataBus = new DUTBus();
除非有必要,否则不要在堆上分配对象。只需使用:

DUTBus DataBus; DataBus-> registerPin(1,T_x_p_ad1);
DataBus-> registerPin(2,T_x_p_ad2);
DataBus-> registerPin(3,T_x_p_ad3);

myBus = DataBus-> getBus();
这又创建了一个DataBus-> _bus的本地*副本*,其中
可能不是您想要的(尽管在这种情况下它可以正常工作)。

cout<< 这辆公共汽车的大小为 << myBus.getBusSize()<< endl;
}
I am an STL newbie trying to build a class DUTBus that has a map
object. In my member function, I try to return a Map Object, however,
it does not seem to work. I am referring to "Bus getBus()"
function.

I need to do this because in order to "add" device pins (via
registerPin() function. In other words, I need to modify the map by
inserting new (key,value) pairs into the object. However, when I try to
access the bus object by returning it, it doesnt work!

Please help!

Thanks
Anand

#include <string>
typedef map<int, string> Bus;
typedef Bus::iterator BusIterator;

class DUTBus
{
private:
Bus _bus; NB: Leading underscores are best avoided in identifier names
(they are reserved for the implementation in some contexts).
Trailing underscores (bus_) are therefore usually preferred.
public:
DUTBus() { }
Bus getBus(); Problem: In C++, this returns a (deep) copy of the stored _bus
object. If you want to return an object reference, you need to
do so explicitly:
Bus& getBus() { return _bus; }
However, the latter defeats the purpose of having a private
data member, and is therefore best avoided.
A sometimes useful alternative, which still allows the class to
control its invariants, is:
Bus const& getBus() const { return _bus; }
inline int getBusSize() { return (_bus.size()); } NB: inline is redundant here, and a ''const'' would be better:
int getBusSize() const { return (_bus.size()); }
void registerPin(int, string);

};
Bus DUTBus::getBus()
{
return(_bus);
}

void DUTBus::registerPin(int _idx, string _pin)Again, leading underscores are best avoided. And it is
a quite uncommon C++ style to use them in this context.
{
Bus myBus = this->getBus(); This creates a local copy of the _bus object. That local
copy is then modified, and destroyed, while the _bus data
member remains unchanged. ***this is what causes your ''bug''***
Use the _bus data member direcly. Or if you want a local
reference to the data member, use:
Bus& myBus = _bus;
BusIterator iter;
iter = myBus.begin(); Prefer combining declaration and instanciation:
BusIterator const iter = myBus.begin();
But anyway, ''iter'' is not needed in this function.
myBus.insert(pair<int, string>(_idx,_pin));
cout << "Inserted Bus[" << _idx << "]\t=\t" << _pin;
} The function will work correctly if you replace its body with:
_bus.insert( pair<int, string>(_idx,_pin) );
Or even easier, since you don''t seem to care about wheter a pin
with the same number already existed:
_bus[_idx] = _pin;
main(void)
{
Bus myBus; It is better to declare local variables on first use... DUTBus* DataBus = new DUTBus(); Don''t allocate objects on the heap unless it is necessary. Just use:
DUTBus DataBus; DataBus->registerPin(1,"T_x_p_ad1");
DataBus->registerPin(2,"T_x_p_ad2");
DataBus->registerPin(3,"T_x_p_ad3");

myBus = DataBus->getBus(); This again creates a local *copy* of the DataBus->_bus , which
may not be what you want (although it will work ok in this context).
cout << "This bus has a size of " << myBus.getBusSize() << endl;
}




我希望这会有所帮助。请注意,在尝试编写更复杂的程序之前,花更多时间学习C ++可能是个好主意...

问候,Ivan

-
http://ivan.vecerina.com/ contact /?subject = NG_POST < - 电子邮件联系表格

Brainbench MVP for C ++<> http://www.brainbench.com


[见 http://www.gotw.ca/resources /clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]



I hope this helps. Note that it might be a good idea to spend more time
studying C++ before trying to write more complex programs...
Regards, Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


你让一辆公共汽车与DUTBus混淆,插入不能工作

因为你' '插入_bus的本地副本,而不是_bus本身。

另外,不要忘记包含必需的标题并有资格获得

std命名空间:


#include< iostream>

#include< map>

#include< utility>

#include< string>


typedef std :: map< int,std :: string>公共汽车;

typedef Bus :: iterator BusIterator;


class DUTBus

{

private:

Bus _bus;

public:

Bus getBus();

inline int getBusSize(){return( _bus.size()); } $ / $
void registerPin(int,std :: string);

};


Bus DUTBus :: getBus()

{

返回(_bus);

}


void DUTBus :: registerPin(int _idx ,std :: string _pin)

{

_bus.insert(std :: pair< int,std :: string>(_ idx,_pin));

std :: cout<< Inserted Bus [" << _idx<< "] \t = \t" << _pin;

}


int main(无效)

{

巴士myBus;

DUTBus * DataBus = new DUTBus();


DataBus-> registerPin(1," T_x_p_ad1");

DataBus - > registerPin(2," T_x_p_ad2");

DataBus-> registerPin(3," T_x_p_ad3");


myBus = DataBus - > getBus();

std :: cout<< 这辆公共汽车的大小为 << myBus.size()<< std :: endl;

}

[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

You''re confusing a Bus with a DUTBus, and the insertions don''t work
because you''re inserting into a local copy of _bus, not _bus itself.
Also, don''t forget to include the requisite headers and qualify for the
std namespace:

#include <iostream>
#include <map>
#include <utility>
#include <string>

typedef std::map<int, std::string> Bus;
typedef Bus::iterator BusIterator;

class DUTBus
{
private:
Bus _bus;
public:
Bus getBus();
inline int getBusSize() { return (_bus.size()); }
void registerPin(int, std::string);
};

Bus DUTBus::getBus()
{
return(_bus);
}

void DUTBus::registerPin(int _idx, std::string _pin)
{
_bus.insert(std::pair<int, std::string>(_idx,_pin));
std::cout << "Inserted Bus[" << _idx << "]\t=\t" << _pin;
}

int main(void)
{
Bus myBus;
DUTBus* DataBus = new DUTBus();

DataBus->registerPin(1,"T_x_p_ad1");
DataBus->registerPin(2,"T_x_p_ad2");
DataBus->registerPin(3,"T_x_p_ad3");

myBus = DataBus->getBus();
std::cout << "This bus has a size of " << myBus.size() << std::endl;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


>我尝试返回一个Map对象,然而,它似乎不起作用。

你能不能更具体地说明什么不起作用?

编译时间错误?意外的运行时行为?


据我所知,上述代码唯一不对的是

行:

cout<< 这辆公共汽车的大小为 << myBus.getBusSize()<< endl;

..你在Bus类型的对象上调用getBusSize()。类型总线是

实际上是一个地图< int,string>当然,没有会员

函数getBusSize()。

[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang .C ++。主持。第一次海报:做到这一点! ]

> I try to return a Map Object, however, it does not seem to work.
Could you please be a bit more specific about what doesn''t work?
Compile time error? Unexpected runtime behavior?

As far as I can tell the only thing wrong with the above code is the
line:
cout << "This bus has a size of " << myBus.getBusSize() << endl;
.. You are calling getBusSize() on an object of type Bus. Type Bus is
really a map<int, string> and, of course, does not have a member
function getBusSize().
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


这篇关于返回STL映射对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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