[C++] stl, klasa vector

0

jak umozliwic realizacje w kodzie takiej linijki:

vecd = veci;

jesli vecd to kontener vector<double> a veci to vetor<int>

Mam problem bo nie mozna przeciazyc operatora= jako statycznej funkcji

z gory dzieki za info

0

Napisz sobie klasę opakowującą (Wrapper) dla tego wektora dla <double>. W ten sposób w tej klasie przeciążysz sobie metodę.

0

no ale nie moge stworzyc klasy typu vector<double>

ewentualnie szablon vector<T>, o to chodzi?

0

Nie. Możesz stworzyc klasę która będzie agregować w sobie interesujący cię wektor.

0
 

 vector<int> veci;
    vector<double> vecd;

    for (int i = 0; i < 10; ++i)
    {
       veci.push_back(i);
       //vecd.push_back(i);
    }
    vecd = veci;

    cout << veci << endl << vecd << endl;

ale main musi koniecznie wygladac tak jak powyzej, a n ie moge napisac czegos takiego:

class vector<double> { ... std::vector<double> a ... }

czyzby konieczne dla tego maina bylo napisanie od poczatku wlasnej implementacji kontenera vector ?

0

Ale czemu main musi niby tak wyglądać? Tak czy siak nadal mozesz zrobić dokładnie to co napisałem, przy czym będziesz musiał zrobić mały przekręt z nazwami żebyś mógł jednocześnie stworzyc klasę o nazwie vector (co uważam za terroryzm, bo to bardzo mylące nadawać własnej klasie taką samą nazwę jak klasy z std::) i używać zwykłego vector.

0

przygotowuje sie do egzaminu - a to jeden z poprzednich lat (jedno z zadan) :)

ale jaki przekret ?
class jakas { std::vector<double> a ...}

typedef jakas vector<double>

?

0

Bój ty sie Boga ;]
Wystarczy umiejętne wykorzystanie std:: i brak usinga.

0

Chyba nie rozumiesz o co chodzi.

class MyDoubleVector : public std::vector<double> {
public:
  MyDoubleVector() : std::vector<double>() {}
  MyDoubleVector(const std::vector<double>& vec) : std::vector<double>(vec) {}
  MyDoubleVector(const std::vector<int>& vec) : std::vector<double>() {
    std::copy(vec.begin(), vec.end(), begin, end());
  }
}
0

no tak, mozna sie pozbyc std poprzez using std::vector;

ale jaki ma to zwizek z moim problemem, ja nie rozumie jak nazwac szkielet tej klasy opakowujacej vector<double> ? :)

0

mozna dziedziczyc po stly ? (cos mi sie obilo o uszy ze nie - jak to jest ?)

ale nazwa tej klasy wumusza modyfikacje maina, czego nie wolno robic...

0

Chodziło mi raczej o takie coś:

template <typename T>
class vector
{
private:
    std::vector<T> dane;
public:
    vector<T>() {}
    vector<T>(const std::vector<T>& vec) : dane(vec) {}
    vector<T>(const std::vector<int>& vec)
    {
        std::copy(vec.begin(), vec.end(), dane.begin(), dane.end());
    }
    vector<T>& operator=(const std::vector<int>& vec)
    {
        dane.clear();
        for(unsigned i=0; i<vec.size(); i++)
            dane.push_back((T)vec[i]);
        return *this;
    }
};
0
#include<iostream>
//using namespace std;

#include <vector>
#include <algorithm>
using std::cout;
using std::endl;


template <typename T>
class vector
{
private:
    std::vector<T> dane;
public:
    vector<T>() {}
    vector<T>(const std::vector<T>& vec) : dane(vec) {}
     void push_back(T & k)  //dla push back z peti for
    {
        dane.push_back(k);
    }
    vector<T>(const std::vector<int>& vec)
    {
        std::copy(vec.begin(), vec.end(), dane.begin(), dane.end());
    }
    vector<T>& operator=(const std::vector<int>& vec)
    {
        dane.clear();
        for(unsigned i=0; i<vec.size(); i++)
            dane.push_back((T)vec[i]);
        return *this;
    }
};

int main()
{
    vector<int> veci;
    vector<double> vecd;

    for (int i = 0; i < 10; ++i)
    {
        veci.push_back(i);
         vecd.push_back(i);
    }




    vecd = veci;

    cout << veci << endl << vecd << endl;

}

 

Twoj kod sie jakos sypie

