标签: iterator

如何用迭代器删除std :: map的元素?

我想std::map根据内容循环并删除项目.如何做到最好?

c++ iterator stdmap map

57
推荐指数
3
解决办法
8万
查看次数

更新pandas中满足特定条件的行值

说我有以下数据帧:

表

更新列featanother_feat的值的最有效方法是什么,其中2号

是这个吗?

for index, row in df.iterrows():
    if df1.loc[index,'stream'] == 2:
       # do something
Run Code Online (Sandbox Code Playgroud)

更新: 如果我有超过100列怎么办?我不想明确命名我想要更新的列.我想将每列的值除以2(流列除外).

所以要清楚我的目标是什么:

将所有值除以具有流2的所有行中的2,但不更改流列

python iterator pandas

56
推荐指数
2
解决办法
10万
查看次数

Python中的"三个点"在索引看起来像数字时是什么意思?

x [...]下面是什么意思?

a = np.arange(6).reshape(2,3)
for x in np.nditer(a, op_flags=['readwrite']):
    x[...] = 2 * x
Run Code Online (Sandbox Code Playgroud)

python iterator numpy

56
推荐指数
3
解决办法
4万
查看次数

如何遍历字符串并知道索引(当前位置)?

通常在迭代字符串(或任何可枚举对象)时,我们不仅对当前值感兴趣,还对位置(索引)感兴趣.要通过使用string::iterator我们必须维护一个单独的索引来实现这一点:

string str ("Test string");
string::iterator it;
int index = 0;
for ( it = str.begin() ; it < str.end(); it++ ,index++)
{
    cout << index << *it;
}
Run Code Online (Sandbox Code Playgroud)

上面显示的样式似乎不比'c-style'优越:

string str ("Test string");
for ( int i = 0 ; i < str.length(); i++)
{
    cout << i << str[i] ;
}
Run Code Online (Sandbox Code Playgroud)

在Ruby中,我们可以以优雅的方式获取内容和索引:

"hello".split("").each_with_index {|c, i| puts "#{i} , #{c}" }
Run Code Online (Sandbox Code Playgroud)

那么,C++中迭代可枚举对象并跟踪当前索引的最佳实践是什么?

c++ string iterator

54
推荐指数
4
解决办法
15万
查看次数

迭代时修改列表

l  = range(100)                         
for i in l:                         
    print i,                         
    print l.pop(0),                  
    print l.pop(0)
Run Code Online (Sandbox Code Playgroud)

上面的python代码给出了与预期完全不同的输出.我想循环遍历项目,以便我可以在循环时跳过项目.

请解释.

python iterator loops list

54
推荐指数
5
解决办法
8万
查看次数

使用C++ vector :: insert()添加到向量的末尾

我正在写一小段代码,我必须根据向量元素中的值将值插入某个地方的C++ STL向量中.我正在使用该insert()功能来完成此任务.我意识到,当我想在向量的末尾添加一个新元素时,我可以简单地使用push_back().但为了保持我的代码看起来不错,我想专门使用insert(),它将指向所需插入点之后的元素的迭代器和要插入的值作为输入.如果作为参数传入的迭代器的值是v.end(),v我的向量在哪里,它的工作方式是否相同push_back()

非常感谢!

c++ iterator stl vector

54
推荐指数
2
解决办法
7万
查看次数

为什么std :: begin和std :: end"不是内存安全"?

这篇博客文章中,Eric Niebler表示:

std :: begin和std :: end有什么问题?惊喜!他们不安全.考虑一下这段代码的作用:

extern std::vector<int> get_data();
auto it = std::begin(get_data());
int i = *it; // BOOM
Run Code Online (Sandbox Code Playgroud)

std :: begin对const和非const lvalues有两个重载.麻烦的是,rvalues绑定到const左值引用,导致上面的悬空迭代器.

我无法理解他的观点,为什么it是一个悬垂的参考.有人能解释一下吗

c++ iterator std c++11

54
推荐指数
3
解决办法
5486
查看次数

可变对象和数组类型提示?

我有很多函数要么为数组提供类型提示,要么用于is_array()检查变量的数组.

现在我开始使用可迭代的对象.他们实施IteratorIteratorAggregate.如果它们通过类型提示或经历,它们会被接受为数组is_array()吗?

如果我必须修改我的代码,是否有一般类型is_iterable(),或者我必须做类似的事情:

if ( is_array($var) OR $var instance_of Iterable OR $var instanceof IteratorAggregate ) { ... }
Run Code Online (Sandbox Code Playgroud)

还有哪些可迭代的接口?

php iterator type-hinting

53
推荐指数
3
解决办法
2万
查看次数

如何实现Iterable接口?

给定以下代码,如何迭代ProfileCollection类型的对象?

public class ProfileCollection implements Iterable {    
    private ArrayList<Profile> m_Profiles;

    public Iterator<Profile> iterator() {        
        Iterator<Profile> iprof = m_Profiles.iterator();
        return iprof; 
    }

    ...

    public Profile GetActiveProfile() {
        return (Profile)m_Profiles.get(m_ActiveProfile);
    }
}

public static void main(String[] args) {
     m_PC = new ProfileCollection("profiles.xml");

     // properly outputs a profile:
     System.out.println(m_PC.GetActiveProfile()); 

     // not actually outputting any profiles:
     for(Iterator i = m_PC.iterator();i.hasNext();) {
        System.out.println(i.next());
     }

     // how I actually want this to work, but won't even compile:
     for(Profile prof: m_PC) {
        System.out.println(prof);
     }
}
Run Code Online (Sandbox Code Playgroud)

java iterator

52
推荐指数
1
解决办法
10万
查看次数

可移动性如何减少结束迭代器?

刚刚end()在我的公司源代码中遇到了迭代器的减少,对我来说它看起来很奇怪.据我所知,这是在一些平台上工作,但不适用于其他平台.也许我错了,但是在标准方面我找不到任何有用的东西.标准只表示end()返回一个迭代器,它是一个过去的结束值,但它是否保证可递减?这样的代码如何符合标准?

std::list<int>::iterator it = --l.end();
Run Code Online (Sandbox Code Playgroud)

提前致谢.

c++ portability iterator stl

52
推荐指数
2
解决办法
2万
查看次数

标签 统计

iterator ×10

c++ ×5

python ×3

stl ×2

c++11 ×1

java ×1

list ×1

loops ×1

map ×1

numpy ×1

pandas ×1

php ×1

portability ×1

std ×1

stdmap ×1

string ×1

type-hinting ×1

vector ×1