2014-12-11 4 views
2

나는 단어 목록을 작성했는지 코드를 작성하고 있습니다. 여기에는 '단어'와 '설명'이 포함됩니다. 단어와 설명에는 고유 한 벡터가 있습니다. 나는지도를 사용해서도 똑같이 노력하고있다.C++ Wordlist; 벡터 /지도에서 전체 설명 찾기

단어를 조회하려고 할 때까지 프로그램이 잘 진행 중입니다. 프로그램은 설명에서 마지막 단어 만 가져옵니다. 단일 벡터에 대해 전체 문장을 만드는 방법이 있습니까?

이것은 설명을 적어 두는 코드입니다. 전체 프로그램 코드는 정말 오랫동안 난 단지 중요한 물건을 말할 것이다, 그래서입니다 :

cout<< "Describe your word:"; //Describtion by using vectors 
cin>> desc;   //Here you enter the decribtion 
getline(cin, desc); //So you can have "space" and write a whole sentence. 
d.push_back(desc); //Place the describe at the back of the list so it is at the same index as the matching word 

을 그리고이 단어와 describtion 표시하도록되어 코드입니다 :

cout<< "Enter a word to lookup:"; 
cin>> word; 
if (find(o.begin(), o.end(), word) !=o.end()) //Lookup if word excist in vector 
{ 
    int pos = find(o.begin(), o.end(), word) - o.begin(); //Searches which index the word is in the vector 
    cout<< "Describtion for " << word << " is " << d[pos] << endl; //d[pos] takes the description vector that is in the same index as the word vector 
} 
else 
    cout<< "Word not found! Try something else." << endl; //If word not found 

것은 그것은 단지 걸릴 것이다 그 말의 마지막 단어. 지도를 사용하여 동일한 문제가 발생했습니다.

cout<< "Enter a word to lookup:"; 
cin>> word; 
if (L.find(word) != L.end()) //Lookup if the key excist 
{ 
    cout<< "Describtion for " << word << " is " << L[word] << endl; //Tells what the description is for the word if it excist 
} 
else 
    cout<< "Word not found! Try something else." << endl; //Tells you this if key is not found 

그렇다면 특정 단어에 대해 전체 설명을 인쇄하려면 어떻게해야합니까?

편집 : 나는

그래서, 무슨 일이다 (I 2보다 더 많은 단어하지 충분히 바보) 누락 년대 describtion에서 첫 번째 단어입니다 것으로 나타났습니다? 출력물의 설명 부분에 프리스트 단어를 어떻게 얻을 수 있습니까?

+1

모두 테스트 한 설명에 두 단어가 있습니까? – molbdnilo

+0

그래, 지적 해 주셔서 고마워, 첫 단어 만 건너 뛰고있는 것처럼 보입니다. – Supermixerman

+0

독서 코드의 모든 줄 사이에'cout << desc << endl;'을 추가하십시오. – molbdnilo

답변

0

std::stringstd::cin에서 추출하면 단어가 하나뿐입니다. 먼저 설명의 첫 번째 단어를 얻고 desc로를 가하고 있습니다 :

cin >> desc;   // Here you enter the decribtion 

을 그런 다음 설명에 남아있는 단어를 얻고 desc에서 그들을 가하고 있습니다.

getline(cin, desc); // So you can have "space" and write a whole sentence. 

그래서, 지금 desc 제외한 모든 설명의 첫 번째 단어가 포함 desc의 이전 내용 (첫 번째 단어를) 덮어 쓰기. 이런 종류의 것을 찾기 위해 디버거를 사용해보십시오.

기타 조언 : vector 번을 두 번 찾으십시오. find의 결과를 변수에 저장하십시오.

auto search = find(o.begin(), o.end(), word); 
if (search != o.end()) { 
    int pos = std::distance(o.begin(), search); 
    // Use pos ... 
}