#include <iostream.h>
#include <string.h>int main()
{
string a="0123456";
string b;
b=a.substr(1,2);
cout << b <<endl;
return 0;
}
不行,而下面写法可以呀
#include <iostream>
#include <string>
using namespace std;
int main()
{
string a="0123456";
string b;
b=a.substr(1,2);
cout << b <<endl;
return 0;
}
两者什么区别?请尽量说清楚,俺是菜鸟!

解决方案 »

  1.   

    这是版本的问题,涉及到命名空间的问题,using namespace std;就是使用了命名空间,它主要是为了避免相同名字而引起解释混淆而用的一种技术。
      

  2.   

    <string>是新版本,<string.h>不是.
      

  3.   

    这个问题偶原来也遇到过
    好像是这样:
    #include <string.h>他就会调用传统的c的string.h
    这个头文件里包括一些老的字符串处理函数,比如strlen
    strcat等
    想用string来定义和使用字符串只能写
    #include <string>
    using namespcae std;
    要不就用mfc的CString把
    这个很方便的
      

  4.   

    对,用<string>的话会调用c++标准库的函数,因此需用using namespace std;