cppstl/std_vector

Search:
Group by:

Types

CppVector[T] {.importcpp: "std::vector".} = object
CppVectorConstIterator[T] {.importcpp: "std::vector<\'0>::const_iterator".} = CppConstIterator[
    T]
CppVectorIterator[T] {.importcpp: "std::vector<\'0>::iterator".} = CppIterator[T]

Procs

proc `!=`[T](a: CppVector[T]; b: CppVector[T]): bool {.importcpp: "(# != #)",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}

Return true if the contents of lhs and rhs are not equal, that is, either they do not have the same number of elements, or one of the elements in lhs does not compare equal with the element in rhs at the same position.

https://en.cppreference.com/w/cpp/container/CppVector/operator_cmp

Example:

let v1 = @[1, 2, 3].toCppVector()
var
  v2 = v1
  v3 = v1
v2.add(4)
doAssert v2 != v1

v3[0] = 100
doAssert v3 != v1
proc `$`[T](v: CppVector[T]): string
The $ operator for CppVector type variables. This is used internally when calling echo on a CppVector type variable.

Example:

var v = initCppVector[int]()
doAssert $v == "[]"

v.add(100)
v.add(200)
doAssert $v == "[100, 200]"
proc `<`[T](a: CppVector[T]; b: CppVector[T]): bool {.importcpp: "(# < #)",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}

Return true if a is lexicographically less than b.

https://en.cppreference.com/w/cpp/container/CppVector/operator_cmp

Example:

let v1 = @[1, 2, 3].toCppVector()
var v2 = v1
doAssert not (v1 < v2)

v2.add(4)
doAssert v1 < v2

v2[2] = 0
doAssert v2 < v1
proc `<=`[T](a: CppVector[T]; b: CppVector[T]): bool {.importcpp: "(# <= #)",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}

Return true if a is lexicographically less than or equal to b.

https://en.cppreference.com/w/cpp/container/CppVector/operator_cmp

Example:

let v1 = @[1, 2, 3].toCppVector()
var v2 = v1
doAssert v1 <= v2

v2.add(4)
doAssert v1 <= v2

v2[2] = 0
doAssert v2 <= v1
proc `==`[T](a: CppVector[T]; b: CppVector[T]): bool {.importcpp: "(# == #)",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}

Return true if the contents of lhs and rhs are equal, that is, they have the same number of elements and each element in lhs compares equal with the element in rhs at the same position.

https://en.cppreference.com/w/cpp/container/CppVector/operator_cmp

Example:

let
  v1 = @[1, 2, 3].toCppVector()
  v2 = v1
doAssert v1 == v2
proc `>`[T](a: CppVector[T]; b: CppVector[T]): bool {.importcpp: "(# > #)",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}

Return true if a is lexicographically greater than b.

https://en.cppreference.com/w/cpp/container/CppVector/operator_cmp

Example:

let v1 = @[1, 2, 3].toCppVector()
var v2 = v1
doAssert not (v2 > v1)

v2.add(4)
doAssert v2 > v1

v2[2] = 0
doAssert v1 > v2
proc `>=`[T](a: CppVector[T]; b: CppVector[T]): bool {.importcpp: "(# >= #)",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}

Return true if a is lexicographically greater than or equal to b.

https://en.cppreference.com/w/cpp/container/CppVector/operator_cmp

Example:

let v1 = @[1, 2, 3].toCppVector()
var v2 = v1
doAssert v2 >= v1

v2.add(4)
doAssert v2 >= v1

v2[2] = 0
doAssert v1 >= v2
proc `[]`[T](self: CppVector[T]; idx: Natural): lent T {.inline.}
Return the reference to self[idx].
proc `[]`[T](self: var CppVector[T]; idx: Natural): var T {.inline.}
Return the reference to self[idx].

Example:

var v = initCppVector[char]()
v.add('a')
v.add('b')
v.add('c')

v[1] = 'z'
doAssert v[0] == 'a'
doAssert v[1] == 'z'
doAssert v[2] == 'c'
proc `[]=`[T](self: var CppVector[T]; idx: Natural; val: T) {.inline.}
Set the value at v[idx] to the specified value val.

Example:

var v = initCppVector[int](2)
doAssert v.toSeq() == @[0, 0]

v[0] = -1
doAssert v.toSeq() == @[-1, 0]
proc add[T](v: var CppVector[T]; elem: T) {.inline.}
Alias for pushBack proc.

Example:

var v = initCppVector[int]()
doAssert v.len() == 0

v.add(100)
v.add(200)
doAssert v.len() == 2
proc assign[T](first: CppVectorIterator[T]; last: CppVectorIterator[T]) {.
    importcpp: "assign", header: "<vector>", ...raises: [], tags: [], forbids: [].}
proc assign[T](n: csize_t; val: T) {.importcpp: "assign", header: "<vector>",
                                     ...raises: [], tags: [], forbids: [].}
proc assign[T](v: var CppVector[T]; num: csize_t; val: T) {.importcpp: "assign",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}

Return a CppVector with num elements assigned to the specified value val.

https://en.cppreference.com/w/cpp/container/CppVector/assign

Example:

var v: CppVector[float]

v.assign(5, 1.0)
doAssert v.toSeq() == @[1.0, 1.0, 1.0, 1.0, 1.0]

v.assign(2, 2.3)
doAssert v.toSeq() == @[2.3, 2.3]
proc at[T](self: CppVector[T]; n: csize_t): T {.importcpp: "at",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}
proc at[T](self: var CppVector[T]; n: csize_t): var T {.importcpp: "at",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}
proc back[T](v: CppVector[T]): T {.importcpp: "back", header: "<vector>",
                                   ...raises: [], tags: [], forbids: [].}
proc back[T](v: var CppVector[T]): var T {.importcpp: "back",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}

Return the reference to the last element of the CppVector.

This has an alias proc last.

https://www.cplusplus.com/reference/CppVector/CppVector/back/

Example:

var v = initCppVector[int]()

v.add(100)
v.add(200)
doAssert v.back() == 200

v.back() = 300
doAssert v.back() == 300
proc begin[T](v: CppVector[T]): CppVectorIterator[T] {.importcpp: "begin",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}

Return a mutable C++ iterator pointing to the beginning position of the CppVector.

https://www.cplusplus.com/reference/CppVector/CppVector/begin/

Example:

var v = @[1, 2, 3].toCppVector()
discard v.insert(v.begin(), 100)
doAssert v.toSeq() == @[100, 1, 2, 3]
proc capacity[T](self: CppVector[T]): csize_t {.importcpp: "capacity",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}
proc cBegin[T](v: CppVector[T]): CppVectorConstIterator[T] {.
    importcpp: "cbegin", header: "<vector>", ...raises: [], tags: [], forbids: [].}

Return an immutable C++ iterator pointing to the beginning position of the CppVector.

https://www.cplusplus.com/reference/CppVector/CppVector/begin/

Example:

var v = @[1, 2, 3].toCppVector()
discard v.insert(v.cBegin(), 100)
doAssert v.toSeq() == @[100, 1, 2, 3]
proc cEnd[T](v: CppVector[T]): CppVectorConstIterator[T] {.importcpp: "cend",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}

Return an immutable C++ iterator pointing to after the end position of the CppVector.

https://www.cplusplus.com/reference/CppVector/CppVector/end/

Example:

var v = @[1, 2, 3].toCppVector()
discard v.insert(v.cEnd(), 100)
doAssert v.toSeq() == @[1, 2, 3, 100]
proc clear[T](self: var CppVector[T]) {.importcpp: "clear", header: "<vector>",
                                        ...raises: [], tags: [], forbids: [].}
proc crBegin[T](x: CppVector[T]): CppVectorConstIterator[T] {.
    importcpp: "crbegin", header: "<vector>", ...raises: [], tags: [], forbids: [].}
proc crEnd[T](x: CppVector[T]): CppVectorConstIterator[T] {.importcpp: "crend",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}
proc data[T](self: CppVector[T]): ptr T {.importcpp: "data", header: "<vector>",
    ...raises: [], tags: [], forbids: [].}
proc empty(v: CppVector): bool {.importcpp: "empty", header: "<vector>",
                                 ...raises: [], tags: [], forbids: [].}

Check if the CppVector is empty i.e. has zero elements.

https://en.cppreference.com/w/cpp/container/CppVector/empty

Example:

var v = initCppVector[int]()
doAssert v.empty()

