Content deleted Content added
m v2.05b - Bot T20 CW#61 - Fix errors for CW project (Reference before punctuation) |
Add Rust example |
||
Line 6:
In C++ one can emulate indexing by overloading the {{c++|[]}} operator. The expression {{c++|a[b...]}} translates to a call to the user-defined function {{c++|operator[]}} as {{c++|(a).operator[](b...)}}.<ref>{{cite web|url=https://en.cppreference.com/w/cpp/language/operators|title=C++ operator overloading}}</ref> Here is an example,
<syntaxhighlight lang="c++">
struct vector {
int size; double* data;
vector(int n) { size = n; data = new double[n](); }
~vector(){ size = 0; delete[] data; }
double& operator[](int i) { return data[i]; }
};
#include<iostream>
int main() {
vector v(3);
for(int i = 0; i < v.size; i++) v[i] = i + 1;
for(int i = 0; i < v.size; i++) std::cout << v[i] << "\n";
return 0;
}
Line 26:
==== Example 1====
<syntaxhighlight lang="csharp">
public class
{ private double[] data;
public
public int size => data.Length;
public double this[int i] { get => data[i]; set => data[i] = value; }
public static void Main()
for (int i = 0; i < v.size; i++)
for (int i = 0; i < v.size; i++) System.Console.WriteLine(v[i]);
▲ }
}
}
</syntaxhighlight>
Line 98 ⟶ 100:
=== PHP ===
In [[PHP]] indexing can be implemented via the predefined {{
<syntaxhighlight lang="php">
<?php
class
function __construct(int $n) {
$this->size = $n;
$this->data = array_fill(0, $n, 0);
}
public function offsetGet($offset): mixed {
return $this->data[$offset];
}
public function offsetSet($offset, $value): void {
$this->data[$offset] = $value;
}
public function offsetExists($offset): bool {}
public function offsetUnset($offset): void {}
}
$v = new
for ($i = 0; $i < $v->size; $i++) $v[$i] = $i + 1;
for ($i = 0; $i < $v->size; $i++) print "{$v[$i]}\n";
?>
</syntaxhighlight>
=== Python ===
In [[Python (programming language)|Python]] one implements indexing by overloading the {{python|__getitem__}} and {{python|__setitem__}} methods,
<syntaxhighlight lang="python">
import array
class
def __init__
self.size = n
self.data = array.array(
def __getitem__
def __setitem__
v = Vector(3)
</syntaxhighlight>
=== Rust ===
[[Rust (programming language)|Rust]] provides the {{Mono|std::ops::Index}} trait.<ref>{{cite web |title=Index in std::ops - Rust |url=https://doc.rust-lang.org/std/ops/trait.Index.html |website=doc.rust-lang.org |access-date=11 January 2025}}</ref>
<syntaxhighlight lang="rust">
use std::ops::Index;
enum Nucleotide {
A,
C,
G,
T,
}
struct NucleotideCount {
a: usize,
c: usize,
g: usize,
t: usize,
}
impl Index<Nucleotide> for NucleotideCount {
type Output = usize;
fn index(&self, nucleotide: Nucleotide) -> &Self::Output {
match nucleotide {
Nucleotide::A => &self.a,
Nucleotide::C => &self.c,
Nucleotide::G => &self.g,
Nucleotide::T => &self.t,
}
}
}
let nucleotide_count = NucleotideCount {a: 14, c: 9, g: 10, t: 12};
assert_eq!(nucleotide_count[Nucleotide::A], 14);
▲for i in range(v.size) : v[i]=i+1
assert_eq!(nucleotide_count[Nucleotide::C], 9);
▲for i in range(v.size) : print(v[i])
assert_eq!(nucleotide_count[Nucleotide::G], 10);
assert_eq!(nucleotide_count[Nucleotide::T], 12);
</syntaxhighlight>
Line 143 ⟶ 184:
{{Reflist}}
[[Category:Articles with example C++ code]]
[[Category:Articles with example C Sharp code]]
[[Category:Articles with example Python (programming language) code]]
[[Category:Articles with example PHP code]]
[[Category:Articles with example Rust code]]
[[Category:Programming language topics]]
[[Category:Object-oriented programming]]
|