- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我从事大数据和 C++ 编程工作。例如,我需要创建大小为 [7 x 128^3 x 5 x 5] 等的 4 维数组。我将不得不创建更多的数组作为不同属性的中间数据结构。研究了很多,最终还是选择了boost的multi_array来实现我的数据结构。我选择 multi_array 有两个原因:(1) 它处理数组索引越界等异常,这对我调试非常重要。(2) 它处理更高维度的数组(其他选项如STL的多维数组对于3维和更高维度的数组有很多问题)
问题示例。
如果我用一个例子来解释这个问题就变得容易了。比方说,我有以下 3-D 数组。
[(1,2,3), (4,5,6), (7,8,9)]
[(1,2,3), (1,3,3), (4,1,1)]
[(8,9,9), (6,9,7), (17,2,3)]
[(1,2,3), (1,3,2), (2,8,1)]
[(1,2,3), (1,3,3), (4,2,1)]
我想以这样一种方式对这些行进行排序,即排序必须先基于 column_1,然后是 column_2,然后是 column_3。排序后我会有
[(1,2,3), (1,3,2), (2,8,1)]
[(1,2,3), (1,3,3), (4,1,1)]
[(1,2,3), (1,3,3), (4,2,1)]
[(1,2,3), (4,5,6), (7,8,9)]
[(8,9,9), (6,9,7), (17,2,3)]
您会看到 column_1 已排序。对于每两行具有相同的 column_1,则对 column_2 进行排序,依此类推。
我尝试过的。
我能够用普通的 3-D 数组解决这个问题,并编写一个递归比较器并调用库的排序函数(使用比较器)。但是,我改成boost的multi_array后,一直没能解决问题。我搜索了很多 boost 文档我找不到任何解决方案。我不知道如何编写递归比较器来对 boost multi_array 进行排序。
问题。
谁能告诉我用于 boost multi_array 的递归比较器的确切工作语法来对 multi_array 进行排序?代码不得使用依赖于特定编译器/操作系统的 API。假设多数组A的维度为n1 x n2 x ... x nd。
谢谢。
回复 Yakk。
递归比较器是这样的:
struct comparebyID
{
bool operator()(celltuple const &t, celltuple const &u)
{
return comparator(0, t, u);
}
bool comparator(int i, celltuple const &t, celltuple const &u)
{
if (i == (levels - 1))
return t.childIDs[i] < u.childIDs[i];
if (t.childIDs[i] == u.childIDs[i])
return comparator(i + 1, t, u);
else
return t.childIDs[i] < u.childIDs[i];
}
};
使用递归比较器的排序函数是这样的:
sort(cellset, cellset + count, comparebyID());
多维数组是这样的:
struct celltuple
{
cell c[MAX_SIZE];
unsigned long long IDs[MAX_IDS];
int childIDs[MAX_IDS];
int prevChildIDs[MAX_IDS];
unsigned long long prevIDs[MAX_IDS];
}cellset[MAX_CELLTUPLES];
我没有包括每个参数代表什么的许多其他细节,因为它变得困惑(因为它试图做许多其他事情)但核心思想如示例中所述。
我想做的是为 multi_array 编写一个递归比较器,如下定义。
boost::multi_array<int, 3> cell_tuple;
我不能像 compareByID 这样简单地编写比较器,因为当对象是 multi_array 时,我不知道如何将参数传递给比较器函数。
这有帮助吗?
回复sehe.
优秀的解决方案。万分感谢。看来你是使用boost和c++的天才。它完全奏效了。您用于交换和比较器功能的想法非常棒。我不知道这些函数调用(例如 lexicographical_compare() 等)甚至存在。非常感谢。
我有两个相关的问题:
(1) 例如,我们对所有维度的多数组 A 进行排序。我们想对 multi_array B 应用相同的交换/交换/转换。我们可以用你给出的想法来做到这一点吗?
我知道我们可以通过编写一个单独的自定义排序器来解决这个问题(当我们交换时,我们可以交换 A 和 B 中的组件)。但是我很好奇这个问题是否可以用比较器的概念来解决,因为当我们使用它来对 A 进行排序时,比较器对多数组 B 一无所知。如何解决这个问题?
(2) my_comp 中有几个重载函数真的有必要吗?难道我们不能为此目的拥有一个完全通用的功能吗? (抱歉,我是 multi_array、sub_array 概念的新手)。
最佳答案
你不仅需要一个比较器,你还需要另一个concepts required for std::sort
to work以及。具体来说:
RandomIt
must meet the requirements ofValueSwappable
andRandomAccessIterator
.
因此,我破解了一个通用的 swap
实现。注意它使用了实现细节:
namespace boost { namespace detail { namespace multi_array {
template <typename T, size_t dims>
static void swap(sub_array<T, dims> a, sub_array<T, dims> b) {
using std::swap;
for (auto ai = a.begin(), bi = b.begin(); ai != a.end() && bi != b.end(); ++ai, ++bi) {
swap(*ai, *bi);
}
}
} } }
比较器可以同样简单明了,看起来像:
struct my_comp {
template <typename T, size_t dims>
bool operator()(sub_array<T, dims> const& a, sub_array<T, dims> const& b) const {
return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(), *this);
}
// ... some technical overloads omitted
template <typename T>
bool operator()(T a, T b) const {
return std::less<T>()(a,b);
}
};
在所有情况下,我们只是遵从标准库算法来完成工作,递归传递 *this
作为比较器!
#include <boost/multi_array.hpp>
#include <iostream>
namespace ba = boost::multi_array_types;
using Arr = boost::multi_array<int, 3>;
static std::ostream& operator<<(std::ostream& os, Arr const& arr) {
for(auto const& row : arr) {
for (auto const& col : row) {
for (auto const& cell : col) os << cell << " ";
os << ";";
}
os << "\n";
}
return os;
}
struct my_comp {
// short hand only:
template <typename T, size_t dims> using sub_array = boost::detail::multi_array::sub_array<T, dims>;
template <typename T, size_t dims>
bool operator()(sub_array<T, dims> const& a, sub_array<T, dims> const& b) const {
return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(), *this);
}
template <typename T, size_t dims>
bool operator()(boost::multi_array<T, dims> const& a, sub_array<T, dims> const& b) const {
return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(), *this);
}
template <typename T, size_t dims>
bool operator()(sub_array<T, dims> const& a, boost::multi_array<T, dims> const& b) const {
return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(), *this);
}
template <typename T>
bool operator()(T a, T b) const {
return std::less<T>()(a,b);
}
};
namespace boost { namespace detail { namespace multi_array {
template <typename T, size_t dims>
static void swap(sub_array<T, dims> a, sub_array<T, dims> b) {
using std::swap;
for (auto ai = a.begin(), bi = b.begin(); ai != a.end() && bi != b.end(); ++ai, ++bi) {
swap(*ai, *bi);
}
}
} } }
using boost::detail::multi_array::swap;
#include <boost/range/algorithm.hpp>
int main() {
Arr arr(boost::extents[5][3][3]);
auto initial = {
1,2,3, 4,5,6, 7,8,9,
1,2,3, 1,3,3, 4,1,1,
8,9,9, 6,9,7, 17,2,3,
1,2,3, 1,3,2, 2,8,1,
1,2,3, 1,3,3, 4,2,1,
};
boost::copy(initial, arr.origin());
std::cout << arr;
std::sort(arr.begin(), arr.end(), my_comp{});
std::cout << "After sort\n" << arr;
}
打印:
1 2 3 ;4 5 6 ;7 8 9 ;
1 2 3 ;1 3 3 ;4 1 1 ;
8 9 9 ;6 9 7 ;17 2 3 ;
1 2 3 ;1 3 2 ;2 8 1 ;
1 2 3 ;1 3 3 ;4 2 1 ;
After sort
1 2 3 ;1 3 2 ;2 8 1 ;
1 2 3 ;1 3 3 ;4 1 1 ;
1 2 3 ;1 3 3 ;4 2 1 ;
1 2 3 ;4 5 6 ;7 8 9 ;
8 9 9 ;6 9 7 ;17 2 3 ;
关于c++ - 使用排序函数和递归比较器对 boost 的 multi_array 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36117142/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!