本文介绍了引用到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;
}


推荐答案








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?



这会做另一个这样的任务



And this does another such assignment






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

08-14 01:58