mercredi 24 juillet 2019

Why does my std::map values get converted from decimal to "1"? [on hold]

I am trying to create a std::map object where the key is a pair<char, char> and the value is a double which will be a decimal number between [0,1]. An example of what I'm looking for is: pair<char,char> p1("A", "H"); mymap[p1] will output 0.4321.

The pair<char,char> will be combinations from only 20 letters + "*" and a CSV file contains the decimal values for each pair I want to use. I created 2 vector<char>s that contain letters that will make up my pairs. My row_idx, and col_idx are vector<char> that contain the chars that will be my pairs.

I read in a CSV file with all of the decimals and I use stod() to convert my value into a double. It looks like my conversion works, but when I later print out the content of my map, all my values are 1.

I'm not sure what is wrong with my code, because everything compiles and runs. My cout statements show that my value is a double, and after I add my value to the map and immediately print it, the value is also a double, but when I exit out of the loop, the values go straight to 1.

I have also used cout.precision(10) to make sure that it wasn't rounding.

while (getline(file, line)) {
    for (row_it = row_idx.begin(); row_it != row_idx.end(); row_it++) {
        for (col_it = col_idx.begin(); col_it != col_idx.end(); col_it++) {
            int start = 0;
            std::size_t pos = line.find_first_of(',');

            while(pos != string::npos) {
                double value = stod(line.substr(start, pos));
                //cout << "value: " << value << endl;
                start = pos + 1;
                pos = line.find_first_of(',', start);`enter code here`

                blosum_idx blosum = make_pair(*row_it, *col_it);
                blosum62[blosum] = value;
                cout << blosum62[blosum] << endl; // **Shows correct value**
            }
        }
    }
}

cout << blosum62[blosum] << endl; // **Outputs 1**

I expect the output of my_map[p1] = 0.4321 with pair<char,char> p1 = make_pair('A', 'H). Instead, it is 1.

Aucun commentaire:

Enregistrer un commentaire