why I'm not getting the right values

Aug 3, 2018 at 11:15am
Hey,

I've a map that I set on it these values

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

    map<string, size_t>::iterator it;

    for ( it = indexBin.begin(); it != indexBin.end(); it++ )
    {
        std::cout << it->first  // string (key)
                  << ':'
                  << it->second   // string's value
                  << std::endl ;
    }

file0_first_.bin:8
file0_second_.bin:15
file1_first_.bin:8
file1_second_.bin:15
file2_first_.bin:8
file2_second_.bin:9
file3_first_.bin:8
file3_second_.bin:15
file4_first_.bin:8
file4_second_.bin:15
file5_first_.bin:8
file5_second_.bin:9
file6_first_.bin:8
file6_second_.bin:9
file7_first_.bin:8
file7_second_.bin:9
file8_first_.bin:8
file8_second_.bin:15
file9_first_.bin:8
file9_second_.bin:15


map has unique keys. so when I try to access by key using find
 
cout << indexBin.find(Name.c_str())->first <<" / "<<indexBin.find(Name.c_str())->second << endl;

I got nothing.

can somebody explain
Last edited on Aug 3, 2018 at 11:22am
Aug 3, 2018 at 11:37am
What is Name? Are you sure it matches one of the keys exactly. No extra spaces? No difference in letter case?

It's usually a good idea to check that find was successful before trying to access the key-value pair.
1
2
3
4
5
6
7
8
9
auto it = indexBin.find(Name)
if (it != indexBin.end())
{
	cout << it->first << " / " << it->second << endl;
}
else
{
	cout << "No key \"" << Name << "\" found in map" << endl;
}
Last edited on Aug 3, 2018 at 11:39am
Aug 3, 2018 at 12:07pm
Name string containing bin name it changes while I loop over these bin files
yes I sure keys matches exactly
Aug 3, 2018 at 12:17pm
I forgot to add the extention, error of beginners
 
indexBin.find(Name+".bin");

thank you by the way
Last edited on Aug 3, 2018 at 12:18pm
Topic archived. No new replies allowed.