2013-07-10 1 views
1

내가이 사용하는 다른 표준 : : 문자열 내부의 성병의 ocurrence : 문자열을 계산 tryng 오전 :C++ 다른 표준 내부 표준의 ocurrence : 문자열 : 문자열

static int cantidadSimbolos(const std::string & aLinea/*=aDefaultConstString*/, 
            const std::string & aSimbolo/*=aDefaultSimbolSeparador*/) 
     { 
      if(aSimbolo.empty()) return 0; 
      if (aLinea.empty()) return 0; 
      int aResultado=0; 
//This is the line that the compiler doesnt like 
      aResultado=std::count(aLinea.begin(),aLinea.end(),aSimbolo); 
      return aResultado; 
     } 

을하지만 컴파일러 나던 컴파일러가내는 에러입니다.

error: no match for ‘operator==’ in ‘_first._gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* >() == __value’

어떤 도움 ?? thx 사전에!

+2

사람들이 왜 영어 변수 및 함수 이름을 사용하고 있는지 이해할 수 없습니다. 아마 나는 결코 않을 것이다. –

+2

'aaaaaa'안에'aaa '를 찾으려고한다면 몇 번이나 되길 원하나요? –

+0

@ H2CO3 사람들이 아직 그것을 배우지 않았거나 게으르다는 이유로 배웠을 것입니다. – JBL

답변

3

다음 코드는 주어진 문자열의 중첩되지 않는 어커런스 수를 계산합니다.

using namespace std; 

static int countOccurences(const string & line, const string & symbol) { 

if (symbol.empty()) return 0; 
if (line.empty()) return 0; 
int resultCount = 0; 

for (size_t offset = line.find(symbol); offset != string::npos; 
    offset = line.find(symbol, offset + symbol.length())) 
{ 
    resultCount++; 
} 

return resultCount; 

} 

int main(int argc, const char * argv[]) 
{ 
    cout << countOccurences("aaabbb","b") << endl; 

    return 0; 
} 

찾기 기능은 또는 반환 '마지막'따라서 문자열 :: 비영리 단체에 일치하는 값의 첫 번째 요소에 대한 반복자 중 하나를 반환합니다. 이렇게하면 offset + symbol.length()와 겹치지 않게됩니다.

영어 사용자에게 읽기 쉽도록 변수를 영어로 번역하는 데 최선을 다했습니다.

관련 문제