Reputation: 3
I am generating a vector of 20,000 random numbers (doubles) and I would like, in addition to putting the number in this vector, called A here, to also copy all numbers between 0.0 and 0.1 into a separate vector. When I try to do this, my compiler (latest g++ version) gives me these errors:
"error: request member 'begin' in 'r0', which is of non-class type 'std::vector<double>()' for (it0=r0.begin(); it0!=r0.end();++it0)"
"error: request member 'end' in 'r0', which is of non-class type 'std::vector<double>()' for (it0=r0.begin(); it0!=r0.end();++it0)"
"error: request for member 'emplace_back' in 'r0', which is of non-class type 'std::vector<double>()' r0.emplace_back(A[i]);"
I am a relative novice in C++, but I thought that the vector data structure had methods/member functions called .begin() and .end() that help access it using iterators. It seems to me that the compiler doesn't see my vector as a vector? I'm really not sure, but below is what I did:
I have linked: iostream, cmath, fstream, time.h, cstdlib, vector, stdio.h, cstdio, iomainip, algorithm (in that order)
Here is the code I have now:
int main() {
double N=20000;
std::vector<double> A(N+1);
std::vector<double> r0();
std::vector<double>::iterator it0;
std::srand((unsigned)time(0));
for(double i = 0; i<(N+1); ++i) {
double high = 1.0, low = 0.0;
A[i]=((double) rand()/(static_cast<double>(RAND_MAX) + 1.0))*(high - low)+low;
for(it0=r0.begin(); it0!=r0.end(); ++it0) {
if(0.0<=A[i] && A[i]<0.1) { //NOTE:post-incrementing creates an unnecessary temporary iterator
rand1.emplace_back(A[i]);
count0++;
}
}
}
return 0;
}
The point of this is that I will place all of the numbers in vector A into bins of size 0.1 each, starting from 0.0, and then plot them in a histogram using gnuplot to assess the uniformity of the random numbers (generated using the pseudorandom number generator that comes with linux mint). Also, I wanted to ask what the advantages of .emplace_back() are over .push_back(). Thank you very much for your help! I really appreciate it as I am at a loss as to what I'm doing wrong.
Upvotes: 0
Views: 128
Reputation: 109119
std::vector<double> r0();
You've fallen victim to the most vexing parse (although this case is arguably not as vexing as the linked example). The line above defines a function named r0
that takes no arguments, and returns an std::vector<double>
by value. Get rid of the trailing parentheses and your code should compile.
Another oddity in your code is that you say you want to generate 20,000 random numbers, but you're generating 20,001. You don't need the N+1
you keep using, replace that by N
.
And as lizusek points out in the comments, you're pointlessly iterating over an empty vector (r0
) in the statement below:
for(it0=r0.begin(); it0!=r0.end(); ++it0) { // this loop is never entered
Finally, you should'nt be using rand()
and friends anymore, C++11 provides far superior random number generation facilities in the <random>
header. I'd rewrite your code as
std::mt19937 gen(std::random_device{}());
std::uniform_real_distribution<> dis(0, 1);
for(int i=0; i<N; ++i) {
A[i] = dis(gen);
if(A[i] > 0 && A[i] < 0.1) {
// add this to the r0 vector
r0.push_back(A[i]);
}
}
Upvotes: 2
Reputation: 29724
std::vector<double> r0();
This is parsed as a function taking no argument and returning std::vector<double>
. This is called most vexing parse error. There is also error in your loop. Together, this should read:
std::vector<double> r0;
for( int i = 0; i < N+1; ++i) {
if(0.0<=A[i] && A[i]<0.1) {
//...
}
}
Upvotes: 1