C++中strcmp的頭文件問題


今天在寫程序時遇到的一個問題

  #include <stdio.h>
  #include <string>
  using std::string;



  int main()
  {
      char str[STEL];
      while (scanf("%s", str) && strcmp(str, "end"))
      {
          printf("%s = %u\n", str, hash(str));
      }
      return 0;
  }

寫完用g++編譯,出現error: ‘strcmp’ was not declared in this scope

上網查找發現必須再加上#include <string.h>才能正確編譯執行,即同時存在

#include <string.h>
#include <string>
using std::string;

也就是說strcmp不在C++標准庫中,需要單獨包含strcmp所在的頭文件。

 

自己試了下

#include <cstring>
using namespace std;

也可以完成,即c的標准庫中也包含這個函數


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM