Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {.passL:"-lstdc++".}
- #{.push dynlib:"libstd++.so".}
- {.push header:"<utility>".}
- type
- CppPair*[T,P] {.importcpp:"std::pair"} = object
- proc makePair*[T,P](a:T; b:P):CppPair[T,P] {.importcpp:"std::make_pair(@)" .}
- proc first*[T,P](p:CppPair[T,P]):T {.importcpp:"#.first" .}
- proc second*[T,P](p:CppPair[T,P]):P {.importcpp:"#.second" .}
- {.pop.}
- {.push header:"<map>"}
- type
- CppMap*[T,P] {.importcpp:"std::map".} = object
- CppMapIterator* {.importcpp: "std::map<'0,'1>::iterator".} [K,V] = object
- proc insert*[T,P](self:CppMap[T,P],val:CppPair[T,P]) {.importcpp:"#.insert(@)" .}
- proc `[]=`*[K,V](self:var CppMap[K,V],key:K, val:V) {.importcpp:"#[#] = #" .}
- # Iterators
- proc begin*[K,V](x: CppMap[K,V]): CppMapIterator[K,V] {.importcpp: "begin".}
- proc `end`*[K,V](x: CppMap[K,V]): CppMapIterator[K,V] {.importcpp: "end".}
- {.pop.}
- {.push header:"<iterator>".}
- proc first*[K,V](self:CppMapIterator[K,V]):K {.importcpp:"@->first" .}
- proc second*[K,V](self:CppMapIterator[K,V]):V {.importcpp:"@->second" .}
- proc inc*[K,V](self:var CppMapIterator[K,V]) {.importcpp:"#.operator++()" .} #<<<<< ISSUE
- {.pop.}
- iterator pairs*[K,V](self:CppMap[K,V]):tuple[key:K,value:V] =
- var result:tuple[key:K,value:V]
- var ini:CppMapIterator[K,V] = self.begin()
- var tmp:ptr CppMapIterator[K,V]
- echo (ini.first,ini.second)
- yield (ini.first,ini.second)
- inc(ini) # <<<< ISSUE
- echo (ini.first,ini.second)
- yield (ini.first,ini.second)
- #================
- type
- PersonMap = CppMap[string,int]
- Person = CppPair[string,int]
- var mymap:PersonMap
- var tmp1:Person = makePair("jose", 10)
- mymap.insert(tmp1)
- mymap.insert( makePair("ana", 20) )
- mymap.insert( makePair("maria", 30) )
- mymap["jose"] = 15
- var ini:CppMapIterator[string,int] = mymap.begin()
- var fin = mymap.`end`()
- echo ini.first
- echo ini.second
- for i in mymap.pairs():
- echo i.key
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement