的std ::地图 [英] std::map

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

问题描述

2个问题:


给定地图定义为


std :: map< int,string> stringmap;


//如何通过迭代器访问data_type(string)?

std :: string spoo(" doh" ;);


stringmap.insert(std :: make_pair(1,spoo));

//我试过这个:

for(std :: map< int,string> :: iterator it = stringmap.begin(); it!=

stringmap.end(); ++ it)

std :: cout<< *它;

但这并没有编译错误,运算符+尚未定义

....

我认为问题是迭代器返回一对。是否可以这样做?b / b
我知道我能做到:

if(stringmap.find(1)! = stringmap.end())std :: cout<< stringmap [1];

第二个问题:


这对BCB6不起作用(导致编译错误,关于转换一对

一个int ???)


// stringmap定义为私有类成员

const std :: string classname :: getstring( size_t index)const {return

stringmap [index];}


但这个编译很好,似乎没有问题:


const std :: string classname :: getstring(size_t index){return

stringmap [index];}


这是正确的?为什么我不能将此函数定义为const?这不是

更改任何

会员数据。


谢谢。

2 questions:

Given a map defined as

std::map<int,string> stringmap;

//How do you access the data_type (string) via an iterator?

std::string spoo("doh");

stringmap.insert(std::make_pair(1,spoo));
// I tried this:
for(std::map<int,string> ::iterator it = stringmap.begin(); it !=
stringmap.end(); ++it)
std::cout << *it;
But this doesn''t compile with error that the operator+ has not been defined
....
I think the problem is that the iterator returns a pair. Is it possible to
do this?

I know I can do:
if(stringmap.find(1) != stringmap.end()) std::cout << stringmap[1];
2nd question:

This doesn''t work with BCB6 (causes a compile error about casting a pair to
an int???)

//stringmap is defined as a private class member
const std::string classname::getstring(size_t index) const { return
stringmap[index];}

but this compiles fine and seems to work with no problems:

const std::string classname::getstring(size_t index) { return
stringmap[index];}

Is this correct? Why can I not define this function as const? It''s not
changing any
member data.

Thanks.

推荐答案

Duane Hebert写道:
Duane Hebert wrote:
//我试过这个:
for(std :: map< int, string> :: iterator it = stringmap.begin(); it!=
stringmap.end(); ++ it)
std :: cout<< *它;


std :: cout<<它 - >第二个;


应该可以做到这一点。


顺便说一句我建议你使用const_iterator而不是

iterator。


我知道我能做到:
if(stringmap.find(1)!= stringmap.end())std :: cout< < stringmap [1];


Brr,这非常低效。为什么不存储找到的

返回值?


if((it = stringmap.find(1))!= stringmap.end())

std :: cout<< it->第二个;


第二个问题:

这对BCB6不起作用(导致关于将一对转换为
一个int ???)
// stringmap定义为私有类成员
const std :: string classname :: getstring(size_t index)const {return
stringmap [index];}

但这个编译很好,似乎没有问题:

const std :: string classname :: getstring(size_t index){return
stringmap [index];}

这是对的吗?为什么我不能将此函数定义为const?这不是改变任何成员数据。
// I tried this:
for(std::map<int,string> ::iterator it = stringmap.begin(); it !=
stringmap.end(); ++it)
std::cout << *it;
std::cout << it->second;

should do the trick.

Btw I recommend that you use a const_iterator instead of an
iterator.

I know I can do:
if(stringmap.find(1) != stringmap.end()) std::cout << stringmap[1];
Brr, this is terribly inefficient. Why dont you store the
returnvalue of find?

if((it = stringmap.find(1)) != stringmap.end())
std::cout << it->second;

2nd question:

This doesn''t work with BCB6 (causes a compile error about casting a pair to
an int???)

//stringmap is defined as a private class member
const std::string classname::getstring(size_t index) const { return
stringmap[index];}

but this compiles fine and seems to work with no problems:

const std::string classname::getstring(size_t index) { return
stringmap[index];}

Is this correct? Why can I not define this function as const? It''s not
changing any member data.




因为operator []不是const。它可能会更改字符串映射的

