Using 'random_sample'...

D

deancoo

I'm just a tad frustrated right now, because I need to ask such a n00b
question. I'm trying to use 'random_sample', however, I'm getting back a
compiler error saying; " `random_sample' undeclared (first use this
function) ". I've looked a my C++ header file 'algorithm', and its
definition seems to be there. When I include the old style, algo.h, I only
get a warning, but compilation is successful. Should I just use algo.h,
because the SGI STL documentation says it should exist in header
'algorithm'. What am I doing wrong?!

d
 
A

Artie Gold

deancoo said:
I'm just a tad frustrated right now, because I need to ask such a n00b
question. I'm trying to use 'random_sample', however, I'm getting back a
compiler error saying; " `random_sample' undeclared (first use this
function) ". I've looked a my C++ header file 'algorithm', and its
definition seems to be there. When I include the old style, algo.h, I only
get a warning, but compilation is successful. Should I just use algo.h,
because the SGI STL documentation says it should exist in header
'algorithm'. What am I doing wrong?!

d
`random_sample' is an SGI extension, not part of the standard C++
library -- but using `std::random_sample' is likely to work.

HTH,
--ag
 
D

deancoo

Artie Gold said:
`random_sample' is an SGI extension, not part of the standard C++
library -- but using `std::random_sample' is likely to work.
Hmmm, still not working.
 
R

Raghu Uppalli

What compiler are you using. Early version of gcc3.. had non standard
STL headers in a separate directory under the __gnu_cxx namespace. Is
that the case with you?
 
R

Raghu Uppalli

My earlier comment may be misleading. Newer versions of gcc/g++ also
have non-standard includes in ext/.

I am assuming you mean gcc-3.4.2. The following works for me...on gcc
3.4.2

#include <ext/algorithm>
#include <iostream>
#include <vector>
using namespace std;
using namespace __gnu_cxx;

int main()
{
const int a = 5;
const int b = 2;
int A[] = {1, 2, 3, 4, 5};
int B;
for ( int cnt = 0; cnt < 10; ++cnt) {
random_sample(A, A+a, B, B+b);
cout << "Turn " << cnt << ": " ;
for (int i =0; i < b; ++i) {
cout << B << " ";
}
cout << endl;
}
}
 
D

deancoo

Raghu Uppalli said:
My earlier comment may be misleading. Newer versions of gcc/g++ also
have non-standard includes in ext/.

I am assuming you mean gcc-3.4.2. The following works for me...on gcc
3.4.2

#include <ext/algorithm>
#include <iostream>
#include <vector>
using namespace std;
using namespace __gnu_cxx;

int main()
{
const int a = 5;
const int b = 2;
int A[] = {1, 2, 3, 4, 5};
int B;
for ( int cnt = 0; cnt < 10; ++cnt) {
random_sample(A, A+a, B, B+b);
cout << "Turn " << cnt << ": " ;
for (int i =0; i < b; ++i) {
cout << B << " ";
}
cout << endl;
}
}


Yup, that was the problem. I wasn't giving the extra bit of path
information in the include statement for the compiler to find the
non-standard algorithm header file.

Thanks a lot dude! Now I don't have to get that darn warning each time I
compile.

Cheers,
d
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top