blad przy pushowaniu do vectora

0

1 linia pliku to liczba ktora przechowywana jest przez zmienna dataSets, 2 linia pliku(zmienna size1) oznacza liczbe elementow w linii 3(tablica A1),natomiast 4 linia(zmienna size2) liczbe elementow w linii 5(tablica A2). Linie od 2-5 wlacznie sa nadpisywane w petli w funkcji readFile, oraz dla elementow z tablic A2 i A1 funkcja countElementsInArray zwraca ilosc wystapien kolejnych elementow tablicy A2 w tablicy A1. Moj program staje kiedy pushuje wyniki do vectora o nazwie counts. To zadne zadanie ze spoja itp. Prosze o pomoc :)
txt:

3
4
-5 -1 0 8
7
7 9 2 0 -7 2 -5
4
1 2 3 4
2
1 1
5
0 0 0 0 0
1
3

Result for above input should be:
Dataset1:
0 0 0 1 0 0 1
Dataset2:
1 1
Dataset3:
0
int main() {

    int dataSets = 0, size1 = 10, size2 = 10;
    std::fstream file;
    openFile(file);

    int *A1 = new int[size1];
    int *A2 = new int[size2];

    readFile(file, dataSets, size1, size2, A1, A2);
}

void readFile(std::fstream &file, int &dataSets, int &size1, int &size2, int *&A1, int *&A2) {

    std::vector<int> counts;

    file >> dataSets;

    for (size_t i = 0; i < dataSets; i++) {
        file >> size1;

        for (size_t j = 0; j < size1; j++)
            file >> A1[j];

        file >> size2;

        for (size_t j = 0; j < size2; j++)
            file >> A2[j];

        for (size_t j = 0; j < size2; j++) {
            int searchValue = A2[j];
            counts.push_back(countElementsInArray(A1, size1, searchValue));
        }

        int numberOfDataSet = i + 1;
        std::cout << "Dataset" << numberOfDataSet << ":" << std::endl;
        for (size_t j = 0; j < size2; j++) {
            std::cout << counts[j] << " ";
        }

        std::cout << std::endl;
    }

}

int countElementsInArray(int *A, int size, int searchValue) {

    int count = 0, first = 0, last = size;
    int i = 1;
    int j = 1;
    while (first <= last) {
        int mid = first + (last - 1) / 2;

        if (A[mid] == searchValue) {
            count++;
            do {
                if (A[mid + i] == searchValue && A[mid - j] == searchValue) {
                    count += 2;
                    i++;
                    j++;
                } else if (A[mid + i] == searchValue) {
                    count++;
                    i++;
                } else if (A[mid - j] == searchValue) {
                    count++;
                    j++;
                }

                return count;
            } while (A[mid + i] == searchValue || A[mid - j] == searchValue);
        } else if (searchValue < A[mid])
            last = mid - 1;

        else if (searchValue > A[mid])
            first = mid + 1;
    }
    return count;
}
0

Proszę to wyjaśnić:

eee.cc: In function ‘int main()’:
eee.cc:4:10: error: ‘fstream’ is not a member of ‘std’
    4 |     std::fstream file;
      |          ^~~~~~~