v.add(100)
doAssert not v.empty()
proc `end`[T](v: CppVector[T]): CppVectorIterator[T] {.importcpp: "end",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}

Return a mutable C++ iterator pointing to after the end position of the CppVector.

https://www.cplusplus.com/reference/CppVector/CppVector/end/

Example:

var v = @[1, 2, 3].toCppVector()
discard v.insert(v.`end`(), 100)
doAssert v.toSeq() == @[1, 2, 3, 100]
proc erase[T](self: var CppVector[T]; first, last: CppVectorConstIterator[T]): CppVectorIterator[
    T] {.importcpp: "erase", header: "<vector>", ...raises: [], tags: [],
         forbids: [].}
proc erase[T](self: var CppVector[T]; position: CppVectorConstIterator[T]): CppVectorIterator[
    T] {.importcpp: "erase", header: "<vector>", ...raises: [], tags: [],
         forbids: [].}
proc first[T](v: CppVector[T]): T {.inline.}
Alias for front proc.
proc first[T](v: var CppVector[T]): var T {.inline.}
Alias for front proc.

Example:

var v = initCppVector[int]()

v.add(100)
v.add(200)
doAssert v.first() == 100

v.first() = 300
doAssert v.first() == 300
proc front[T](v: CppVector[T]): T {.importcpp: "front", header: "<vector>",
                                    ...raises: [], tags: [], forbids: [].}
proc front[T](v: var CppVector[T]): var T {.importcpp: "front",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}

Return the reference to the first element of the CppVector.

This has an alias proc first.

https://en.cppreference.com/w/cpp/container/CppVector/front

Example:

var v = initCppVector[int]()

v.add(100)
v.add(200)
doAssert v.front() == 100

v.front() = 300
doAssert v.front() == 300
proc initCppVector[T](): CppVector[T] {.constructor,
                                        importcpp: "std::vector<\'*0>(@)",
                                        header: "<vector>".}
proc initCppVector[T](first, last: CppVectorConstIterator[T]): CppVector[T] {.
    constructor, importcpp: "std::vector<\'*0>(@)", header: "<vector>".}
proc initCppVector[T](n: csize_t): CppVector[T] {.constructor,
    importcpp: "std::vector<\'*0>(@)", header: "<vector>".}
proc initCppVector[T](n: csize_t; val: T): CppVector[T] {.constructor,
    importcpp: "std::vector<\'*0>(@)", header: "<vector>".}
proc initCppVector[T](x: CppVector[T]): CppVector[T] {.constructor,
    importcpp: "std::vector<\'*0>(@)", header: "<vector>".}
proc insert[T](v: var CppVector[T];
               position, first, last: CppVectorConstIterator[T]): CppVectorIterator[
    T] {.importcpp: "insert", header: "<vector>", ...raises: [], tags: [],
         forbids: [].}
Insert elements from range first ..< last before the specified position.

Example:

let v1 = @['a', 'b'].toCppVector()
var v2: CppVector[char]
discard v2.insert(v2.cBegin(), v1.cBegin(), v1.cEnd())
doAssert v2.toSeq() == @['a', 'b']
proc insert[T](v: var CppVector[T]; position: CppVectorConstIterator[T];
               count: csize_t; val: T): CppVectorIterator[T] {.
    importcpp: "insert", header: "<vector>", ...raises: [], tags: [], forbids: [].}
Insert count copies of element before the specified position.

Example:

var v = @['a', 'b'].toCppVector()
discard v.insert(v.cBegin(), 3, 'c')
doAssert v.toSeq() == @['c', 'c', 'c', 'a', 'b']
proc insert[T](v: var CppVector[T]; position: CppVectorConstIterator[T]; val: T): CppVectorIterator[
    T] {.importcpp: "insert", header: "<vector>", ...raises: [], tags: [],
         forbids: [].}
Insert an element before the specified position.

Example:

var v = @['a', 'b'].toCppVector()
discard v.insert(v.cBegin(), 'c')
doAssert v.toSeq() == @['c', 'a', 'b']
proc last[T](v: CppVector[T]): T {.inline.}
Alias for back proc.
proc last[T](v: var CppVector[T]): var T {.inline.}
Alias for back proc.

Example:

var v = initCppVector[int]()

v.add(100)
v.add(200)
doAssert v.last() == 200

