在我的代碼,我有以下部分:C++值不被改變一個隱藏的方式
void do_avg(){
std::map<int,hqs>::iterator it = vals.begin();
while(it != vals.end()){
int q = (*it).first;
hqs val = (*it++).second;
val.hs = 0;
printf("%i: %f\n",q,val.hs);
}
it = vals.begin();
while (it != vals.end()){
int q = (*it).first;
hqs val = (*it++).second;
printf("%i: %f\n",q,val.hs);
}
}
打印:
0: 0.000000
1: 0.000000
2: 0.000000
4: 0.000000
5: 0.000000
8: 0.000000
9: 0.000000
10: 0.000000
13: 0.000000
0: 14.713500
1: 0.050911
2: 0.006717
4: 0.074708
5: 0.020139
8: 0.049042
9: 0.033990
10: 0.033952
13: 0.005567
_some定義: vals
定義as:
std::map<int, hqs> vals;
and hqs
is a structure:
typedef struct{
short q;
double hs;
std::vector <double> ah;
double error;
short counter;
}hqs;
代碼的報頭還包括:
#include <map>
#include <math.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sstream>
void do_avg();
出於某種原因,val.hs的值在第一和第二次運行之間改變。有什麼想法嗎?
我知道,我希望它們是0,但爲什麼它們不保持0? (當我意識到他們不保持0時,問題就開始了)。 – Yotam
...並且改變val.hs隻影響val,而不影響地圖中的數據。 val是你的一個地圖值的副本。 –
對,我應該使用指針或其他什麼,謝謝 – Yotam