简体   繁体   English

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

[英]Dereference pointer to array of long from list iterator

How do I correctly dereference pointers to arrays of long . 如何正确取消对long数组的指针的引用。
In the function main my code seems to print the addresses instead of the long values. 在函数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";
    }
}

Thanks to the comments I realized that arrays within lists are a bad idea in C++. 多亏了这些注释,我意识到列表内的数组在C ++中不是一个好主意。
I changed my code to the following, which works: 我将代码更改为以下代码,它可以正常工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM