2012-12-04 29 views
0
#include "Q1-VerifyUniqueCharInString.h" 
#include <cstring> 
#include <stdio.h> 
#include <string.h> 

using namespace std; 


bool isUniqueChar(string str) 
{ 

    int length=strlen(str),i=0; 
    bool tab[] = new bool[length]; 
    if (length > 0xff) { 
     return false; 
    } 
    for (; i<length;++i) { 
     if (str[i]) { 
      return false; 
     } 
     tab[str[i]]=true; 
    } 
    return true; 
} 

這是我的代碼,我用gcc + Xcode中....爲什麼總是告訴我不能strlen的發現,我同時使用的CString和string.h ...沒有任何一點毛病包括,爲什麼我找不到的strlen()

回答

5

strlen適用於const char*而不是string。你可以(也應該)改爲使用str.length()

0

c字符串和C++字符串lib的彼此非常不同,並且不能混合使用。的修復程序,這將是用於治療字符串爲C字符串:

strlen(str.c_str()); //convert string into char * 

C++串保持並便於攜帶成C代碼和c的方法內部C字符串。

這也將是很好的注意,cstringstring.h指的是同一個文件,c是C++的方法來組織C++庫和C庫

#include <cstdio> 
#include <cstdlib> //could be stdlib.h, but used to show that this lib is a c lib 
#include <csting> //same as string.h 
#include <string> //c++ string lib