TransWikia.com

Подскажите, как можно заменить эту ошибку, что бы не переделывать код? Отсутствует оператор "[]", соответствующий этим операндам

Stack Overflow на русском Asked by John Maksimenko on November 12, 2020

#include <iostream>
#include <list>
#include <iterator>
#include <map>

using namespace std;

int main()
{
    string word;
    list<string> list;
    map<string, int> reiteration;
    map<int, int> max_min;
    while (word != "/") {
        cin >> word;
        if (word != "/")list.push_back(word);
    }
    for (unsigned int i = 0; i < list.size(); i++) {
        word = list[i];
        reiteration.insert(pair<string, int>(word, 1));
        max_min.insert(pair<int, int>(i, 1));
        auto rep = max_min.begin();
        for (auto it = reiteration.begin(); it != reiteration.end(); it++, rep++) {
            if (it->first == word) {
                it->second++;
                rep->second = i;
            }
            else {
                reiteration.insert(pair<string, int>(word, 1));
            }
        }
    }
    auto rep = max_min.begin();
    for (auto it = reiteration.begin(); it != reiteration.end(); it++) {
        cout << "Word`s : " << it->first << " repeated : " << it->second - 1 << " times" << endl;
        if (it->second - 1 > 1) {
            cout << "position 1 : " << rep->first + 1 << " position 2 : " << rep->second + 1 << endl;
            rep++;
        }
    }
    return 0;
}

2 Answers

list<string> list;

Не нужен он тебе, используй vector:

vector<string> list;

Answered by Qwertiy on November 12, 2020

for (unsigned int i = 0; i < list.size(); i++) {
    word = list[i];
unsigned int i = 0; 
for (string word : list) {
  ...
  ++i;
}

Answered by Qwertiy on November 12, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP