Firstly, I am quickly looking through C ++ style iterators. For instance:
vector<int> v;
. . .
for (vector<int>::iterator it = v.begin(); it!=v.end(); ++it) {
cout << *it << endl;
}
He is flexible. Easy to change basic container types. For example, later you may decide that the number of insertions and deletions is so large that the list will be more efficient than the vector. It also has many useful member functions. Many of the member functions for vector use iterators, for example, are assigned, inserted, or erased. Moreover, we can use an iterator (if supported) bidirectional, for example, ++, -. This is useful for analyzing a stream similar to objects.
Python problems: 1: Currently, python for loop syntax is less flexible than C ++. (well, safer) 2: instead of the style "it! = Iter.end ()", python will throw an exception if next () has no more. This is not flexible.
Question 1: Is my idea above correct?
OK This begs my question, how to implement a more powerful python iterator as powerful as C ++ iterators? The syntax for python for loop is currently less flexible than C ++ for. I also found some possible solutions, such as http://www.velocityreviews.com/forums/t684406-pushback-iterator.html . but it asks the user push_back, not asking iterator -.
2: python? http://www.cplusplus.com/reference/std/iterator/BidirectionalIterator/.
:
it = v.begin();
while( it!=v.end()) {
//do sth here
if (condition1)
++it;//suppose this iterator supports ++
if(condition2)
--it;//suppose this iterator supports --
}
: 1) , 2) "". "++" "-" ( ).
,
:
:
i = 0
while i < len(sequence): # or i < len and some_other_condition
star_it = sequence[i]
if condition_one(star_it):
i += 1
if condition_two(star_it):
i = max(i - 1, 0)
, , O (n). , "list" python , . , . ++ " ", " ". ? .