저는 현재 Andrew Koenig와 Barbara Moo가 저술 한 Accelerated C++를 통해 각 장의 모든 연습을하고 C++을 배우고 있습니다.벡터가있는 요소의 발생 횟수를 카운트하십시오.
연습 3-3 : 입력란에 각 단어가 몇 번 나 왔는지 계산하는 프로그램을 작성하십시오.. 나에게이 운동은 특히 다음과 같이 고려하기가 매우 어려워 보였다. 1. 그 장의 예제와 다른 연습은 비교적 간단했다. 2. 오직 벡터 만 사용할 수 있으므로 아무 것도 진전되지 않았다. (또는 단지 내가 어려움을 잘못 판단한 것일뿐입니다)
나는이 웹에서 힌트를 찾았고이 운동에 문제가있는 다른 사람들을 보았습니다. 그러나 사람들이 제공하는 해결책은 저에게 불분명했습니다. 대부분의 사람들은이 책의 뒷부분에 소개 된 정리 방법을 사용하도록 제안했는데, 이는 운동의 요점을 무너 뜨 렸습니다.
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::setprecision;
using std::cout;
using std::string;
using std::endl;
using std::streamsize;
using std::sort;
using std::vector;
int main()
{
// Ask for string input
cout << "Please write some text, followed by end-of-file: " << endl;
vector<string> word_input;
string word;
// input words into string vector word_input
typedef vector<string>::size_type vecsize;
while (cin >> word)
{
word_input.push_back(word);
}
// sort the vector in alphabetical order to be able to separate distinct words
sort(word_input.begin(),word_input.end());
// create two vectors: one where each (string) element is a unique word, and one
// that stores the index at which a new distinc word appears
vector<string> unique_words;
vector<int> break_index;
for (int i=0; i != word_input.size()-1; ++i)
{
if(word_input[i+1] != word_input[i])
{
unique_words.push_back(word_input[i]);
break_index.push_back(i);
}
}
// add the last word in the series to the unique word string vector
unique_words.push_back(word_input[word_input.size()-1]);
// create a vector that counts how many times each unique word occurs, preallocate
// with 1's with as many times a new word occurs in the series (plus 1 to count the first word)
vector<int> word_count(1,break_index[0]+1);
// if a new word occurs, count how many times the previous word occured by subtracting the number of words so far
for(int i=0; i != break_index.size()-1;++i)
{
word_count.push_back(break_index[i+1] - break_index[i]);
}
// add the number of times the last word in the series occurs: total size of text - 1 (index starts at 0) - index at which the last word starts
word_count.push_back(word_input.size()-1-break_index[break_index.size()-1]);
// number of (distinct) words and their frequency output
cout << "The number of words in this text is: " << word_input.size() << endl;
cout << "Number of distinct words is: " << unique_words.size() << endl;
// The frequency of each word in the text
for(int i=0; i != unique_words.size(); ++i)
cout << unique_words[i] << " occurs " << word_count[i] << " time(s)" << endl;
return 0;
}
이 사용하는 벡터를 수행하는 더 좋은 방법이 있나요 : 마지막으로, 나는 다른 포럼에서 찾을 방법 내 자신의 해결책을 마련하기 위해 (여기 포함)의 힌트와 비트를 함께 재현? 루프를 결합하여 코드를보다 효율적으로 만들 수 있습니까?
[코드 검토] (http://codereview.stackexchange.com/)에 더 적합합니다. – jrok
Stackoverflow는 코드 리뷰 사이트가 아닙니다. 제 생각에는 운영자 주변의 들여 쓰기와 공간 사용이 이상하게 일치하지 않습니다.의 std :: unique를 사용하여 두 개의 반복자 사이에서 연속되지 않는 요소를 제거함으로써 단어 목록에서 중복을 제거하는 작업을 통합 할 수 있습니다. –
Wug
코드를 매우 빠르게 스캔하면 꽤 많은 로직을 보냈습니다. 이것 때문에. 그것은 컨테이너 (목록/벡터), 모든 단어를 반복하는 루프 및 현재 컨테이너의'if (std :: find())'이어야합니다. 단어가 이미 포함되어있는 경우 (그렇지 않은 경우 삽입하십시오. 소원). 총 단어 수는 컨테이너를 통해 쉽게 검색 할 수 있습니다.size()와 자체적으로 저장해서는 안됩니다. (많이 쓰지 않고, 그냥 necc가 아닙니다) – Najzero