从迭代器强制转换为已定义类型

Casting from an iterator to a defined type?

本文关键字:定义 类型 转换 迭代器      更新时间:2023-10-16

我正在尝试构建一个循环,该循环将在typedef std::pair<double, double> CFPointstd::vector上循环,以产生两个点的集合。

if(pointList.size() % 2 == 0)
{
    CFPoint first = NULL;
    CFPoint second = NULL;
    for (std::vector<CFPoint>::const_iterator it = pointList.begin(); it != pointList.end(); ++it)
    {
        TRACE("APoint: %f, %fn", it->first, it->second);
        if(first == NULL && second == NULL)
            {first = it; continue;}
        if(first != NULL && second == NULL)
            second = it;
        if(first != NULL && second != NULL)
        {
            double transitLen = sqrt(  pow((second.first - first.first), 2) + pow((second.second - first.second), 2) );
            TRACE("Distance between points: %fn", transitLen);
            first = NULL;
            second = NULL;
        }
    }
}

查看代码,您可能会看到我使用= NULL来表示CFPoint变量"准备好"或不使用。我的问题是NULL在c++中被定义为0,我不能将0分配给CFPoint。c++中也没有null关键字。

我的选项可能是设置我的变量为"null"或类似的东西来实现上面的循环?

嗯,当我写这篇文章的时候,我想到了在我的循环中使用指针而不是实际的变量。可以设置指针为NULL。唯一的问题是当试图设置firstsecond。我试图通过做first = &it;来设置我的first,但ititerator类型,我怎么能将it转换为CFPoint*呢?

你可以这样做:

if (pointList.size() % 2 == 0)
{
    for (std::size_t i = 0; it != pointList.size(); i += 2)
    {
        const CFPoint& first = pointList[i];
        const CFPoint& second = pointList[i + 1];
        TRACE("APoint: %f, %fn", first.first, first.second);
        TRACE("APoint: %f, %fn", second.first, second.second);
        const double transitLen = sqrt(pow((second.first - first.first), 2) + pow((second.second - first.second), 2) );
        TRACE("Distance between points: %fn", transitLen);
    }
}

你的任务实际上不需要一个'null'值的点,你所需要的只是迭代对向量元素。

这个怎么样:

if (pointList.size() % 2 == 0)
{
    for (std::vector<CFPoint>::const_iterator it = pointList.begin(); it != pointList.end(); it += 2)
    {
        TRACE("APoint: %f, %fn", it->first, it->second);
        CFPoint first = *it;
        CFPoint second = *(it+1);
        double transitLen = sqrt(pow((second.first - first.first), 2) + pow((second.second - first.second), 2));
        TRACE("Distance between points: %fn", transitLen);
    }
}

明显的问题是std::pair<double, double>没有有一个时态值和它的组成部分的名称是什么可能也不合适。(还有一个问题是你的命名方式表明它是MFC的一部分。)您应该将类型定义为类,并为空值,如果需要的话。这也是可以做到的通过添加额外的bool来表示值是否是有效的还是无效的,或者通过对nan做一些棘手的事情。(首选第一名,至少一开始是这样。第二个)

it->first和it->second不是指针。它们都是双份的。那么你在这里做什么呢:

if(first == NULL && second == NULL)

检查它们是否等于0。

甚至也是一个指针,它是CFPoint而不是CFPoint

CFPoint first = NULL; CFPoint second = NULL;

也不是指针,这就是为什么不能将它们设置为NULL的原因。试一试:

CFPoint* first = NULL; CFPoint* second = NULL;

,也可以使用std::vector。

或者这样做:

typedef typedef std::pair<double, double>* CFPoint

构建解决方案的方式似乎使其变得比可能需要的更复杂(Jarod42的解决方案显示了一种更简单的方法)。运行它,虽然你应该能够让它工作使用pointList.end()作为你的保护值:

if(pointList.size() % 2 == 0)
{
    typedef std::vector<CFPoint>::const_iterator ConstPointIterator;
    const ConstPointIterator guardValue(pointList.end());
    ConstPointIterator first = guardValue;
    ConstPointIterator second = guardValue;
    for (ConstPointIterator it = pointList.begin(); it != pointList.end(); ++it)
    {
        TRACE("APoint: %f, %fn", it->first, it->second);
        if(first == guardValue && second == guardValue)
            first = it; continue;
        if(first != guardValue && second == guardValue)
            second = it;
        if(first != guardValue && second != guardValue)
        {
            double transitLen = sqrt(  pow((second.first - first.first), 2) + pow((second.second - first.second), 2) );
            TRACE("Distance between points: %fn", transitLen);
            first = guardValue;
            second = guardValue;
        }
    }
}