有没有办法将向量从辅助函数传递到C 中的另一个辅助功能

Is there a way to pass a vector from a helper function to another helper function in C++?

本文关键字:功能 另一个 函数 向量 有没有      更新时间:2023-10-16

我正在通过编写程序的经典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";
    }
}

所以我希望我能够在determine_prime()函数中使用vector_of_sqrt_primes,然后算出哪些值是Primes a将它们返回我的main()。但是我撞墙。因此,所有这些都可以使我能够做到这一点吗?我还没有达到能够使用指针或任何先进的东西。我正在通过StrousTroup编程的原则和实践进行工作,这是第4章。

以下是解决问题的两种不同方法。一个返回向量,另一种用途通过引用通过,以便将传递到参数中的向量修改

#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;
}