内容。这个地图在这个

函数中是常量,所以你不能调用它。


使用find代替。


希望这会有所帮助,


Christoph



Because operator[] is not const. It might change the
contents of your stringmap. The map is const in this
function, so you are not allowed to call it.

Use find instead.

hope this helps,

Christoph


Duane Hebert在新闻中写道:YY **** **************@wagner.videotron.net:
Duane Hebert wrote in news:YY******************@wagner.videotron.net:
2个问题:

给定地图定义为

std :: map< int,string> stringmap;

//如何通过迭代器访问data_type(string)?

std :: string spoo(" doh");

stringmap.insert(std :: make_pair(1,spoo));

//我试过这个:
for(std :: map< int,string> :: iterator it = stringmap.begin(); it!=
stringmap.end(); ++ it)
std :: cout<< *它;


将此更改为 - >>秒;如果你想要键那么 - >首先。


但这并没有编译错误,运算符+尚未被定义...
我认为问题是迭代器返回一对。是否可以这样做?

我知道我能做到:
if(stringmap.find(1)!= stringmap.end())std :: cout< ;<
stringmap [1];


这应该工作得很好stringmap [i]应该返回一个std :: string&

第二个问题:

这对BCB6不起作用(导致关于将一对
对转换为int的编译错误???)


我真的不明白这一点,也许你应该发布一个完整的

例子来编译。

// stringmap定义为私有类成员
const std :: string classname :: getstring(size_t index)const {return
stringmap [index];}


返回std: :string const&在这里会更有效。


但这个编译很好,似乎没有问题:

const std :: string classname :: getstring(size_t index) ){返回
stringmap [index];}

这是对的吗?为什么我不能将此函数定义为const?它没有改变任何
成员数据。
2 questions:

Given a map defined as

std::map<int,string> stringmap;

//How do you access the data_type (string) via an iterator?

std::string spoo("doh");

stringmap.insert(std::make_pair(1,spoo));
// I tried this:
for(std::map<int,string> ::iterator it = stringmap.begin(); it !=
stringmap.end(); ++it)
std::cout << *it;
Change this to it->second; if you want the key do it->first.


But this doesn''t compile with error that the operator+ has not been
defined ...
I think the problem is that the iterator returns a pair. Is it
possible to do this?

I know I can do:
if(stringmap.find(1) != stringmap.end()) std::cout <<
stringmap[1];


This should work fine stringmap[i] should return a std::string &
2nd question:

This doesn''t work with BCB6 (causes a compile error about casting a
pair to an int???)
I don''t really understand this, maybe you should post a complete
example that doesen''t compile.

//stringmap is defined as a private class member
const std::string classname::getstring(size_t index) const { return
stringmap[index];}

A return of std::string const & would be more effecient here.

but this compiles fine and seems to work with no problems:

const std::string classname::getstring(size_t index) { return
stringmap[index];}

Is this correct? Why can I not define this function as const? It''s
not changing any
member data.




是的,这是正确的std :: map<> :: operator []未被定义为

const-member函数,这是因为非const版本

将在地图中插入新元素(如果它不存在)。


std :: string classname :: getstring(int index)const

{

std :: map< int,string> ; :: iterator ptr =

stringmap.find(index)




if(ptr!= stringmap.end( ))返回ptr->秒;


//返回std :: string("< some default>");

// throw std :: runtime_error(" Item Not Found");


//选择是你的。

}

HTH


Rob。

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


> > const std :: string classname :: getstring(size_t index){return
> > const std::string classname::getstring(size_t index) { return
stringmap [index];}

这是正确的吗?为什么我不能将此函数定义为const?它不会改变任何成员数据。
stringmap[index];}

Is this correct? Why can I not define this function as const? It''s not
changing any member data.



因为operator []不是const。它可能会更改字符串映射的内容。这个
函数中的地图是常量,所以不允许你调用它。



Because operator[] is not const. It might change the
contents of your stringmap. The map is const in this
function, so you are not allowed to call it.




但为什么不映射定义一个const运算符[]除了非const

一个?



But why doesn''t map define a const operator[] in addition to the non const
one?


这篇关于的std ::地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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