-
3 votesanswersviews
将临时istringstream对象传递给istream_iterator <string>
我有一个关于以下代码的问题,该代码标记了一个字符串(用空格分隔标记) . #include <iostream> #include <iterator> #include <sstream> #include <string> #include <vector> using namespace std; int main() { ... -
0 votesanswersviews
使用boost :: sregex_token_iterator时出错
我想搜索正则表达式并用颜色打印 . 我使用boost :: sregex_token_iterator来做到这一点 . 这是我的代码 boost::regex re("ab."); string s=""; string buf; string infile("retest.txt"); //string ... -
-1 votesanswersviews
如何从C中的map迭代器调用类成员函数?
我无法使用map的迭代器调用show函数 . 有没有办法使用迭代器来做到这一点? #include <iostream> #include <string> #include <map> using namespace std; class A { int i; public: A(int pi):i(pi) { cout&l... -
0 votesanswersviews
将新列附加到groupby对象中的pandas数据帧
我想将列添加到groupby对象中的pandas数据帧 # create the dataframe idx = ['a','b','c'] * 10 df = pd.DataFrame({ 'f1' : np.random.randn(30), 'f2' : np.random.randn(30), ... -
13 votesanswersviews
使用Iterator collect键入问题
我正在尝试使用以下代码片段将 &str 对的向量转换为 HashMap : use std::collections::HashMap; fn main() { let pairs = vec!(("foo", "bar"), ("toto", "tata")); let map: HashMap&l... -
2 votesanswersviews
无法将元素插入到嵌套的stl集合中
我有一组嵌套的int但我不能将元素插入到嵌套集中 . std::set<std::set<int> > centre_as_set = bitset_to_set(centre->second->bit_partitions); std::set<std::set<int> >::iterator set_itr; for ( set_i... -
0 votesanswersviews
Iterator对矢量对,并使用std :: copy打印结果
我有以下向量: std::vector<std::pair<int,std::string>> songs; 我想使用std :: copy将向量中的元素(比如说int)传递给ostream,类似于以下哪些方法不起作用: std::copy(songs.begin(),songs.end(),std::ostream_iterator<int>(std::cou... -
531 votesanswersviews
在Java中调用foreach循环中的remove [duplicate]
这个问题在这里已有答案: Iterating through a Collection, avoiding ConcurrentModificationException when modifying (removing) in loop 22个答案 在Java中,使用foreach循环遍历集合时,对集合调用remove是否合法?例如: List<String> names = .... -
600 votesanswersviews
如何并行迭代两个列表?
我在Python中有两个迭代,我想成对地遍历它们: foo = (1, 2, 3) bar = (4, 5, 6) for (f, b) in some_iterator(foo, bar): print "f: ", f, "; b: ", b 它应该导致: f: 1; b: 4 f: 2; b: 5 f: 3; b: 6 一种方法是迭代索引... -
1 votesanswersviews
为什么List包装在IntoIter中?
在Learning Rust With Entirely Too Many Linked Lists的书中 IntoIter 的implementation中,为什么 List 包含在元组结构中?相反,可以为 List 实施 Iterator . -
6 votesanswersviews
为什么不能在迭代器上下文中使用unsafe关键字?
在看这个问题时,Jon在关闭之前做了很好的工作......'How to read a text file reversly with iterator '. And there was a similar question in which I answered using pointers hocus pocus..' .net is there a way to read a text fi... -
456 votesanswersviews
迭代器失效规则
C容器的迭代器失效规则是什么? 优选地以摘要列表格式 . (注意:这是Stack Overflow的C FAQ的一个条目 . 如果你想批评在这个表单中提供常见问题解答的想法,那么发布所有这些的meta上的帖子就是这样做的地方 . 这个问题在C聊天室中受到监控,其中FAQ的想法首先出现在那里,所以你的答案很可能被那些提出这个想法的人阅读 . ) -
1 votesanswersviews
vector.erase(begin(vector))将删除vector的LAST元素
下列 void PlayListModel::removeTrack(int track) //does not work { qDebug()<<track; assert(track >= 0); if (not mTracks.empty() and static_cast<int>(track) < mTracks.size())... -
0 votesanswersviews
如何使用end()迭代器扩展范围
(不确定这是正确的任务)我正在编写一个与经典排序相关的stl风格的算法 . 原型是: template<typename RAIter> void Algo(RAIter first, RAIter last) { .... size_t size = std::distance(first, last); RAIter midIter =first; ... -
1 votesanswersviews
使用erase删除remove_if
这件事让我发疯,我无法理解: // v contains : 101, 1, 2, 3, 4 , 5 v.erase(remove_if(v.begin(), v.end(), bind2nd(less<int>(), 3)), v.end()); // v contains now : 101, 3, 4, 5 为什么在擦除中有v.end()作为第二个参数?我读到remove_if... -
0 votesanswersviews
在循环时安全地允许删除向量迭代器值
我有一个类型的向量,它有两个函数,setSucceeed()和setFailed() . 调用setFailed导致一些工作正在完成,然后该对象被破坏 . 我需要迭代这个向量,并根据外部条件为每个元素调用其中一个函数 . 这是我在此代码中的初始传递: void F(bool success) { for (vector<t>::iterator i = myVector.beg... -
0 votesanswersviews
我的矢量迭代器出现了一个奇怪的错误
我有一个向量的迭代器,我想删除向量中的某些元素 . 运行代码时,编译中没有出现错误,但在执行后我收到一条消息“调试断言失败!” . 有人能告诉我迭代器有什么问题吗? int function(unsigned int n, unsigned int m) { vector<int> vec1; vec1.push_back(3); vec1.push_back(4);... -
4 votesanswersviews
条件中的迭代器赋值 - 矢量迭代器不兼容
我有 std::vector 的包装器,我已经实现了用另一个向量替换向量中的一个部分的函数 . I've tried to put the assignment of an iterator directly in the if condition and got unexpected results . 我正在使用Visual Studio 2013并且 FAIL 已定义我得到 Debug... -
4 votesanswersviews
使用迭代器 - C找到矢量的中间元素
我有两个问题 . First question 我在 C++ 从 C++ primer 5th edition 书中读到 iterators . 在本书的一个代码示例中,给出了以下代码片段将在向量中找到中间元素 vi auto mid = vi.begin() + vi.size() / 2; 现在我想问一下,为什么在 vi.size() 中添加 vi.begin() ? vi.size()... -
0 votesanswersviews
基于范围的for循环向量迭代器不可递增
在我的应用程序中,我将所有当前打开的窗口存储在一个向量中,以便进行事件处理 . 打开窗口时,它会添加到矢量中并在关闭时删除 . 我有一个进程事件函数,我遍历该向量并使用当前事件更新所有窗口: for(Window* w : windows) w->OnWindowEvent(event); 但是,如果我关闭窗口,我会收到错误'Debug Assertion failed:v... -
1 votesanswersviews
什么是PHP中的对象迭代
任何人都可以解释什么是对象迭代以及此代码的工作原理? class MyIterator implements Iterator{ private $var = array(); public function __construct($array){ if (is_array($array)) { $this->var = $array; ... -
4 votesanswersviews
Scala:在修改序列时迭代序列?
我正在尝试在Scala中实现Sieve of Eratosthenes . 我首先初始化所有奇数加2的序列: // (end goal is to find all prime factors of bigNumber) val largestPrime : Long = Math.ceil(Math.sqrt(bigNumber)).toLong var nums : Seq[Long] = (... -
7 votesanswersviews
C中迭代器的键/值功能
我知道这些问题之前有各种各样的问题,但这有点不同 . 我有一个包含std :: map的类 . 虽然我希望在类中使用映射用于其他目的,但在外部我想要将迭代器适配器暴露给映射内的值(即std :: pair中的第二项) . 例如在python中我可能会这样做: def __iter__(self): return self._dict.itervalues() 我如何在c中执行此操作,将实... -
8 votesanswersviews
使用泛型迭代器而不是特定的列表类型
我是Rust的新手,来自C#/ Java /类似 . 在C#中,我们有 IEnumerable<T> 可用于迭代几乎任何类型的数组或列表 . C#还有一个 yield 关键字,可用于返回惰性列表 . 这是一个例子...... // Lazily returns the even numbers out of an enumerable IEnumerable<int> E... -
2 votesanswersviews
类似的python groupby在haskell
在python中有groupby函数 . 它的类型可以用像这样的haskell表示 groupby :: a->b->[a]->[(b, [a])] 因为需要对数据进行排序,我们可以将它的运行时间视为 O(n*log(n)) . 我可能不是唯一一个对此不满意的人,所以我发现这个library这个groupby的实现需要两次传递输入序列 . 所以我认为它的运行时间是 O(n) ,... -
6 votesanswersviews
共享,借用Vec上的iter()和into_iter()之间的区别?
我正在阅读Rust 101 tutorial,其中作者谈到共享借用的例子是传递给函数的 Vec 对象 . 下面是教程正在教授的略微改进的MWE . 有趣的部分是 vec_min 在 vec_min . 作者写道: 这次,我们明确地请求向量v的迭代器 . 该方法借用它所处理的向量,并提供元素的共享借用 . 但是如果我在共享的对象上使用 for ... in ... 构造会发生什么?根据this... -
6 votesanswersviews
如何使用async for循环迭代列表?
所以我需要为列表中的所有项调用 async 函数 . 这可以是一个URL列表和一个使用 aiohttp 的异步函数,它从每个URL获取响应 . 现在显然我不能做到以下几点: async for url in ['www.google.com', 'www.youtube.com', 'www.aol.com']: 我可以使用普通的for循环但是我的代码将同步操作,我失去了 async 响应获取功... -
39 votesanswersviews
如何删除const_iterator的constness?
作为这个问题的延伸Are const_iterators faster?,我在 const_iterators 上有另一个问题 . 如何删除 const_iterator 的常量?虽然迭代器是指针的通用形式,但仍然是两个不同的东西 . 因此,我相信,我也不能使用 const_cast<> 从 const_iterator 转换为 iterator . 一种方法可能是您定义一个迭代器,... -
0 votesanswersviews
如何为向量实现std :: erase?
我有一个自定义Vec类,复制std :: vector的功能,但是我无法实现一个擦除函数,它接受与标准库实现相同的参数(并优雅地处理它们) . 具体来说,在C 11中,vector::erase具有签名 iterator erase (const_iterator position); ,其中返回的迭代器指向元素删除后元素的新位置 . 我唯一的解决方案是传递一个非const迭代器,将给定迭代器之后... -
2 votesanswersviews
如何在模板化的类中使用std :: map迭代器?
我在类模板中使用私有std :: map变量时遇到问题 . 任何人都可以解释为什么以下(简化示例)不起作用,以及我应该做什么呢? #include <iostream> #include <map> template <class T> class Container { private: typedef std::map<lon...