今天在看C++PRIMER做课后题的时候突然发现一个小问题,题目是3.23,起先自编的代码如下
- #include <iostream>
- #include <bitset>
- #include <cstring>
- using namespace std;
- void main()
- {
- bitset<64> bitvec(32);
- cout<<bitvec<<endl;
- bitset<32> bv(1010101);
- cout<<bv<<endl;
- string bstr;
- cin>>bstr;
- bitset<8> bv2(bstr);
- cout<<bv2<<endl;
- }
结果出现一个错误 -------------------Configuration: 3_23 - Win32 Debug-------------------- Compiling... 3_23.cpp d:/c++学习/c++ primer 课后练习/3_23/3_23.cpp(12) : error C2679: binary '>>' : no operator defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits,class std::allocator >' (or there is no acce ptable conversion) 执行 cl.exe 时出错. 3_23.exe - 1 error(s), 0 warning(s) 搞的我不明不白的,简简单单输入或输出一个string类的对象都会出错,很无奈,只能上网查查。十几分钟后大概有了结果,看别人写的程序好像都是
- #include<string>
而非cstring。但我记得我学C++的时候老师教我的就是Cstring,当时还花了不少功夫去记呢,不禁觉得很奇怪。我把我的cstring改成string后,果然就成功运行了,毛病就出在这个头文件上。可是到底这两个头文件有什么区别呢?我虽然很好奇但是由于水平有限暂时没办法了解,希望知道的朋友能告诉我。