引用到std :: map有什么问题? [英] What is wrong with reference into std::map?

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

问题描述

预期的节目输出:

键是:0字符串是:你好

键是:1字符串是:再见

键是:2字符串是:结束


实际输出:

键是:0字符串是:结束

键是: 1字符串是:

键是:2字符串是:


我做错了什么?


如果我每次都声明我的参考新鲜,比如把{}放在我插入的

个地方并做

std :: string& MyString =(* it).second;

每次出来都正确。


为什么我不能重复使用这些参考文件?


花了很长时间才发现我的程序中导致这种情况的原因。这个

只是一个显示问题的测试程序。


#include< iostream>

#include< string>

#include< map>


std :: map< unsigned int,std :: string> MyMap;


int main()

{

unsigned int ID = 0;

std ::地图< unsigned int,std :: string> :: iterator it = MyMap.insert(

MyMap.end(),std :: make_pair< unsigned int,std :: string>(ID,

std :: string()));

std :: string& MyString =(* it).second;

MyString =" Hello";


ID = 1;

it = MyMap.insert(MyMap.end(),std :: make_pair< unsigned int,

std :: string>(ID,std :: string()));

std :: string& MyString2 =(* it).second;

MyString =" Goodbye";


ID = 2;

it = MyMap.insert(MyMap.end(),std :: make_pair< unsigned int,

std :: string>(ID,std :: string()));

MyString =(* it).second;

MyString =" The End";


for(std :: map< unsigned int,std :: string> :: iterator i = MyMap.begin();

i!= MyMap.end(); ++ i)

{

std :: cout<< 关键是: << (* i).first<< "字符串是: <<

(* i).second<< std :: endl;

}


std :: string wait;

std :: cin>>等待;

}

Expected output of program:
Key is: 0 String is: Hello
Key is: 1 String is: Goodbye
Key is: 2 String is: The end

Actual output:
Key is: 0 String is: The End
Key is: 1 String is:
Key is: 2 String is:

What am I doing wrong?

If I declare my reference fresh each time, such as putting { } around the
places I insert and doing
std::string& MyString = (*it).second;
each time it will come out right.

Why can''t I reuse the references?

It took a long time to find out what was causing this in my program. This
is just a test program showing the issue.

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

std::map<unsigned int, std::string> MyMap;

int main ()
{
unsigned int ID = 0;
std::map< unsigned int, std::string>::iterator it = MyMap.insert(
MyMap.end(), std::make_pair< unsigned int, std::string >( ID,
std::string() ) );
std::string& MyString = (*it).second;
MyString = "Hello";

ID = 1;
it = MyMap.insert( MyMap.end(), std::make_pair< unsigned int,
std::string >( ID, std::string() ) );
std::string& MyString2 = (*it).second;
MyString = "Goodbye";

ID = 2;
it = MyMap.insert( MyMap.end(), std::make_pair< unsigned int,
std::string >( ID, std::string() ) );
MyString = (*it).second;
MyString = "The End";

for ( std::map< unsigned int, std::string >::iterator i = MyMap.begin();
i != MyMap.end(); ++i )
{
std::cout << "Key is: " << (*i).first << " String is: " <<
(*i).second << std::endl;
}

std::string wait;
std::cin >> wait;
}


推荐答案



"吉姆兰斯顿 < TA ******* @ rocketmail.com>在消息中写道

新闻:TB ************* @ fe04.lga ...

"Jim Langston" <ta*******@rocketmail.com> wrote in message
news:TB*************@fe04.lga...
预期的节目输出:
关键是:0字符串是:你好
关键是:1字符串是:再见
关键是:2字符串是:结束

实际输出:
键是:0字符串是:结束
键是:1字符串是:
键是:2字符串是:

我做错了什么?
std :: string&的位置。 MyString =(* it).second;
每次它都会正确显示。

为什么我不能重复使用这些参考文件?

它需要一个很长一段时间来找出我的程序中导致这种情况的原因。这个
只是一个显示问题的测试程序。

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

std :: map< unsigned int,std :: string> MyMap;

