You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/Var/container_and_sequence_operations.md
+47-32Lines changed: 47 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,41 +39,56 @@ This page documents all user-facing container and sequence APIs for `var`, in a
39
39
## Examples
40
40
41
41
```cpp
42
-
#include"pythonic/pythonic.hpp"
42
+
#include<pythonic/pythonic.hpp>
43
43
usingnamespacepy;
44
44
45
-
var vlist = list(1, 2, 3);
46
-
vlist.append(4); // [1,2,3,4]
47
-
vlist.extend(list(5,6)); // [1,2,3,4,5,6]
48
-
vlist.remove(2); // [1,3,4,5,6]
49
-
print(vlist.len()); // 5
50
-
print(vlist.front()); // 1
51
-
print(vlist.back()); // 6
52
-
print(vlist.at(2)); // 4
53
-
print(vlist[1]); // 3
54
-
print(vlist.slice(1,4)); // [3,4,5]
55
-
print(vlist(1,4)); // [3,4,5]
56
-
print(vlist.empty()); // false
57
-
vlist.clear();
58
-
print(vlist.empty()); // true
59
-
60
-
var vset = set(1,2,3);
61
-
vset.add(4);
62
-
vset.update(set(5,6));
63
-
print(vset.contains(3)); // true
64
-
print(vset.has(5)); // true
65
-
vset.remove(1);
66
-
67
-
var vdict = dict({{"a", 1}, {"b", 2}});
68
-
print(vdict["a"]); // 1
69
-
print(vdict.keys()); // ["a", "b"]
70
-
print(vdict.values()); // [1,2]
71
-
print(vdict.items()); // [("a",1), ("b",2)]
72
-
73
-
// Iteration
74
-
for (auto it = vdict.begin(); it != vdict.end(); ++it) {
75
-
// ...
45
+
intmain()
46
+
{
47
+
var vlist = list(1, 2, 3);
48
+
vlist.append(4); // [1,2,3,4]
49
+
vlist.extend(list(5,6)); // [1,2,3,4,5,6]
50
+
vlist.remove(2); // [1,3,4,5,6]
51
+
print(vlist.len()); // 5
52
+
print(vlist.front()); // 1
53
+
print(vlist.back()); // 6
54
+
print(vlist.at(2)); // 4
55
+
print(vlist[1]); // 3
56
+
print(vlist.slice(1,4)); // [3,4,5]
57
+
print(vlist(1,4)); // [3,4,5]
58
+
print(vlist.empty()); // false
59
+
vlist.clear();
60
+
print(vlist.empty()); // true
61
+
62
+
var vset = set(1,2,3);
63
+
vset.add(4);
64
+
vset.update(set(5,6));
65
+
print(vset.contains(3)); // true
66
+
print(vset.has(5)); // true
67
+
vset.remove(1);
68
+
69
+
var vdict = dict({{"a", 1}, {"b", 2}});
70
+
print(vdict["a"]); // 1
71
+
print(vdict.keys()); // ["a", "b"]
72
+
print(vdict.values()); // [1,2]
73
+
print(vdict.items()); // [("a",1), ("b",2)]
74
+
for (auto it = vdict.get<Dict>().begin(); it != vdict.get<Dict>().end(); ++it)
75
+
{
76
+
print("key: ", it->first); // key (std::string)
77
+
print("value: ", it->second); // value (var)
78
+
}
79
+
for (auto [key,value] : vdict.get<Dict>())
80
+
{
81
+
print("key: ", key); // key (std::string)
82
+
print("value: ", value); // value (var)
83
+
}
84
+
for (auto it = vdict.begin(); it != vdict.end(); ++it)
85
+
{
86
+
print(1);
87
+
// but you can't use it->first and it->second here as the vdict is not a std::map. also you can not do auto[key,value] like iteration directly on vdict, you have to use vdict.get<Dict>() to get the underlying std::map
0 commit comments