- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在展示给我编译器错误的代码之前,我想简要解释一下为什么我需要一组迭代器到一个列表,只是为了避免像“你真的需要那个吗?”这样的评论。或者也许将它们更改为评论“您的代码可以通过这种方式解决......但是我应该像这样处理原始问题”。您可以跳过阅读并直接转到最后一节(“未编译代码”)。
背后的原因
我构建了一个有向二分加权图。每条弧都存储在一个结构中
typedef struct edge_ {
int src;
int des;
double w; //weight of the arc
}edge;
我使用此类结构的列表存储有关图的信息。
list<edge> all_edges;
之后,我按重量对弧线进行排序,然后将它们从最轻到最重循环循环。因为我想从 all_edges
中删除一些元素在循环中,在我调用的每个循环步骤的开始
list<edge>::iterator smallest = all_edges.begin();
在该循环的每一步中,在对弧的权重做了一些处理后,我想从图中删除所有离开 src
的节点。或以 des
结尾.为此,在图的构造过程中,我创建了两个 vector ,一个用于二分图的每个组件,由它们的元素索引,并且在每个 vector 的每个位置,我将所有迭代器的列表存储到边缘all_edges
它偏离与所考虑的 vector 位置相对应的节点。代码如下(“小”和“大”是我识别二分图两个分量的方式)
vector<list<list<edge>::iterator>> edges_from_small(small.size());
vector<list<list<edge>::iterator>> edges_from_big(big.size());
这是我用来填充上述 vector 的代码
//inside a loop...
edge e;
e.src = ...
e.des = ...
e.w = ...
all_edges.push_back(e);
edges_from_small[e.src].push_back(--(all_edges.end()));
edges_from_big[e.des].push_back(--(all_edges.end()));
假设我想删除边 e
.我很想循环所有元素 edges_from_small[e.src]
并为他们每个人打电话 all_edges.erase(iterator)
, 但是这样做,因为一条边可以同时列在 edges_from_small
中和 edges_from_big
,我会结束使用取消引用的迭代器删除元素的尝试!
Set 将是一个解决方案!我只需要创建一组 list<edge>::iterator
并用 edges_from_small[e.src]
的元素填充它和 edges_from_big[e.des]
然后,由于删除了重复项,因此删除列表中的所有元素 all_edges
.
但我的代码无法编译,它在以下行之一处给我一个我无法理解的错误:
set<list<edge>::iterator> to_remove;
for (auto it = edges_from_small[smallest->src].begin(); it != edges_from_small[smallest->src].end(); ++it) {
to_remove.insert(*it); //error here!
}
for (auto it = edges_from_big[smallest->des].begin(); it != edges_from_big[smallest->des].end(); ++it) {
//to_remove.insert(*it); //error here!
}
for (auto it = to_remove.begin(); it != to_remove.end(); ++it) {
all_edges.erase(*it);
}
编译器给了我一个相当大的输出(全部引用上面的行),目前我只输入了第一行和最后一行,我认为这是最具指示性的。
g++ -ggdb3 -g -O0 -std=c++14 -Wall -Werror -o compare main.cpp peaks.cpp common.cpp compare.cpp parameters.cpp
In file included from compare.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:628:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:606:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:344:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__functional_base:63:21: error:
invalid operands to binary expression ('const std::__1::__list_iterator<_edge,
void *>' and 'const std::__1::__list_iterator<_edge, void *>')
{return __x < __y;}
~~~ ^ ~~~
....................MUCH MORE OUTPUT....................
compare.cpp:226:23: note: in instantiation of member function
'std::__1::set<std::__1::__list_iterator<_edge, void *>,
std::__1::less<std::__1::__list_iterator<_edge, void *> >,
std::__1::allocator<std::__1::__list_iterator<_edge, void *> > >::insert'
requested here
to_remove.insert(*it);
^
1 error generated.
make: *** [compare] Error 1
Compilation exited abnormally with code 2 at Tue Sep 5 17:34:39
知道那条线有什么问题吗?
未编译代码
typedef struct _edge {
int src;
int des;
double w; //weight of the arch
}edge;
list<edge> all_edges;
vector<list<const list<edge>::iterator>> edges_from_small(small.size());
vector<list<const list<edge>::iterator>> edges_from_big(big.size());
//graph construction
//for loop ...
edge e;
e.src = ...
e.des = ...
e.w = ...
all_edges.push_back(e);
edges_from_small[e.src].push_back(--(all_edges.end()));
edges_from_big[e.des].push_back(--(all_edges.end()));
//end of loop
list<edge>::iterator smallest = all_edges.begin();
set<list<edge>::iterator> to_remove;
for (auto it = edges_from_small[smallest->src].begin();
it != edges_from_small[smallest->src].end(); ++it) {
to_remove.insert(*it); //<--- COMPILER ERROR HERE, you can see the error description in the last lines of the previous paragraph
}
最佳答案
std::list::iterator
无法排序,因为没有函数或运算符来比较它们(wrt less/greater)。
改为使用 std::unordered_set
,这不需要对元素进行排序,它使用元素的哈希值将它们放入桶中。您可能需要提供一个哈希函数,只需在 std::unordered_set
的 namespace std
中放置一个 std::hash
的重载即可找到并使用它。
另请注意,std::unordered_set
具有插入的平均恒定时间复杂度,而 std::set
关于c++ - 无法将迭代器的 std::set 中的元素插入到 std::list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46058894/
#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
我是一名优秀的程序员,十分优秀!