int main()
{
unsigned int ID = 0;
std :: map< unsigned int,std :: string> :: iterator it = MyMap.insert(
MyMap.end(),std :: make_pair< unsigned int,std :: string>(ID,
std: :string()));
std :: string& MyString =(* it).second;
MyString =" Hello";

ID = 1;
它= MyMap.insert(MyMap.end(),std: :make_pair< unsigned int,
std :: string>(ID,std :: string()));
std :: string& MyString2 =(* it).second;


这是一次破碎的诊断尝试。即使这一行更改为:

std :: string& MyString2 =(* it).second


输出相同。

MyString =" Goodbye";

ID = 2;
它= MyMap.insert(MyMap.end(),std :: make_pair< unsigned int,
std :: string>(ID,std :: string()));
MyString =(* it).second;
MyString =" The End" ;;

for(std :: map< unsigned int,std :: string> :: iterator i =
MyMap.begin(); i!= MyMap.end(); ++ i)
{
std :: cout<< 关键是: << (* i).first<< "字符串是: <<
(* i).second<< std :: endl;
}

std :: string wait;
std :: cin>>等;;
Expected output of program:
Key is: 0 String is: Hello
Key is: 1 String is: Goodbye
Key is: 2 String is: The end

Actual output:
Key is: 0 String is: The End
Key is: 1 String is:
Key is: 2 String is:

What am I doing wrong?

If I declare my reference fresh each time, such as putting { } around the
places I insert and doing
std::string& MyString = (*it).second;
each time it will come out right.

Why can''t I reuse the references?

It took a long time to find out what was causing this in my program. This
is just a test program showing the issue.

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

std::map<unsigned int, std::string> MyMap;

int main ()
{
unsigned int ID = 0;
std::map< unsigned int, std::string>::iterator it = MyMap.insert(
MyMap.end(), std::make_pair< unsigned int, std::string >( ID,
std::string() ) );
std::string& MyString = (*it).second;
MyString = "Hello";

ID = 1;
it = MyMap.insert( MyMap.end(), std::make_pair< unsigned int,
std::string >( ID, std::string() ) );
std::string& MyString2 = (*it).second;
This was a broken attempt to diagnose. Even with this line changed to:
std::string& MyString2 = (*it).second

the output is the same.
MyString = "Goodbye";

ID = 2;
it = MyMap.insert( MyMap.end(), std::make_pair< unsigned int,
std::string >( ID, std::string() ) );
MyString = (*it).second;
MyString = "The End";

for ( std::map< unsigned int, std::string >::iterator i =
MyMap.begin(); i != MyMap.end(); ++i )
{
std::cout << "Key is: " << (*i).first << " String is: " <<
(*i).second << std::endl;
}

std::string wait;
std::cin >> wait;



Jim Langston写道:
Jim Langston wrote:
std :: string& MyString2 =(* it).second;
MyString =" Goodbye" ;;


你要踢自己,但是你想要在上面一行的
中使用MyString2。你正在这里插入第一个插入的

字符串。
ID = 2;
它= MyMap.insert(MyMap.end(),std :: make_pair< unsigned int,
std :: string>(ID,std :: string()));
MyString =(* it).second;


这不会初始化引用,它会将第二个

的值分配到MyString初始化的位置(第一次插入)。

MyString =" The End" ;;


这又做了另一个这样的任务

std::string& MyString2 = (*it).second;
MyString = "Goodbye";
You''re going to kick yourself, but you want MyString2 in
the above line. You''re righting into the first inserted
string here.
ID = 2;
it = MyMap.insert( MyMap.end(), std::make_pair< unsigned int,
std::string >( ID, std::string() ) );
MyString = (*it).second;
This doesn''t initialize a reference, it assigns the value of second
into where MyString was initialized (your first insertion).
MyString = "The End";
And this does another such assignment





Ron Natalie < ro*@spamcop.net>在消息中写道

news:44 ************** @ spamcop.net ...

"Ron Natalie" <ro*@spamcop.net> wrote in message
news:44**************@spamcop.net...
Jim Langston写道:
Jim Langston wrote:
std :: string& MyString2 =(* it).second;
MyString =" Goodbye";
你要踢自己,但是你想要MyString2在上面这一行。你正在这里插入第一个插入的
字符串。
std::string& MyString2 = (*it).second;
MyString = "Goodbye";
You''re going to kick yourself, but you want MyString2 in
the above line. You''re righting into the first inserted
string here.

ID = 2;
它= MyMap.insert(MyMap.end(),std: :make_pair< unsigned int,
std :: string>(ID,std :: string()));
MyString =(* it).second;

ID = 2;
it = MyMap.insert( MyMap.end(), std::make_pair< unsigned int,
std::string >( ID, std::string() ) );
MyString = (*it).second;


<这不会初始化引用,它会将第二个值分配到MyString初始化的位置(第一次插入)。



This doesn''t initialize a reference, it assigns the value of second
into where MyString was initialized (your first insertion).




哦好吧,拍摄。你是对的。那完全超越了我。如果在同一行声明了引用

,那么它会初始化它。


嗯...那么如何分配已经初始化的引用?

IE

int int1;

int int2;

int& MyRef = int1;

我该怎么做才能让MyRef现在指向int2?或者我可以吗?



Oh, well, shoot. You''re right. That totally excaped me. If the reference
is declared on the same line, then it initializes it.

Hmm.. so how do you assign a reference that has already been initialized?
I.E.
int int1;
int int2;
int& MyRef = int1;
Who do I do to get MyRef to point to int2 now? Or can I?

MyString =" The End";
MyString = "The End";



这会做另一个这样的任务



And this does another such assignment






这篇关于引用到std :: map有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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