main.cpp:124:   instantiated from here
main.cpp:109: error: ‘vector<T>::vector(const std::vector<int, std::allocator<int> >&) [with T = int]’ cannot be overloaded
main.cpp:104: error: with ‘vector<T>::vector(const std::vector<T, std::allocator<_CharT> >&) [with T = int]’
main.cpp: In function ‘int main()’:
main.cpp:130: error: no matching function for call to ‘vector<double>::push_back(int&)’
main.cpp:105: note: candidates are: void vector<T>::push_back(T&) [with T = double]
main.cpp:136: error: no match for ‘operator=’ in ‘vecd = veci’
main.cpp:113: note: candidates are: vector<T>& vector<T>::operator=(const std::vector<int, std::allocator<int> >&) [with T = double]
main.cpp:99: note:                 vector<double>& vector<double>::operator=(const vector<double>&)
main.cpp:138: error: no match for ‘operator<<’ in ‘std::cout << veci’
/usr/include/c++/4.4/ostream:108: note: candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>& (*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:117: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:127: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:165: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:169: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:173: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/bits/ostream.tcc:91: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:180: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/bits/ostream.tcc:105: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:191: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:200: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:204: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:209: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:213: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:221: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/ostream:225: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/bits/ostream.tcc:119: note:                 std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]


 

napisalem na szybko wlasna wersje kontenera i dziala ok (ale dalej jestem zdania ze docelowo chodzilo o wykorzystanie stl):

#include<iostream>
using namespace std;

template <typename T>
class vector
{
public:



    vector() : pojemnosc(10), rozmiar(0), tab(new T[10])  //przydalby sie jeszcze realloc na pozniej, ale dla tego main ok
    {
    };

    friend ostream & operator <<(ostream& o, const vector<T>& nazwa)
    {
        for (int i = 0; i < nazwa.rozmiar; ++i)
            o << nazwa.tab[i] << " ";
        return o;

    };
    void push_back(T wartosc);
    void operator =(const vector<int>& nazwa){

   delete [] tab;
    T* temp;
    temp = new T[nazwa.pojemnosc];
    for (int i = 0; i < nazwa.rozmiar; ++i)
        temp[i] = nazwa.tab[i];
    tab = temp;
};
    
 

    
    T* tab;

    int rozmiar;
    int pojemnosc;
};




template<typename T> void vector<T>::push_back(T wartosc)
{
    
        tab[rozmiar] = wartosc;
        rozmiar++;
};

int main()
{
    vector<int> veci;
    vector<double> vecd;

    for (int i = 0; i < 10; ++i)
    {
        veci.push_back(i);
         vecd.push_back(i);
    }

    
       
   
    vecd = veci;

    cout << veci << endl << vecd << endl;

}
 
0

a tak btw. wczesniej tez pisalem szablon vector z zawieraniem vectora z stla (podobnie jak Ty) ale bez przerwy mi sie sypalo...

normalnie rzadka sytuacja, ze latwiej dla tego maina od nowa napisac kontener niz z gotowego skorzystac ;)

0

Sypie się z prostego powodu. Bo jest problem

  • z nazwami
  • z operatorem<<
    O ile operator zawsze można dopisac, o tyle umiejętne wykorzystanie nazw już takie oczywiste nie musi być.
0

No to musisz zaimplementowac swoja klase typu wektor. Te main, ktore podales wczle nie musi sugerowac, ze uzywasz naglowka #inlcude <vector>, szczegolnie, ze nie masz using namespace std; To moze byc zwykla zmylka. Poznie musisz dodac cos takiego

template<typename T>
ostream& operator << (ostream& out, const vector<T>& in)
{
std::copy(in.begin(), in.end(), std::ostream_iterator<T>(out, "\t"));
}

do wyrzucania i po robocie

Pozdrawiam serdecznie

0

no tak, zapomnialem dodac operatora<<, ale to nie problem, sypie sie przy definicji wektora w main oraz przy metodzie push_back (sugerujesz jakis problem z nazwami - ja tego nie widze :) )

tak, czy siak, zostane chyba przy wlasnej implementacji vector na tablicy (jakby cos podobnego sie na egz pojawilo), ale mimo wszystko korci mnie troche jak to zrobic z zawieraniem konteneru z stl...

0

Problem jest dość oczywisty -> musisz zaimplementować metodę push_back ;]

0
   
void push_back(T & k)  //dla push back z peti for
{
        dane.push_back(k);
}
 

czyli ta powyzej ktora dokleilem do Twojego kodu jest zla ?

0

jeśli wymagane jest tylko żeby program się skompilował to możesz zrobić coś takiego :) :

#include <iostream>
#include <vector>

using std::cout;
using std::endl;

int main()
{
  cout << "qwerty" << endl;
  return 0;
}

template<class T>
struct vector
{
  void push_back(int a) { n=a; }
  operator T() { return n; }
  template <class lol>
  void operator= (lol a) { n=a; }
  T n;
};

#define main xmain

int main()
{
    vector<int> veci;
    vector<double> vecd;

    for (int i = 0; i < 10; ++i)
    {
        veci.push_back(i);
         vecd.push_back(i);
    }


    vecd = veci;

    cout << veci << endl << vecd << endl;
}

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