使用地图打印生日,它们最终变成八进制
Printing birthdates using maps and they end up as Octals
如果我为姓名和生日创建地图。当我输入以 0 结尾的生日时,它会将数字更改为八进制。我如何打印生日即 010525
== 4437
所以当我调用 it -> second
时它会打印 010525
#include <stdio.h>
#include <iterator>
#include <string>
#include <map>
#include <iostream>
using namespace std;
int main(){
map<string, int>birth;
birth["Chicken"] = 010525;
birth["Dragon"] = 010266;
map<string, int>::iterator it;
for(it = birth.begin() ; it != birth.end(); it++){
cout << it -> first + " ";
cout << it ->second << endl;
}
}
输出:
Chicken 4437
Dragon 4278
有两种方法可以解决这个问题:
(1) 第一种方式输入生日不带前导0,如下:
birth["Chicken"] = 10525;
birth["Dragon"] = 10266;
然后用setfill()
和setw()
补前导0。
请注意,使用 std:setw() 和 std::setfill() 的代码只能使用新的 C++ 编译器版本进行编译,例如 C++14(或 C++11)
#include <stdio.h>
#include <iterator>
#include <string>
#include <map>
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
map<string, int>birth;
birth["Chicken"] = 10525;
birth["Dragon"] = 10266;
map<string, int>::iterator it;
for(it = birth.begin() ; it != birth.end(); it++){
cout << it -> first + " ";
cout << setfill('0') << setw(6) << it ->second << endl;
}
}
(2) 第二种方法是将生日存储为字符串。这样就可以输入0开头的生日了。
代码如下:
#include <stdio.h>
#include <iterator>
#include <string>
#include <map>
#include <iostream>
using namespace std;
int main(){
map<string, string>birth;
birth["Chicken"] = "010525";
birth["Dragon"] = "010266";
map<string, string>::iterator it;
for(it = birth.begin() ; it != birth.end(); it++){
cout << it -> first + " ";
cout << it ->second << endl;
}
}
您可以在地图中将两者用作字符串,例如:
map<string, string>birth;
其余保持原样。
如果我为姓名和生日创建地图。当我输入以 0 结尾的生日时,它会将数字更改为八进制。我如何打印生日即 010525
== 4437
所以当我调用 it -> second
时它会打印 010525
#include <stdio.h>
#include <iterator>
#include <string>
#include <map>
#include <iostream>
using namespace std;
int main(){
map<string, int>birth;
birth["Chicken"] = 010525;
birth["Dragon"] = 010266;
map<string, int>::iterator it;
for(it = birth.begin() ; it != birth.end(); it++){
cout << it -> first + " ";
cout << it ->second << endl;
}
}
输出:
Chicken 4437
Dragon 4278
有两种方法可以解决这个问题:
(1) 第一种方式输入生日不带前导0,如下:
birth["Chicken"] = 10525;
birth["Dragon"] = 10266;
然后用setfill()
和setw()
补前导0。
请注意,使用 std:setw() 和 std::setfill() 的代码只能使用新的 C++ 编译器版本进行编译,例如 C++14(或 C++11)
#include <stdio.h>
#include <iterator>
#include <string>
#include <map>
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
map<string, int>birth;
birth["Chicken"] = 10525;
birth["Dragon"] = 10266;
map<string, int>::iterator it;
for(it = birth.begin() ; it != birth.end(); it++){
cout << it -> first + " ";
cout << setfill('0') << setw(6) << it ->second << endl;
}
}
(2) 第二种方法是将生日存储为字符串。这样就可以输入0开头的生日了。
代码如下:
#include <stdio.h>
#include <iterator>
#include <string>
#include <map>
#include <iostream>
using namespace std;
int main(){
map<string, string>birth;
birth["Chicken"] = "010525";
birth["Dragon"] = "010266";
map<string, string>::iterator it;
for(it = birth.begin() ; it != birth.end(); it++){
cout << it -> first + " ";
cout << it ->second << endl;
}
}
您可以在地图中将两者用作字符串,例如:
map<string, string>birth;
其余保持原样。