我在我的main方法上有以下代码,当我遍历Set并打印值时,值已经被排序.什么原因?
Set<Integer> set = new HashSet<Integer>();
set.add(2);
set.add(7);
set.add(3);
set.add(9);
set.add(6);
for(int i : set) {
System.out.println(i);
}
Run Code Online (Sandbox Code Playgroud)
输出:
2
3
6
7
9
Run Code Online (Sandbox Code Playgroud) 所以我很失望地发现JavaScript for ( var in array/object)并不等同于蟒蛇for var in list:.
在JavaScript中,您正在迭代索引本身,例如
0,
1,
2,
...
Run Code Online (Sandbox Code Playgroud)
与Python一样,您正在迭代索引指向的值,例如
"string var at index 0",
46,
"string var at index 2",
["array","of","values"],
...
Run Code Online (Sandbox Code Playgroud)
是否存在与Python的循环机制等效的标准JavaScript?
我知道for(var in object)构造意味着用于迭代字典中的键,而不是通常在数组的索引上.我问一个特定的问题,这个问题与我不关心顺序(或非常关于速度)的用例有关,并且只是不想使用while循环.
是否允许使用std::unique通过函数创建的迭代器std::make_move_iterator?我尝试了以下,并取得了成功:
#include <iostream>
#include <ostream>
#include <vector>
#include <algorithm>
#include <limits>
#include <iterator>
#include <cstdlib>
struct A
{
A() : i(std::numeric_limits< double >::quiet_NaN()) { std::cout << __PRETTY_FUNCTION__ << "\n"; }
A(double ii) : i(ii) { std::cout << __PRETTY_FUNCTION__ << "\n"; }
A(A const & a) : i(a.i) { std::cout << __PRETTY_FUNCTION__ << "\n"; }
A(A && a) : i(std::move(a.i)) { std::cout << __PRETTY_FUNCTION__ << "\n"; a.i = std::numeric_limits< double >::quiet_NaN(); }
A & operator …Run Code Online (Sandbox Code Playgroud) 我正在尝试习惯迭代器.为什么我输入
b = list(reversed([1,2,3,4,5]))
Run Code Online (Sandbox Code Playgroud)
它会给我一个反向列表,但是
c = str(reversed('abcde'))
Run Code Online (Sandbox Code Playgroud)
不会给我一个反转的字符串?
我理解fail-fast(LinkedList)和故障安全(copyonwrite)迭代器,但是弱的一致性仍然是个谜.
文档说它可能反映了底层集合的变化,但不能保证.因此,我认为弱一致性不会创建支持集合的副本.(在并发Map中,它在同一个bucketarray上工作).
我假设如果一个线程A创建了一个迭代器并且经过了一半,那么当线程B将一个项目放到数组开头的桶中时,这个更改对于线程A的迭代器是不可见的.
如果B将该项放到数组的末尾,A就会看到它.
是否可能有一个nosuchelement例外?
如果线程A创建一个迭代器,然后遍历到一个项目X,它有一个下一个项目Y,然后jvm停止线程A并恢复线程B,谁删除Y.这对线程A是否可见(我想是这样,否则并发映射将不会'是线程安全的,但对其迭代器的实现方式一无所知),因为它对线程A不可见,那么它很容易引发异常.
我遇到了许多需要迭代器的问题.通常,它们是简单的事情,您已经拥有了可以遵循的基础数据结构.其他时候,它变得更加复杂.
一个例子是使用有序遍历在没有父链接的情况下迭代BST.这要求您执行以下操作:
您可以完成工作以在hasNext()或next()中找到下一个节点.您还可以在构造函数中或第一次调用hasNext()时找到第一个节点.
我的问题
在迭代器实现中,有哪些标准或最佳实践可用于执行大部分工作?一种方式比另一种"更清洁"吗?
在std::back_insert_iterator具有value_type等于void,但它也有一个protected构件container,其保持指针到底层Container.我试图写一个traits类来提取容器value_type,沿着这些方向:
#include <iterator>
#include <type_traits>
#include <vector>
template<class OutputIt>
struct outit_vt
:
OutputIt
{
using self_type = outit_vt<OutputIt>;
using value_type = typename std::remove_pointer_t<decltype(std::declval<self_type>().container)>::value_type;
};
int main()
{
std::vector<int> v;
auto it = std::back_inserter(v);
static_assert(std::is_same<outit_vt<decltype(it)>::value_type, int>::value, "");
}
Run Code Online (Sandbox Code Playgroud)
但是,这(或多或少地预期)会遇到不完整的类型错误.反正这是为了得到容器的提取物value_type吗?
我有一个Board(aka &mut Vec<Vec<Cell>>)我想迭代它时更新.我想要更新的新值来自一个函数,它需要一个&Vec<Vec<Cell>>我正在更新的集合.
我尝试了几件事:
使用board.iter_mut().enumerate(),row.iter_mut().enumerate()以便我可以cell在最里面的循环中更新.Rust不允许调用该next_gen函数,因为它需要a,&Vec<Vec<Cell>>并且当您已经有一个可变引用时,您不能拥有不可变引用.
更改next_gen功能签名以接受a &mut Vec<Vec<Cell>>.Rust不允许对对象进行多次可变引用.
我目前正在将所有更新推迟到a HashMap,然后在我执行迭代后应用它们:
fn step(board: &mut Board) {
let mut cells_to_update: HashMap<(usize, usize), Cell> = HashMap::new();
for (row_index, row) in board.iter().enumerate() {
for (column_index, cell) in row.iter().enumerate() {
let cell_next = next_gen((row_index, column_index), &board);
if *cell != cell_next {
cells_to_update.insert((row_index, column_index), cell_next);
}
}
}
println!("To Update: {:?}", cells_to_update);
for ((row_index, column_index), cell) in …Run Code Online (Sandbox Code Playgroud) 我不确定我正在尝试编写的代码的相应数学术语.我想生成唯一整数的组合,其中每个组合的"有序子集"用于排除某些后来的组合.
希望一个例子可以说明这一点:
from itertools import chain, combinations
?
mylist = range(4)
max_depth = 3
rev = chain.from_iterable(combinations(mylist, i) for i in xrange(max_depth, 0, -1))
for el in list(rev):
print el
Run Code Online (Sandbox Code Playgroud)
该代码导致输出包含我想要的所有子集,但也包含一些我不需要的额外子集.我手动插入注释以指示我不想要的元素.
(0, 1, 2)
(0, 1, 3)
(0, 2, 3)
(1, 2, 3)
(0, 1) # Exclude: (0, 1, _) occurs as part of (0, 1, 2) above
(0, 2) # Exclude: (0, 2, _) occurs above
(0, 3) # Keep
(1, 2) # Exclude: (1, 2, _) occurs above …Run Code Online (Sandbox Code Playgroud) 我正在遍历带有auto(附加代码)的向量.在遍历时,我还在后面添加了一些元素.我没想到我得到的输出.
#include <iostream>
#include <vector>
using namespace std;
vector <int> dynamic_vector;
void access( )
{
for ( auto i : dynamic_vector ) {
if ( i == 3 ) {
dynamic_vector.push_back( 4 );
dynamic_vector.push_back( 5 );
}
cout << i << endl;
}
}
int main() {
dynamic_vector.push_back( 1 );
dynamic_vector.push_back( 2 );
dynamic_vector.push_back( 3 );
access( );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
1
2
3
Run Code Online (Sandbox Code Playgroud)
我期待从1到5的所有数字都会被打印出来.我无法理解如何遍历汽车工作?
iterator ×10
c++ ×3
java ×3
python ×3
algorithm ×1
auto ×1
c++11 ×1
c++14 ×1
collections ×1
combinations ×1
for-loop ×1
generator ×1
hashset ×1
immutability ×1
inserter ×1
iterable ×1
javascript ×1
list ×1
loops ×1
rust ×1
stl ×1
string ×1
type-traits ×1
value-type ×1
vector ×1