+ 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); }
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++.
+ 1
you are trying to print a string using printf, use cout or it->second.c_str()
0
but why it is not working with printf?
0
thank u