In Dev-C++, you can use string name1, name2 without using
include <string> ; why?
For example:
#include <iostream>
//#include <string>
using namespace std;
int main() {
string s1, s2;
cout << "请输入两个字符串:";
cin >> s1 >> s2;
cout << "排序结果:";
if(s1 < s2)
cout << s1 << "," << s2;
else
cout << s2 << "," << s1;
return 0;
}
Compile and run
In C++, the string type is defined in the <string> header file. Generally, if you want to use the string type in your code, you need to include the <string> header file. However, in some cases, you may find that your code still works fine even without explicitly including <string>. This is because other header files (such as <iostream>) may already include <string>, however, this behavior is not standardized.
Because not all compilers do this, it is best to include the <string> header explicitly to ensure code portability and stability.
If your code uses strings, it is recommended to include the <string> header file, which can clearly express "explicitly following the standard C++ specification". This makes your code easier to understand and avoids problems with different compilers or settings.