Reputation: 353
I'm trying to generate random numbers either of type int or double and then insert them int a type of container vector, list into a sorted order but the problem is that insert is not working, and I don't know why, so can you help me please?
#include <iostream>
#include <iterator>
#include <random>
#include <vector>
#include<list>
#include<deque>
#include <algorithm>
#include <chrono>
using namespace std;
int random_gen(){
default_random_engine re { std::random_device()() };
uniform_int_distribution<int> dist;
auto r= bind(dist,re);
int x =r();
return x;
}
template<typename SequenceContainer>
void build_cont(const SequenceContainer& seq, int n)
{
for(int i=0; i!=n; ++i) {
int gen = random_gen();
//std::iterator<T> it=lower_bound(begin(seq), end(seq), gen);
typename SequenceContainer::iterator it;
it=find_if(seq.begin(), seq.end(), [it](){ return *it<gen;});
seq.insert(it, gen);
}
for(auto i:seq)
cout<<i<<endl;
}
int main() {
int n=10;
vector<int> v;
list<int>ls;
deque<int> deq;
build_cont(v, n);
build_cont(ls, n);
build_cont(deq, n);
return 0;
}
the error I'm getting right now is a linker error, so I don't know what is wrong??
Upvotes: 0
Views: 179
Reputation: 326
There are two issues:
You can't take a const reference to SequenceContainer then try to obtain a non-const iterator or insert items into that const reference. Remove the const.
You need to include 'gen' in the lambda capture clause, and it must take the value as a parameter:
typename SequenceContainer::iterator it = find_if(
seq.begin(),
seq.end(),
[gen](const int& val) { return gen < val; }
);
Upvotes: 4