Witam,
Mam mały problem z moim programem. Mianowicie w klasie Joiner w konstruktorze przesyłam dwa kontenery, ale niestety przesyłam ich kopię, a nie oryginały.
Czy mógłby mi ktoś pomóc jak przesłać oryginalne zmienne, tak żeby funkcje:
*JoinAtBegin() (dodaje na początek kontener który jest podany jako drugi w konstruktorze do tego pierwszego)
*JoinAtEnd() (dodaje na koniec kontener który jest podany jako drugi w konstruktorze do tego pierwszego)
zmieniały kontenery z funkcji main() , a nie te wewnątrz klasy? Poniżej zamieszczam kod:
#include <iostream>
#include<vector>
#include <list>
#include <deque>
#include <random>
#include <chrono>
#include <iterator>
#include <utility>
using Vector = std::vector<double>;
using List = std::list<double>;
template <typename T, typename L>
class Joiner
{
public:
Joiner(T& container1, L& container2)
{
container1_ = container1;
container2_ = container2;
}
void joinAtBegin()
{
auto it = container1_.begin();
auto it2 = container2_.begin();
container1_.insert(it, it2, it2 = container2_.end());
}
void joinAtEnd()
{
auto it = container1_.end();
auto it2 = container2_.begin();
container1_.insert(it, it2, it2 = container2_.end());
}
void showResults()
{
std::cout << "kontener 1 z klasy Joiner: " << container1_.size() << std::endl;
std::cout << "kontener 2 z klasy Joiner: " << container2_.size() << std::endl;
}
private:
T container1_;
L container2_;
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
Vector v1(1000,1.0),v2(1000,2.0);
List l1(1000,3.0),l2(1000,4.0);
Joiner <Vector, Vector > joiner1{v1, v2};
Joiner <List, List > joiner2{l1, l2};
Joiner <Vector, List > mixedJoiner{v1, l1};
std::cout << "joiner1: " << std::endl;
std::cout << "v1 z funkcji main przed: " << v1.size() << std::endl;
std::cout << "v2 z funkcji main przed: " << v2.size() << std::endl;
joiner1.joinAtBegin();
joiner1.joinAtEnd();
joiner1.showResults();
std::cout << "v1 z funkcji main po: " << v1.size() << std::endl;
std::cout << "v2 z funkcji main po: " << v2.size() << std::endl << std::endl;
std::cout << "joiner2: " << std::endl;
std::cout << "l1 z funkcji main przed: " << l1.size() << std::endl;
std::cout << "l2 z funkcji main przed: " << l2.size() << std::endl;
joiner2.joinAtBegin();
joiner2.joinAtEnd();
joiner2.showResults();
std::cout << "l1 z funkcji main po: " << v1.size() << std::endl;
std::cout << "l2 z funkcji main po: " << v2.size() << std::endl << std::endl;
std::cout << "mixedjoiner: " << std::endl;
std::cout << "l1 z funkcji main przed: " << l1.size() << std::endl;
std::cout << "l2 z funkcji main przed: " << l2.size() << std::endl;
mixedJoiner.joinAtBegin();
mixedJoiner.joinAtEnd();
mixedJoiner.showResults();
std::cout << "l1 z funkcji main przed: " << l1.size() << std::endl;
std::cout << "l2 z funkcji main przed: " << l2.size() << std::endl;
return 0;
}
po wywołaniu programu otrzymuję taki wynik (zdjęcie w załączniku)
Czyli w funkcjach JoinAtBegin() i JoinAtEnd() wielkość wektorów się zwiększa, ale kontenery z funkcji main pozostają bez zmiany(nadal mają 1000 elementów), a chcę żeby właśnie te kontenery z maina się zmieniały.
Z góry dziękuję za odpowiedź. Pozdrawiam