In int main(), I am getting the error message "stoi was not declared in this scope." Is there a way around using stoi? Thanks!
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <ctime>
#include <cstdlib>
#include <iterator>
using namespace std;
vector<int> vec(1);
class isprime
{
public:
isprime();
bool operator() (int);
private:
int num;
};
isprime::isprime()
{
vec.push_back(2);
}
bool isprime::operator()(int num)
{
//sort the vector so that we can apply binary search
sort(vec.begin(), vec.end());
//use binary search to get iterator
bool ans = binary_search(vec.begin(), vec.end(), num);
if (ans) {
return true;
}
if (num < 2) {
return false;
}
for (int i = 2; i <= sqrt(num); i++) {
if (num%i == 0) {
return false;
}
}
vec.push_back(num);
return true;
}
struct random{
int operator()() {
int num = 0;
//this will return number between 0 and 99 both inclusive
num = rand() % 100;
if (num == 0)
num = 100;
return num;
}
}GetRandom;
int main(int argc, char *argv[]) {
int N = 10, i;
isprime pcheck;
//check if commandline argments have been provided
if (argc >= 2) {
N = stoi(argv[1]);
}
//set seeding to generate random numbers
srand(time(0));
//declare vectors of size N
vector<int> vec_random(N);
vector<bool> result(N);
//push numbers in a vector
generate(vec_random.begin(), vec_random.end(), GetRandom);
//use transform function to generate a boolean vector indicating whether a number is prime or not
transform(vec_random.begin(), vec_random.end(), result.begin(), pcheck);
cout << "Sequence contains " << count(result.begin(), result.end(), 1) << " prime numbers." << endl;
return 0;
}
I'm going to guess you're on MinGW. Upgrade your compiler instead, newest versions of MinGW should have this fixed.
PS: You never set num inside your isprime class to anything. I would just delete the variable.