eee.cc:1:1: note: ‘std::fstream’ is defined in header ‘<fstream>’; did you forget to ‘#include <fstream>’?
  +++ |+#include <fstream>
    1 | int main() {
eee.cc:5:14: error: ‘file’ was not declared in this scope
    5 |     openFile(file);
      |              ^~~~
eee.cc:5:5: error: ‘openFile’ was not declared in this scope
    5 |     openFile(file);
      |     ^~~~~~~~
eee.cc:10:5: error: ‘readFile’ was not declared in this scope
   10 |     readFile(file, dataSets, size1, size2, A1, A2);
      |     ^~~~~~~~
eee.cc: At global scope:
eee.cc:13:20: error: variable or field ‘readFile’ declared void
   13 | void readFile(std::fstream &file, int &dataSets, int &size1, int &size2, int *&A1, int *&A2) {
      |                    ^~~~~~~
eee.cc:13:20: error: ‘fstream’ is not a member of ‘std’
eee.cc:13:20: note: ‘std::fstream’ is defined in header ‘<fstream>’; did you forget to ‘#include <fstream>’?
eee.cc:13:29: error: ‘file’ was not declared in this scope
   13 | void readFile(std::fstream &file, int &dataSets, int &size1, int &size2, int *&A1, int *&A2) {
      |                             ^~~~
eee.cc:13:35: error: expected primary-expression before ‘int’
   13 | void readFile(std::fstream &file, int &dataSets, int &size1, int &size2, int *&A1, int *&A2) {
      |                                   ^~~
eee.cc:13:50: error: expected primary-expression before ‘int’
   13 | void readFile(std::fstream &file, int &dataSets, int &size1, int &size2, int *&A1, int *&A2) {
      |                                                  ^~~
eee.cc:13:62: error: expected primary-expression before ‘int’
   13 | void readFile(std::fstream &file, int &dataSets, int &size1, int &size2, int *&A1, int *&A2) {
      |                                                              ^~~
eee.cc:13:74: error: expected primary-expression before ‘int’
   13 | void readFile(std::fstream &file, int &dataSets, int &size1, int &size2, int *&A1, int *&A2) {
      |                                                                          ^~~
eee.cc:13:84: error: expected primary-expression before ‘int’
   13 | void readFile(std::fstream &file, int &dataSets, int &size1, int &size2, int *&A1, int *&A2) {
      |                                                                                    ^~~
4
    int *A1 = new int[size1];
    int *A2 = new int[size2];

Po co to? Użyj np. vectora...

"staje" na pushu bo zapewne dla jakiegoś przypadku pętla w funkcji count jest nieskończona.

Jak to zadanie ze spoja to pokaż co to za zadanie. Nie wiedząc co masz tam zrobić i mając za wskazówkę wyłącznie niedziałającą próbę rozwiązania, domyślam się, że chcesz policzyć elementy w zbiorze, tutaj warto się zainteresować:

  • posortowanym vectorem/set/multiset
  • funkcjami upper_bound/lower_bound
2

Do SPOJ potrzebujesz tyle do odczytu:

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

int main()
{
   istream_iterator<int> iit(cin);
   for(int t=*iit;t--;)
   {
      vector<int> a(*(++iit));
      copy_n(++iit,a.size(),begin(a));
      vector<int> b(*(++iit));
      copy_n(++iit,b.size(),begin(b));

      // dalej samo wyświetlenie
      ostream_iterator<int> oit(cout,", ");
      cout<<a.size()<<": ";
      copy_n(begin(a),a.size(),oit);
      cout<<"; ";
      cout<<b.size()<<": ";
      copy_n(begin(b),b.size(),oit);
      cout<<";"<<endl;
   }
   return 0;
}
0

Napisałem że to nie jest zadanie ze spoja.

2
micw napisał(a):

To zadne zadanie ze spoja itp. Prosze o pomoc :)

0

@kq: zmieniłem troche kod, uzylem vectorow ale dalej mam problem z wyswietleniem vectora counts. W debuggerze widze ze elementy sie dodaja do tego vectora

void fread(std::fstream &file, std::vector<int> &A1, std::vector<int> &A2) {

    std::vector<int> counts;
    int dataSets, size1, size2, x, y;
    file >> dataSets;
    for (size_t i = 0; i < dataSets; i++) {
        file >> size1;

        for (size_t j = 0; j < size1; j++) {
            file >> x;
            A1.push_back(x);
        }

        file >> size2;

        for (size_t j = 0; j < size2; j++) {
            file >> y;
            A2.push_back(y);
        }

        for (size_t j = 0; j < size2; j++) {
            int searchValue = A2[j];
            counts.push_back(fcount(A1, size1, searchValue));
        }

        int numberOfDataSet = i + 1;
        std::cout << "Dataset" << numberOfDataSet << ":" << std::endl;
        for(auto& k : counts)
            std::cout << k;
        std::cout << std::endl;
    }
}
1

Przyjmując, że poprawnie wczytałeś wektory do pamięci oraz że dobrze zrozumiałem zadanie to można to rozwiązać w ten sposób.

std::string searchDuplicate(std::vector<int> &first_line,std::vector<int> &second_line){
    std::string output;
    std::stringstream ss(output);

    for(int second_item : second_line){
        int counter = 0;
        for(int first_item : first_line)
            if(first_item == second_item)
                ++counter;
        ss << counter << " ";
    }
    return ss.str();
}

Powinno obliczać ilość powtórzeń pomiędzy wektorami. Funkcja daje output w formie stringa. Nie ma przeszkód żeby zastosować vector zamiast stringa.
Można bardziej elegancko tak jak napisał @kq lecz takie podejście może być bardziej zrozumiałe dla Ciebie.

1 użytkowników online, w tym zalogowanych: 0, gości: 1