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の両方を使用しています...

4

2 に答える 2

5

strlenに適用されますが、には適用されconst char*ませんstring。代わりにを使用できます(そして使用する必要があります)str.length()

于 2012-12-04T01:32:03.403 に答える
0

c文字列とc++文字列libは互いに大きく異なり、混在させることはできません。これに対する修正は、文字列をac文字列として扱うことです。

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

c ++文字列は、cコードおよびcメソッドへの移植を容易にするための内部c文字列を保持します。

また、同じファイルcstringstring.h参照する場合、cはc++libとclibを編成するための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
于 2012-12-04T01:33:55.513 に答える