print specific key and value of the map

Hi, I have this code here it print all map key what I need is that how I can print
a specific key and value
so if the out now is
1
2
2x4 =>8
32/2 =>16

I need to print
1
2

32/2 =>16
or
1
2
2x4 =>8


here is my code
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
  #include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <stdlib.h>
using namespace
std;
int main() {
string  x;
int y,k;
map<string,int>mymap;

map<string,int>::const_iterator it;
ifstream in("h.txt");
if (!in) {
cout<< "Unable to open h.txt file" <<endl;
return 1;
}
while (in >> x >> y){
mymap[x] = y;
}
while (in >> x >> y){
mymap[x] = y;
}
for (it = mymap.begin(); it!= mymap.end(); it++){

    cout << it->first<<" =>"<< it->second<<endl;}
cin >> k;
return 0;
}

my h.txt is
1
2
32/2 16
2x4 8
Last edited on
What is the problem?
If yiu need to print value from specific key you can use find member function:
1
2
3
4
5
6
7
std::map<std::string, int>::iterator it;
if((it = mymap.find("32/2")) != mymap.end())
    std::cout << it->first<<" =>"<< it->second << '\n';
//or
auto it = mymap.find("32/2");
if (it != mymap.end())
    std::cout << it->first<<" =>"<< it->second << '\n';
thanks for fast replay
But that means I know all what map contains. I need for example to print the second key and value whatever they are
Well you can do that:
1
2
3
4
5
6
7
8
int index = 2;
auto it = mymap.begin();
for(int i = 0; i < index && it != mymap.end(); ++i)
    ++it;
if(it != mymap.end())
    std::cout << it->first<<" =>"<< it->second << '\n';
else
    std::cout << "not found";
Last edited on
I did this
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
32

#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <stdlib.h>
using namespace
std;
int main() {
string  x;
int y,k;
map<string,int>mymap;;
ifstream in("h.txt");
if (!in) {
cout<< "Unable to open h.txt file" <<endl;
return 1;
}
while (in >> x >> y){
mymap[x] = y;

}
int index = 2;
auto it = mymap.begin();
for(int i = 0; i < index && it != mymap.end(); ++i)
    ++it;
if(it != mymap.end())
    std::cout << it->first<<" =>"<< it->second << '\n';
else
    std::cout << "not found";
cin >> k;
return 0;
}

then I got
1
2
3
4
5
6
7
 In function `int main()':
24  ISO C++ forbids declaration of `it' with no type  
24  cannot convert `std::_Rb_tree_iterator<std::pair<const std::string, int> >' to `int' in initialization
 25 no match for 'operator!=' in 'it != (&mymap)->std::map<_Key, _Tp, _Compare, _Alloc>::end [with _Key = std::string, _Tp = int, _Compare = std::less<std::string>, _Alloc = std::allocator<std::pair<const std::string, int> >]()' 
27  no match for 'operator!=' in 'it != (&mymap)->std::map<_Key, _Tp, _Compare, _Alloc>::end [with _Key = std::string, _Tp = int, _Compare = std::less<std::string>, _Alloc = std::allocator<std::pair<const std::string, int> >]()'  
 base operand of `->' is not a pointer 
28  base operand of `->' is not a pointer 
Oh, you have an outdated compiler.
So change line 23 in your code to std::map<std::string, int>::iterator it = mymap.begin();
thanks that works fine, what if I want to print the first and the second using the same way with index
1
2
32/2 16
2x4 8
Well, move loop and printing in own function and call it with different parameters
(alternatively you can print values from the loop body if you would check if it has enough elements beforehand.)
here is my code so far. It works fine
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
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <stdlib.h>
using namespace
std;
int main() {
string  x;
int y,k;
map<string,int>mymap;;
ifstream in("h.txt");
if (!in) {
cout<< "Unable to open h.txt file" <<endl;
return 1;
}
while (in >> x >> y){
mymap[x] = y;

}
int m=0;
asd:
int index = m + 0;

for (int j=0;j<2;j++){
map<string, int>::iterator it = mymap.begin();
for(int i = 0; i < index && it != mymap.end(); ++i)
    ++it;
    index++;
if(it != mymap.end())
    std::cout << it->first<<" =>"<< it->second << '\n';
else
    cout << "not found";} 
    m = m + 2;
    cin >> k;
    goto asd;

return 0;
}


Now could you please tell how I can do the next
I need to assign the value of the second which is integer to a variable each time. so if the first second is 8 I need a=8 and if the second second is 4 I need b=4 so I can use them later
Topic archived. No new replies allowed.