For Each (set)

From SwiftAPI

Jump to: navigation, search

Contents

C++

#include <set>
using namespace std;
 
int values[] = {1, 2, 4, 8};
set<int> s(values, values+4);
 
int n = 0;
for (set<int>::iterator iter = s.begin(); iter != s.end(); iter++) {
    n = n + *iter;
}
// n == 15

Java

Java SE 5.0+:

Set<Integer> s = new HashSet<Integer>(Arrays.asList(1, 2, 4, 8));
int n = 0;
for (Integer x : s) {
    n = n + x;
}
// n == 15

Objective-C

NSSet *s = [NSSet setWithObjects:@1, @2, @4, @8, nil];
int n = 0;
for (NSNumber *x in s) {
  n = n + x.intValue;
}
// n == 15

Python

s = set([1, 2, 4, 8])
n = 0
for x in s:
    n = n + x
# n == 15

Ruby

Ruby-Doc.org

require 'set'
 
s = Set.new([1, 2, 4, 8])
n = 0
s.each do |x|
  n = n + x
end
# n == 15

Swift

let s : Set<Int> = [1, 2, 4, 8]
var n = 0
for x in s {
  n = n + x
}
// n == 15
Personal tools