+ 1

help in c++,it is giving wrong output please help

#include<stdio.h> #include<map> using namespace std; int main() { map<int,string>m; m.insert(make_pair(9,"hi")); m.insert(make_pair(1,"hi3")); m.insert(make_pair(12,"hi33")); map<int,string>::iterator it; it=m.find(9); if(it==m.end()) printf("%s","not found"); else printf("%s",it->second); }

18th Jun 2018, 6:30 AM
Bahubali
Bahubali - avatar
4 Antworten
+ 3
because printf is made for c and not c++. it prints c strings which are just arrays of chars that are terminated by '\0', but string in c++ is a class and not a c string and therefore printf can‘t handle it. just use cout if you are using c++.
18th Jun 2018, 8:06 AM
Max
Max - avatar
+ 1
you are trying to print a string using printf, use cout or it->second.c_str()
18th Jun 2018, 7:12 AM
Max
Max - avatar
0
but why it is not working with printf?
18th Jun 2018, 7:40 AM
Bahubali
Bahubali - avatar
0
thank u
18th Jun 2018, 8:26 AM
Bahubali
Bahubali - avatar