有没有一种方法可以将向量从辅助函数传递到C ++中的另一个辅助函数?

时间:2019-02-27 03:35:56

标签: c++ vector

我正在通过一种经典的C ++练习来编写程序,以确定哪些数字是质数。我现在正在使用的版本要求我能够确定哪些值是由用户输入的max所填的值。

我尝试构造的算法的行为如下:

1)输入所需的max值。

2)取这个max,然后将其放入将计算sqrt(max)的函数中。

3)使用sqrt(max),我将构造一个素数向量,其值最大为sqrt(max)

4)使用这个sqrt(max)向量,我将通过创建一个特定的函数来确定列表中最多max的值,来评估哪些值最适合值max。主要。然后,我将生成所有这些素数的列表。

有了这个结构,这就是我的努力代码:

#include "pch.h"
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using std::cin;
using std::cout;
using std::string;
using std::vector;

int determine_prime(int x) {
    // function made to determine if a number is prime
    // used the fact that to determine if number is prime only need to check if
    // prime values less than sqrt(x) divide x

    vector<int> vector_of_sqrt_primes = list_of_prime_sqrt();

    vp_1 = x % vp_1 = x % vector_of_sqrt_primes[i];

    for (int i = 0; i < vector_of_sqrt_primes.size(); i = i + 1) {
        if (vp_1 == 0 &&
            x != vector_of_sqrt_primes[i]) { // verifying if value is prime
            cout << x << " is not a prime number. \n";
            return 0;
        }

        else {
            cout << x << " is a prime number. \n";
            return 1;
        }
    }
}

int list_of_prime_sqrt(int y) {
    // using this vector as reference for all values less than the sqrt of max

    vector<int> vector_of_primes_sqrt = {2};
    int vps = 0;

    for (int i = 2; i < round(sqrt(y)); i = i + 1) {
        for (int j = 0; j < vector_of_primes_sqrt.size(); j = j + 1) {
            vps = i % vector_of_primes_sqrt[j];

            if (vps == 0 && i != vector_of_primes_sqrt[j]) {
                cout << i << " is not a prime number. \n";
            } else {
                cout << i << " is a prime number. \n";
                vector_of_primes_sqrt.push_back(i);
            }
        }
    }
}

int main() {
    int max = 0;
    vector<int> primes_list = {};

    cout << "Please enter the number of integers you would like to inspect "
            "whether they are prime.\n";
    cin >> max;

    list_of_prime_sqrt(max);

    for (int i = 1; i < max + 1; i = i + 1) {
        int p = determine_prime(i);

        if (p == 1) {
            primes_list.push_back(i);
        }
    }

    for (int j = 0; j < primes_list.size(); j = j + 1) {
        cout << primes_list[j] << "\n";
    }
}

因此,我希望能够在vector_of_sqrt_primes函数中使用determine_prime(),然后找出哪些值是素数,然后将它们返回给我的main()。但是我撞墙了。所以所有这些都问我是否有办法做到这一点?我还没到能够使用指针或任何高级功能的地步。我正在研究Stroustroup编程原理和实践,这是第4章。

1 个答案:

答案 0 :(得分:1)

以下是解决问题的两种不同方法。一个返回一个向量,另一个使用按引用传递,以便能够修改传递给参数的向量

#include <iostream>
#include <vector>
#include <string>

bool is_prime(int number){

    //exceptions
    if(number == 1) return false;
    if(number == 2 || number == 5) return true;

    std::string str = std::to_string(number);
    if(str.back() == '1' || str.back() == '3' || str.back() == '7' ||  str.back() == '9'){
        for(int i = 3; i * i <= number; i++){
            if(number % i == 0){
                return false;
            } 
        }
        return true;
    }
    return false;
}

//adds the value to the vector passed in and the values will 'save'
void find_primes(std::vector<int>& primes, int max){

    for(int i = 0; i < max; i++){
        if(is_prime(i)) primes.push_back(i);
    }
}

//adds the results to a vector and returns that vector
std::vector<int> return_vec_primes(int max){

    std::vector<int> results;
    for(int i = 0; i < max; i++){
        if(is_prime(i)) results.push_back(i);
    }

    return results;
}

int main(){

    std::vector<int> reference_vec;

    //pass the vector into the function
    find_primes(reference_vec, 100);

    //the function will return the vector into 'returned_vec'
    std::vector<int> returned_vec = return_vec_primes(100);

    //same results
    for(int i : reference_vec) std::cout << "prime: " << i << "\n";
    for(int i : returned_vec) std::cout << "prime: " << i << "\n";

    return 0;
}