- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试根据 vector 中的坐标是否被其他坐标包围或支配来对它们进行排序。例如,坐标 [1 , 2 , 1 , 1]
被 [4, 2 , 1 , 2]
包围或控制,即使两者的第二个和第三个值坐标相等。
节目亮点。 (完整程序 online at rextester.com )
int input[18][4] = { { 4 , 3 , 3 , 3 } , { 1 , 5 , 4 , 1 } , { 2 , 4 , 5 , 4 } ,
{ 3 , 1 , 2 , 5 } , { 4 , 2 , 1 , 2 } , { 1 , 3 , 3 , 1 } ,
{ 2 , 3 , 3 , 3 } , { 3 , 1 , 2 , 3 } , { 5 , 2 , 1 , 2 } ,
{ 1 , 4 , 4 , 1 } , { 1 , 1 , 2 , 1 } , { 1 , 2 , 1 , 1 } ,
{ 2 , 1 , 2 , 4 } , { 2 , 2 , 1 , 2 } , { 3 , 1 , 1 , 2 } ,
{ 2 , 1 , 2 , 3 } , { 1 , 1 , 1 , 1 } , { 2 , 1 , 1 , 2 } };
struct Coordinate
{
Coordinate(){}
Coordinate( int (&val)[4] );
bool operator<( const Coordinate& otherCoord ) const;
void print() const;
int value[4];
};
void print( const std::vector<Coordinate>& coord );
int main()
{
std::vector<Coordinate> coord;
coord.assign( input , input + 18 );
print( coord );
std::sort( coord.begin() , coord.end() );
print( coord );
}
但是程序输出不是我所期望的,
[ 1 , 1 , 1 , 1 ]
[ 1 , 4 , 4 , 1 ]
[ 2 , 1 , 1 , 2 ]
[ 2 , 1 , 2 , 3 ]
[ 3 , 1 , 1 , 2 ]
[ 1 , 3 , 3 , 1 ]
[ 2 , 2 , 1 , 2 ]
[ 2 , 1 , 2 , 4 ]
[ 1 , 2 , 1 , 1 ]
[ 1 , 1 , 2 , 1 ]
[ 4 , 3 , 3 , 3 ]
[ 5 , 2 , 1 , 2 ] // <-- ???
[ 3 , 1 , 2 , 3 ]
[ 2 , 3 , 3 , 3 ]
[ 4 , 2 , 1 , 2 ] // <-- ???
[ 3 , 1 , 2 , 5 ]
[ 2 , 4 , 5 , 4 ]
[ 1 , 5 , 4 , 1 ]
例如 [ 5 , 2 , 1 , 2 ]
包络或支配 [ 4 , 2 , 1 , 2 ]
但出现在它之前,如程序输出所示.
最佳答案
你要的是lexicographical ordering这基本上相当于说比较(x1, y1) < (x2, y2)
相当于说if (x1 < x2 || (x1 == x2 && y1 < y2))
你的 body Coordinate::operator<
可以修改如下:
for( int i = 0; i < 4; ++i ) {
if( value[i] > otherCoord.value[i] )
return false;
if (value[i] < otherCoord.value[i] )
return true;
}
return false;
我们返回false
最后,因为我们正在执行严格的小于比较。当我们到达那条线时,我们知道两个坐标的所有元素都是相同的,所以如果我们返回 true 那么我们就满足了 <=
相反。
但是,我建议您更新此代码以使用更现代的 C++。即 vector 和数组。这很好,尤其是因为默认 operator<
对于 std::array
将为您执行字典排序。 (此外,您不必担心指针数学,因为您可以使用迭代器)。
这是你的新类(class):
template<size_t N>
struct Coordinate
{
Coordinate(){}
Coordinate( std::array<int, N> _val);
bool operator<( const Coordinate& otherCoord ) const;
void print() const;
std::array<int, N> value;
};
下面是您将如何实现 operator<
:
template<size_t N>
bool Coordinate<N>::operator<( const Coordinate<N>& otherCoord ) const
{
return value < otherCoord.value;
}
最后 main
:
int main()
{
std::vector<Coordinate<4>> coords;
coords.assign( input.begin(), input.end() );
print(coords);
std::sort(coords.begin(), coords.end());
print( coords );
}
首选 Coordinate
的模板这样您就可以在编译时制作任意维度的坐标。现在有很多 magic numbering继续使一切正常。
关于c++ - 按 'enveloping' 或 'dominance' 标准排序坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43936023/
我必须使用工具来生成 SOAP 请求,它可以通过两种方式来实现。生成以以下开头的请求: 或与: 我怀疑一种是当前技术,另一种是遗留技术。但是由于只有一个在 url 中有类似日期的东西,我无法弄清楚
之间有什么区别 和 以及如何在它们之间切换? 如何将响应从 具有定义的命名空间 "http://schemas.xmlsoap.org/soap/envelope/" ,它的特殊含义是底层 XML
我的 wcf 和我的服务的soap xml 响应有问题。怎么改
我需要从使用信封模式 (p7m) 或信封模式 (p7s) 的签名文件中提取其签名的原始文件。 我很难弄清楚如何使用 bouncycaSTLe 库来做到这一点。 我同时使用 BouncyCaSTLe 1
我正在为 android 中的 DBConnection 起诉 asp.net webservice (visual studio 2008)。 运行成功。 我的安卓代码是: protected vo
我当前正在尝试从我的 Azure Function 调用 SOAP 服务,但出现以下错误: 但是,我能够成功处理来自 .NET 应用程序的请求。 HTTP Status 500 - Request p
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 4 年前。 Improve
smtp中的信封和电子邮件之间的确切区别是什么? 为什么协议(protocol)需要信封?在蜗牛邮件中,信件不需要包含地址并且 postman 看不到(至少这是你想要相信的),所以所有的路由都是完全基
免责声明:这不是任何类型的作业,这个问题只是在我浏览所有圣诞贺卡时想到的 问题如下:我们有 M 个信封和 N 个字母,每个字母都被描述为一对正整数。信封和信件都是长方形的,显然可以旋转。如果两个尺寸都
我收到错误“找不到元素soapenv:Envelope 的声明” 与其他 xml 文件 xsd 一起工作正常..问题在于以下文件.. 我的 Xml 文件是:-
我正在尝试将 SoapObject 或 Envelope 存储在一个文件中,但认为它不是可序列化的,因此它没有存储在文件中。 我的目标是将 SoapObject 或 Envelope 一次存储在文件中
我正在尝试连接到通过网络服务客户端保护的网络服务 https。 Eclipse 生成了一个基于 stub 的 web 服务客户端,对我来说看起来不错。当我尝试从网络服务调用方法时出现问题: Strin
我正在查询其余 API 以获取所有信封。 以下是UI中的文档 当我使用包含正确的开始日期和结束日期以及帐户 ID 的信封列表更改进行查询时 它只给我第一个和最后一个文档,如下所示。 最佳答案 您可以使
我正在尝试在 Windows 7(64 位)上运行的 Delphi XE7 应用程序中通过 FireDAC 加密/解密 SQLite 数据库。 代码如下所示: Procedure TMain.Acti
我正在编写一个 IMAP 客户端,我有一个问题。 获取发件人、发件人、收件人、抄送等 - 我可以 FETCH RFC822.HEADER ,这会给我一个长字符串,有时不相关信息,或者我可以 FETCH
上下文:我正在尝试在网络上制作一个 100% 模块化合成器 到目前为止,我看到的制作包络的教程使用函数来触发包络的不同阶段,但是如果我想根据 AudioNode 的输出触发包络该怎么办? 例如,在方波
我对这一切都很陌生,但我正在尝试创建 SOAP 消息并陷入困境,我使用 Java 8 和标准 javax.xml.soap 类,但似乎无法将 namespace 添加到信封 MessageFactor
我正在尝试根据 vector 中的坐标是否被其他坐标包围或支配来对它们进行排序。例如,坐标 [1 , 2 , 1 , 1] 被 [4, 2 , 1 , 2] 包围或控制,即使两者的第二个和第三个值坐标
我正尝试在 python 库的帮助下使用 wsdl zeep .它工作正常,但我无法找到如何从请求中获取数据。 我的代码: # encoding=utf-8 from requests.auth im
我正在尝试将 Go 结构编码为 Soap-Envelope (xml)。到目前为止,除了一个小错误外,皂体看起来还不错。虽然我的信封应该是这样的: param1
我是一名优秀的程序员,十分优秀!