v.last() = 300
doAssert v.last() == 300
proc len(v: CppVector): csize_t {.inline.}
Alias for size proc.
proc maxSize[T](self: CppVector[T]): csize_t {.importcpp: "max_size",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}
proc popBack[T](v: var CppVector[T]) {.importcpp: "pop_back",
                                       header: "<vector>", ...raises: [], tags: [],
                                       forbids: [].}

Remove the last element of the CppVector. This proc does not return anything.

https://www.cplusplus.com/reference/CppVector/CppVector/pop_back/

Example:

var v = initCppVector[int]()
doAssert v.len() == 0

v.add(100)
doAssert v.len() == 1

v.popBack()
doAssert v.len() == 0
proc pushBack[T](v: var CppVector[T]; elem: T) {.importcpp: "push_back",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}

Append a new element to the end of the CppVector.

This has an alias proc add.

https://en.cppreference.com/w/cpp/container/CppVector/push_back

Example:

var v = initCppVector[int]()
doAssert v.len() == 0

v.pushBack(100)
v.pushBack(200)
doAssert v.len() == 2
proc rBegin[T](x: CppVector[T]): CppVectorIterator[T] {.importcpp: "rbegin",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}
proc rEnd[T](x: CppVector[T]): CppVectorIterator[T] {.importcpp: "rend",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}
proc reserve[T](self: var CppVector[T]; n: csize_t) {.importcpp: "reserve",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}
proc resize[T](self: CppVector[T]; n: csize_t) {.importcpp: "resize",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}
proc shrinkToFit[T](self: var CppVector[T]) {.importcpp: "shrink_to_fit",
    header: "<vector>", ...raises: [], tags: [], forbids: [].}
proc size(v: CppVector): csize_t {.importcpp: "size", header: "<vector>",
                                   ...raises: [], tags: [], forbids: [].}

Return the number of elements in the CppVector.

This has an alias proc len.

https://en.cppreference.com/w/cpp/container/CppVector/size

Example:

var v = initCppVector[int]()
doAssert v.size() == 0

v.add(100)
v.add(200)
doAssert v.len() == 2
doAssert v.size() == 2
proc swap[T](v1, v2: var CppVector[T]) {.importcpp: "swap", header: "<vector>",
    ...raises: [], tags: [], forbids: [].}

Swap the contents of vectors v1 and v2.

https://en.cppreference.com/w/cpp/container/CppVector/swap

Example:

var
  v1 = @[1, 2, 3].toCppVector()
  v2 = @[7, 8, 9].toCppVector()
v1.swap(v2)
doAssert v1.toSeq() == @[7, 8, 9]
doAssert v2.toSeq() == @[1, 2, 3]
proc toCppVector[T](s: openArray[T]): CppVector[T]
Convert an array/sequence to a CppVector.

Example:

let
  s = @[1, 2, 3]
  a = [1, 2, 3]

doAssert s.toCppVector().toSeq() == s
doAssert a.toCppVector().toSeq() == s
proc toSeq[T](v: CppVector[T]): seq[T]
Convert a CppVector to a sequence.

Example:

var v: CppVector[char]
v.assign(3, 'k')

doAssert v.toSeq() == @['k', 'k', 'k']

Iterators

iterator items[T](v: CppVector[T]): T
Iterate over all the elements in CppVector v.

Example:

var
  v: CppVector[int]
  sum: int

v.assign(3, 5)

for elem in v:
  sum += elem
doAssert sum == 15
iterator pairs[T](v: CppVector[T]): (csize_t, T)
Iterate over (index, value) for all the elements in CppVector v.

Example:

var
  v: CppVector[int]
  sum: int

v.assign(3, 5)

for idx, elem in v:
  sum += idx.int + elem
doAssert sum == 18

Converters

converter CppVectorIteratorToCppVectorConstIterator[T](x: CppVectorIterator[T]): CppVectorConstIterator[
    T] {.importcpp: "#", ...raises: [], tags: [], forbids: [].}
Implicitly convert mutable C++ iterator to immutable C++ iterator.

Templates

template constIterator[T](_: typedesc[CppVector[T]]): typedesc[
    CppVectorConstIterator[T]]
template `iterator`[T](_: typedesc[CppVector[T]]): typedesc[CppVectorIterator[T]]