我正在编写一个程序来确定一系列数字的素因子。在该计划中,该范围的startingNumber
和endingNumber
表示为std::pair
。
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const long long MAX = 100000000; // One hundred million
long long factor[MAX];
pair<const long long, const long long> get_pair() // Gets user start and end number to establish range in vector.
{
long long largestNumber = 100000000;
long long smallestNumber = 2;
std::pair<long long, long long> p;
cout << "Enter a start number: ";
cin >> p.first;
while (p.first < smallestNumber || p.first > largestNumber || cin.fail()) {
cin.clear();
cin.ignore(1000, '\n');
cout << "That's not a valid choice. Please try again (" << smallestNumber << " - " << largestNumber << "): ";
cin >> p.first;
}
cout << "Enter a end number: ";
cin >> p.second;
while (p.second < p.first || p.second > largestNumber || cin.fail()) {
cin.clear();
cin.ignore(1000, '\n');
cout << "That's not a valid choice. Please try again (" << p.first << " - " << largestNumber << "): ";
cin >> p.second;
}
cout << endl;
return make_pair(p.first, p.second);
}
vector<long long> rangeToVector(const long long &startingNumber, const long long &endingNumber) // creates vector of numbers from std::pair.
{
vector<long long> iterator;
for (long long i = 1; i <= ((10 - 1) + 1); i += 1) iterator.push_back((1-1)+i);
return iterator;
}
void precalculate() // Calculates all of the prime numbers between 1 and MAX. Stores prime numbers from smallest to largest.
{
for (long long i = 1; i < MAX; i++) { // stores 1 through Max in factor.
factor[i] = i;
}
for (long long i = 2; i * i < MAX; i++) if (factor[i] == i) { // If factor[i] is even.
for (long long j = i + i; j < MAX; j += i) {
factor[j] = i;
}
}
}
上述代码按预期工作。然后将代码输入到follow函数中,我不确定是否正常工作(因为我的函数在main中不会编译)。
vector<long long> get_factors(vector<long long> &vect) // performs calculation on rangeToVector[i] (determined by precalculate) until it is not divisible, then it returns those that were divided into the number as prime factors of the rangeToVector[i].
{
vector<long long> factors;
for (auto i : vect)
{
while (vect[i] > 1)
{
long long f = factor[vect[i]];
factors.push_back(f);
vect[i] /=f;
}
}
sort(factors.begin(), factors.end()); // sorts the vector.
factors.erase(unique(factors.begin(), factors.end()), factors.end()); // erases duplicates (requires sorted vector).
return factors;
}
在主(第3行)中,我收到错误下划线rangeToVector
,initial value of reference to non-const must be an lvalue
。
int main()
{
for (auto i : get_factors(rangeToVector(pairs.first, pairs.second))) // get_factors returns factors of numbers in vector rangeToVector
{
cout << i << " ";
}
system("pause");
return 0;
}
我试图在rangeToVector
常量中创建值,但这并没有解决问题。我的问题是,我的引用不正确吗?我应该以不同方式传递函数吗?或者我可以对代码做些什么来允许get_factors
在第3行的主要内容中使用rangeToVector[i]
的因子。我是c ++的新手,所以任何帮助都会非常感激。
答案 0 :(得分:1)
你的问题在于:
std::vector<long long> get_factors(std::vector<long long> &vect) { ... }
变量vect
是一个左值引用,这意味着它是一个无法绑定到临时变量的引用。
然而,你这样使用它:
get_factors(rangeToVector(...))
rangeToVector
返回临时值。
解决方法就是让vect
成为一个常量左值引用,它可以绑定到临时值:
// const here -------v
std::vector<long long> get_factors(std::vector<long long> const& vect) { ... }
您的代码中还有许多其他问题,我建议您阅读一个好的C ++并尝试不要假设它的语法。例如,您没有正确使用range for loops:
for (auto i : vect) {
while (vect[i] > 1) {
long long f = factor[vect[i]];
factors.push_back(f);
vect[i] /=f;
}
}
正确的用法是:
for (auto i : vect) {
while (i > 1) {
long long f = factor[i];
factors.push_back(f);
i /= f; // warning here!
}
}
我发出警告的行是因为你正在修改一个局部变量,没有副作用。这条线的意图尚不清楚。
答案 1 :(得分:0)
首先,我要感谢大家的意见和建议,这有助于我解决我的问题。正如Guillaume Racicot所说,我当然需要更多地研究载体。再次,谢谢。
我的问题出在下面的代码中:
vector<long long> get_factors(vector<long long> &vect) // Notice how I have it relying on "vector<long long> &vect".
{
vector<long long> factors;
for (auto i : vect)
{
while (vect[i] > 1)
{
long long f = factor[vect[i]];
factors.push_back(f);
vect[i] /=f;
}
}
sort(factors.begin(), factors.end()); // sorts the vector.
factors.erase(unique(factors.begin(), factors.end()), factors.end()); // erases duplicates (requires sorted vector).
return factors;
}
以及我在main中调用它的方式:
for (auto i : get_factors(rangeToVector(pairs.first, pairs.second))) // get_factors returns factors of numbers in vector rangeToVector
{
cout << i << " ";
}
为了解决我的问题,我需要意识到,为了访问向量/数组,你必须创建一个for循环来收集它的值。因此,我的解决方案如下。
vector<long long> get_factors(long long x, vector<long long> vec )
{
vector<long long> factors;
while (x > 1) { /* insert code*/ vec.push_back(f); } // Or use a for loop.
return vec;
}
int main(int argc, const char * argv[]) {
vector<long long> temp1;
for (long long val : inputVector) // where initial data was stored (see original post)
{
result1 = getPrimes(val, temp1); // iterate through vec as long long in function
}
return 0;
}