2012-06-15 1 views
0

나는지도에서지도에 역 색인을 만들려고 해요 .At 순간이 코드가 있습니다지도에서 역 색인을 생성하여 C++로 매핑하는 방법은 무엇입니까?

int main() 
{ 

    char lineBuffer[200]; 
    typedef std::map<std::string, int> MapType; 
    std::ifstream archiveInputStream("./hola"); 

    // map words to their text-frequency 
    std::map<std::string, int> wordcounts; 

    // read the whole archive... 
    while (!archiveInputStream.eof()) 
    { 
     //... line by line 
     archiveInputStream.getline(lineBuffer, sizeof(lineBuffer)); 

     char* currentToken = strtok(lineBuffer, " "); 

     // if there's a token... 
     while (currentToken != NULL) 
     { 
      // ... check if there's already an element in wordcounts to be updated ... 
      MapType::iterator iter = wordcounts.find(currentToken); 
      if (iter != wordcounts.end()) 
      { 
       // ... then update wordcount 
       ++wordcounts[currentToken]; 
      } 
      else 
      { 
       // ... or begin with a new wordcount 
       wordcounts.insert(
         std::pair<std::string, int>(currentToken, 1)); 
      } 
      currentToken = strtok(NULL, " "); // continue with next token 
     } 

     // display the content 
     for (MapType::const_iterator it = wordcounts.begin(); it != wordcounts.end(); 
       ++it) 
     { 
      std::cout << "Who(key = first): " << it->first; 
      std::cout << " Score(value = second): " << it->second << '\n'; 
     } 
    } 
} 

이 문제에 관하여를 내가 생각하지 않은, 내가 맵 구조를 사용하여 초보자이기 때문에.

귀하의 도움에 매우 감사드립니다.

+2

더 구체적으로하시기 바랍니다 당신이 실제로 필요한 도움에 대해, 그렇지 않으면 – xmoex

+0

도와 주셔서 감사합니다. 난이 코드의지도에서지도를 사용하여 거꾸로 색인을 만드는 데 도움이 필요합니다. 그럼 출력이 같은 주파수로 단어를 생성해야합니다 . –

+0

''freqm [42]'가'42 '번 발생하는 단어를 얻을 수 있도록 주파수로 색인 된'map'을 생성하려고합니까? – dirkgently

답변

1

나는 (A histogram 유사)과 같이,이 인덱스에 의해, 같은 단어 수-인덱스 string의 색인 목록을 두 번째지도를 작성하는 것입니다 도움이 될 어떻게 생각 :

std::map<int, std::list<std::string> > inverted;

그렇게 할 때 당신은 당신이 수동으로이 같은 역 색인에 모든 string를 삽입해야 -map wordcounts 만드는 완료 (주의를이 코드는 안된!) :

// wordcounts to inverted index 
for (std::map<std::string, int>::iterator it = wordcounts.begin(); 
     it != wordcounts.end(); ++it) 
{ 
    int wordcountOfString = it->second; 
    std::string currentString = it->first; 

    std::map<int, std::list<std::string> >::iterator invertedIt = 
      inverted.find(wordcountOfString); 
    if (invertedIt == inverted.end()) 
    { 
     // insert new list 
     std::list<std::string> newList; 
     newList.push_back(currentString); 
     inverted.insert(
       std::make_pair<int, std::list<std::string>>(
         wordcountOfString, newList)); 
    } 
    else 
    { 
     // update existing list 
     std::list<std::string>& existingList = invertedIt->second; 
     existingList.push_back(currentString); 
    } 

} 
+0

xmoex, 귀하의 유용한 정보와 도움에 감사드립니다. –

+0

본인은이 커뮤니티에 새로운 회원입니다. 당신의 대답을 어떻게 수용 할 수 있습니까? –

+0

@Christian이 답변의 그림을보십시오. http://meta.stackexchange.com/a/5235 – anatolyg

관련 문제