为什么这个函数总是返回0?

时间:2011-09-08 05:47:57

标签: c++

我有一个总是返回0的函数。我认为数据是一个unsigned char,它不是库的一部分。它需要是unsigned char。那么我怎样才能做到这一点,因为我所拥有的不是。

#include <string>
#include <iostream>

int Count( const std::string & str, 
           const std::string & obj ) {
    int a = 0;
    int b = 0;
    int c = 0;
    int d = 0;
    std::string ::size_type pos = 0;
    while( (pos = obj.find( str, pos )) 
                 != std::string::npos ) {
        a++;
        b++;
        c++;
        d++;
    pos += str.size();
    }
    return a;
    return b;
    return c;
    return d;
}
void printTcpContent(unsigned char *data,int)
{
    std::string s = (const char*) data;
    int a = Count( "text/html", s );
    int b = Count( "text/plain", s );
    int c = Count( "image/jpg", s );
    int d = Count( "image/png", s );
    std::cout << a << std::endl;
    std::cout << b << std::endl;
    std::cout << c << std::endl;
    std::cout << d << std::endl;
}

1 个答案:

答案 0 :(得分:0)

请尝试使用此代码:

int Count( const std::string & strToFind, 
       const std::string & strToSearch ) {
    int n = 0;
    std::string ::size_type pos = 0;
    while( (pos = strToSearch.find( strToFind, pos )) != std::string::npos ) {
        n++;
        pos += strToSearch.size();
    }
    return n;
}

此外,您可以通过使用较小的测试字符串调用它来测试它,例如Count(“3”,“1234534”)