2015-09-07 6 views
-3

Ошибки:Максимальное количество дублей

Описание Путь информационного ресурса Тип не может преобразовать '__gnu_cxx :: __ alloc_traits>> :: value_type {ака станд :: basic_string}' на 'Int' в уступке duplicatedWords.cpp/duplicatedWords/src строка 42 C/C++ Проблема не может преобразовать '__gnu_cxx :: __ alloc_traits>> :: value_type {aka std :: basic_string}' в 'int' в инициализации duplicatedWords.cpp/duplicatedWords/src line 37 C/C++ Задача не может convert 'std :: basic_string' в 'int' в инициализации duplicatedWords.cpp/duplicatedWords/src line 40 C/C++ Задача Нет совпадения для 'operator>' (типы операндов: '__gnu_cxx :: __ alloc_traits>> :: value_type {aka std :: basic_string} 'и' INT ') duplicatedWords.cpp/duplicatedWords/ЦСИ строка 41 C/C++ Проблема

/* 
Write a program to read strings from standard input 
looking for duplicated words. The program should find places in the input 
where one word is followed immediately by itself. Keep track of the largest 
number of times a single repetition occurs and which word is repeated. Print 
the maximum number of duplicates, or else print a message saying that no 
word was repeated. For example, if the input is 

how now now now brown cow cow 

the output should indicate that the word now occurred three times. 
*/ 

#include <iostream> 
#include <vector> 
using namespace std; 

int main() { 
cout << "Please enter your words " << endl; 
    string words; 
    vector<string> vec; 

    while(cin>>words && words != "qu"){ 
     vec.push_back(words); 

    } 

    int max = vec[0]; 
    int result = 0; 

    for(int i: vec){ 
     if(vec[i] > max){ 
     max = vec[i]; 
     result = i; 
     } 
    } 

    cout<<result<<endl; 
return 0; 

}

+0

Я теряюсь о том, как я могу найти максимальный дублируют – TheChessDoctor

+1

Во-первых, у вас есть строка вектор и вы делаете некоторые струнные вещи с ним. ОК. Затем, внезапно, остальная часть кода похожа на вектор, содержащий целое число, которое может быть присвоено 'int' и по сравнению с'> '. Что делаешь?! – deviantfan

+0

Что произойдет, если пользователь введет 'qu' в качестве первого входа? Обращайтесь с этим делом изящно. –

ответ

1

Если вход сортируется, который я думаю, что ваша программа предполагает, то следующие модификации сделано в код ниже будет работать. Однако, если вход несортирован, вы должны использовать хеш-таблицы или отсортировать входные данные.

#include <iostream> 
#include <vector> 
using namespace std; 

int main() { 
    int max = 0; 
    int result = 0; 
    unsigned int rep_count = 1; 
    string words; 
    vector<string> vec; 

    cout << "Please enter your words " << endl; 
    while(cin>>words && words != "qu"){ 
     vec.push_back(words); 
    } 

    if (!vec.size()) 
     return 0; 

    for(unsigned int i = 1; i < vec.size(); i++){ 
     if (vec[i] == vec[i-1]) { 
      rep_count++; 
     } else { 
      rep_count = 1; 
     } 
     if (rep_count > max) { 
      result = i; 
      max = rep_count; 
     } 
    } 
    cout<<"Word = "<<vec[result]<<", Repitition = "<<max<<endl; 
    return 0; 
} 
Смежные вопросы