从迭代器强制转换为已定义类型
Casting from an iterator to a defined type?
我正在尝试构建一个循环,该循环将在typedef std::pair<double, double> CFPoint
的std::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
。唯一的问题是当试图设置first
和second
。我试图通过做first = ⁢
来设置我的first
,但it
是iterator
类型,我怎么能将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;
}
}
}
- 使用QJsEngine在Qt中注册自定义类型
- 在UE4中使用未定义类型'UTextBlock'
- 修改"std::set"中用户定义类型的值
- 当我使用自定义类型创建动态数组时,即使使用字符串,它似乎也不起作用
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- 如何使自定义类型在unordered_map中用作键
- 在 C++20 中是否不再允许在 std 中对程序定义类型的函数模板进行专用化?
- Qt5 远程对象 + 自定义类型,但不在 POD 中
- 为什么转换函数声明不需要至少一个定义类型说明符
- 标准::原子中的自定义类型
- 如何使用自定义类型声明Arduino数组?
- 如何在 Cython 中定义返回 cpp 定义类型的函数?
- 使用自定义访问者时具有自定义类型的提升变体失败(源自 boost::static_visitor)
- 您可以将binary_search应用于具有自定义类型的矢量吗?
- 在自定义类型图中重用 SWIG 映射
- 扩展自定义类型的spdlog
- vim使用户定义类型的COLOR与C++中的基本类型相同
- 重载自定义类型的 std::to_string 和 std::to_chars?
- 具有未声明/未定义类型的 typedef 结构
- 函数重载:内置类型与用户定义类型