有没有办法将向量从辅助函数传递到C 中的另一个辅助功能
Is there a way to pass a vector from a helper function to another helper function in C++?
我正在通过编写程序的经典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;
}
相关文章:
- 有没有办法C++将给定类的功能限制为仅另一个类(不使用继承,朋友)?
- 当一个功能完成另一个功能打开时,我该怎么办?
- 为什么我要尝试在一个if语句中调用两个fucntions,但是在第一个完成后它忽略了另一个功能?(C )
- 在C 中的另一个类中调用功能
- 如何在另一个名称空间内从另一个命名空间内明确专业化功能模板
- LLVM插入功能调用到另一个函数中
- 当指针作为指向另一个功能内部指针的指针传递时会发生什么
- 是什么导致程序在我继续执行另一个功能之前停止
- 如何制作新功能并将其传递到另一个功能
- C 将用户输入从一个函数到另一个功能,并使用Main(Newbie)显示
- 有没有办法将向量从辅助函数传递到C 中的另一个辅助功能
- 如何从另一个功能中修改的结构的炭指针成员中获取值
- 对函数的未定义引用,该功能是另一个类的成员
- 是否可以将一个类别的插槽或常规功能从一个类连接到另一个类的插槽或常规功能?(QT)
- 接受元组并返回另一个元组的功能
- 如何将功能和数据从一个涡轮C 程序访问到另一个涡轮增压程序
- C 初学者可以在另一个功能中调用功能
- 将C 本地字符串返回到另一个功能
- CMAKE:使用来自另一个目标的功能,而无需直接target_link_libraries
- 使用另一个功能在另一个函数的变量