Answer the question
In order to leave comments, you need to log in
How to create an object with std::initializer_list?
Hello everyone
I have a class with the following constructors
public:
std::vector<T> v;
SearchVektor(){};
SearchVektor(const initializer_list<T> & t ) {
for( auto a : t) {
v.push_back(a);
}
};
SearchVektor<unsigned int> search ( T num) {
vector<int> liste; // -------- search result
int begin = 0;
int mitt = v.size()/2;
int end = v.size();
thread t1(Work(begin, mitt, num, std::ref(liste))); // тут будет liste заполнено
thread t2(Work(mitt+1, end, num, std::ref(liste)));
t1.join();
t2.join();
SearchVektor<T> t(liste) ;
return t; ; ----> Моя цель
}
Answer the question
In order to leave comments, you need to log in
This is not possible, initializer_list is a thin layer that allows you to initialize a vector from a linear array. She doesn't need anything else.
You have some oddity - SearchVektor<unsigned int>
we initialize from a vector vector<int>
. It is generally not written in any gate. For this case, you need to...
1. vector<int> liste
turn it into vector<unsigned int>
.
2. Write a constructor that works from a temporary vector: SearchVektor(std::vector<T> && t )
. Such a constructor will be very fast, because it takes all the information from t, leaving a void in it.
3. Declaring liste temporary -
return SearchVektor< unsigned int> (std::move(liste));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question