pat-1124

An important problem is found. If you use a temporary variable to repeatedly input and then create a key-value pair, you can only use cin>> instead of scanf 

#include<iostream>
#include<map>
using namespace std;
map<string,int> pos;
int main(){
	int m,n,s;
	
	scanf("%d %d %d",&m,&n,&s);
	for(int i=1;i<=m;i++){
		string temp;//每一次都要重新声明,重新声明时,一般变量名不变,地址也不变; 
	
		cin>>temp;// 所以最好使用cin这就是一个值传递,会copy一个新地址,实现除去地址的值传递
	 
		int flag=0;
		if(s>m){if(i==1) printf("Keep going...\n");
		}
		else if(i==s){//更新s
		if(pos[temp]==0){//本质是地址建立键值对 
			pos[temp]=1;
			printf("%s\n",temp.c_str());
			flag=1;
			s=s+n;
		}
		if(flag==1) continue;
		if(i==s)s=s+1;
		}
	}
	return 0;
} 

to sum up

Essentially the difference between quoting and addressing

It can be understood that cin does not need the address of temp. He will give temp an alias and allocate a new memory, so that each key-value pair corresponds to the new value. The key-value pair is essentially the correspondence between address and address. , The key is that the address of the key must be assigned a new one, and the value will be redistributed, so as long as the key corresponds to the new address, it cannot be wrong

And scanf, directly change the original address, the address is always one, resulting in the key address in the key-value pair has not changed, but the incoming data has been changing, and the tag value is always the same, causing an error

So if you can use cin, especially the repeated assignment of temporary variables, use cin 

Simply understand that cin can truly realize value-to-value without involving the problem of address overlap

English 

problem

Pay attention to the details of these compilers

 

Guess you like

Origin blog.csdn.net/m0_45359314/article/details/113076899