2014-02-27 4 views
0

단어와 태그 (단어/태그 모양)로 텍스트 파일을 구문 분석하고 있습니다. 내 파일에서 고유 한 태그의 수를 찾으려고 노력하고 있으며 C++에서 태그를 삽입하기 위해 정렬되지 않은 세트를 사용하고 있습니다. 그러나 무작위 로이 예외를 받고있는 것 : "(삽입의 미확인 된 숫자) 삽입 한 후"EXC_I386_GPFLT "내 정렬되지 않은 집합으로 .Xcode 말하기 때문에 나는 메모리가 부족하다고 생각하지 않습니다 ~ 300 - 400킬로바이트 여기 C++에서 정렬되지 않은 세트의 크기에 제한이 있습니까

내 주요 기능이다.

#include <iostream> 
#include <map> 
#include <string> 
#include <vector> 
#include <unordered_set> 

class ParseTrain{ 
private: 

    //map to relate the work and part of speech tag 
    std::vector <std::map<std::string, std::string>> _sentence; 
    std::unordered_set<std::string> _tag; 
public: 

    //constructor to parse file 
    //takes in path to file to parse 
    ParseTrain(std::string fName); 

    inline size_t getSizeOfTag(){ 
     return _tag.size(); 
    } 
}; 
: 여기
#include "ParseTrain.h" 
#include <fstream> 
#include <string> 
#include <iostream> 


ParseTrain::ParseTrain(std::string fName){ 
    std::ifstream file(fName); 
    std::string word; 

    if(!file) 
     return; 

    //read file by word 
    while(file >> word){ 
     char * cWord = new char (word.size()+1); 
     std::strcpy(cWord,word.c_str()); 

     char *p = std::strtok(cWord, "/"); 
     std::string key = p; 
     p = strtok(NULL, " "); 
     std::string value = p; 
     std::cout<<value<<std::endl; 
     _tag.insert(value);//getting exception thrown after undeterminable number of inserts at this line 
     delete [] cWord; 
     cWord = NULL; 
    } 
} 

내 ParseTrain.h입니다 : 여기

#include <iostream> 
#include "ParseTrain.h" 

int main(int argc, const char * argv[]) 
{ 
    ParseTrain p("~/Desktop/treebank.5290.train"); 
    std::cout<<"The Number of Tags is: "<<p.getSizeOfTag()<<std::endl; 
    return 0; 
} 

내 ParseTrain.cpp입니다 예외가 삽입시 발생하는 이유 정말 알아낼 수 없습니다

Pierre/NP Vinken/NP ,/, 61/CD years/NNS old/JJ ,/, will/MD join/VB the/DT board/NN as/IN a/DT nonexecutive/JJ director/NN Nov./NP 29/CD ./. 
Mr./NP Vinken/NP is/VBZ chairman/NN of/IN Elsevier/NP N.V./NP ,/, the/DT Dutch/NP publishing/VBG group/NN ./. 

: 마침내 여기

그리고 내가 태그를 구문 분석하고 얻으려고 노력하고있는 텍스트 파일의 작은 부분이다. 내가 생각할 수있는 유일한 점은 정렬되지 않은 세트의 크기에 제한이있을 수 있지만 그와 같은 작은 메모리를 사용하는 것은 이상한 것처럼 보입니다. 어떤 도움이라도 대단히 감사하겠습니다.

+0

항상 한계가 있습니다. 하지만 아마도 * P == nullptr 일 때 가치에서 읽으려는 경향이 있습니다. 문자열을 줄이고 마지막 부분 만 사용하십시오. 그런 다음 코드를 단계별로 실행하고 현지인을 검사하는 데 너무 오래 걸릴 것입니다. strtok이 null을 반환하는 경우를 처리해야합니다. – Dan

+1

메모리는 정말로 무제한 일 수는 없지만'strtok', 원시'new' 및 원시'delete'로 채워진 코드는 훨씬 더 많은 가능성이있는 목표를 이끌어냅니다. –

+0

이것을 디버깅하는 명백한 방법은 파싱 코드에 몇 가지 에러 ​​검사를하는 것입니다 :'strtok' 이후에는 포인터가 NULL이 아니라고 가정합니다 - 입력에 나쁜 라인이 있다면 어떨까요? 카운터를 추가하여 어떤 입력 행이 포함되어 있는지 알 수 있습니다. 모든 행을 인쇄하는 것이 너무 느려 실용적이지 않은 경우 (파일 또는 '꼬리'까지도) 유용합니다. 더 나은 것은'std :: string :: find'와'erase' 또는'substr'을 사용하여 값을 추출하는 것입니다. –

답변

4

이 :

char * cWord = new char (word.size()+1); 

이 있어야한다 :

char * cWord = new char [word.size()+1]; 

참고 괄호.

첫 번째 바이트는 한 바이트를 할당하고 word.size()+1으로 초기화합니다. 두 번째 것은 word.size()+1 바이트를 할당합니다.

+0

그래, 그것을 고정! 감사! – user2604504