从列表迭代器取消引用long数组的指针

时间:2018-12-11 16:09:39

标签: c++ arrays list pointers

如何正确取消对long数组的指针的引用。
在函数main中,我的代码似乎在打印地址而不是长值。

#include <list>
#include <iostream>
#include <cmath>
#include <vector>

bool
is_prime (long i)
{
    // fix wrong return values for 0 and 1
    if (i <= 1)
        return false;

    const long j = (long) std::sqrt(i);

    for (long k=2; k<j+1; ++k) {
        if (i % k == 0)
            return false;
    }

    return true;
}

std::list<long(*)[2]>
goldbach (long number)
{
    std::list<long(*)[2]> sums;
    std::list<long(*)[2]>::iterator it;

    if (number < 4)  // Exclude too small integers.
        return sums;

    if (number % 2)  // Exclude uneven numbers.
        return sums;

    it = sums.begin();

    for (long candidate=2; candidate < number; ++candidate)
    {
        if (is_prime(candidate))
        {
            if (is_prime(number - candidate))
            {
                long sum[2];
                sum[0] = candidate;
                sum[1] = number - candidate;
                std::cout << sum[0] << " + " << sum[1] << "\n";
                sums.insert(it, &sum);
                ++it;
            }
        }
    }

    return sums;
}

int
main ()
{
    std::list<long(*)[2]> sums;
    std::list<long(*)[2]>::iterator it;

    for(long i = 4; i < 100; i+=2)
    {
        std::cout << "### " << i << " ###" << "\n";
        sums = goldbach(i);

        for (it = sums.begin(); it != sums.end(); ++it)
        {
            long num1 = *((*it)[0]);
            long num2 = *((*it)[1]);
            std::cout << num1  << " + "  << num2 << "\n";
        }

        std::cout << "\n\n";
    }
}

1 个答案:

答案 0 :(得分:0)

由于有了注释,我意识到列表中的数组在C ++中不是一个好主意。
我将代码更改为以下代码,它可以正常工作:

#include <list>
#include <iostream>
#include <cmath>
#include <vector>

bool
is_prime (long i)
{
    // fix wrong return values for 0 and 1
    if (i <= 1)
        return false;

    const long j = (long) std::sqrt(i);

    for (long k=2; k<j+1; ++k) {
        if (i % k == 0)
            return false;
    }

    return true;
}

std::list<std::pair<long, long>>
goldbach (long number)
{
    std::list<std::pair<long, long>> sums;
    std::list<std::pair<long, long>>::iterator it;

    if (number < 4)  // Exclude too small integers.
        return sums;

    if (number % 2)  // Exclude uneven numbers.
        return sums;

    it = sums.begin();
    long complement;

    for (long candidate=2; candidate < number; ++candidate)
    {
        if (is_prime(candidate))
        {
            complement = number - candidate;

            if (is_prime(complement))
            {
                std::pair<long, long> sum = std::make_pair(candidate, complement);
                sums.insert(it, sum);
                ++it;
            }
        }
    }

    return sums;
}

int
main ()
{
    std::list<std::pair<long, long>> sums;
    std::list<std::pair<long, long>>::iterator it;

    for(long i = 4; i < 100; i+=2)
    {
        std::cout << "### " << i << " ###" << "\n";
        sums = goldbach(i);

        for (it = sums.begin(); it != sums.end(); ++it)
        {
            std::cout << it->first  << " + "  << it->second << "\n";
        }

        std::cout << "\n\n";
    }
}