-1

Suppose I have objects in range (r1,r2] I want to insert as keys to a map. They have no iterator/next defined, but have a less than operator. How would I do this:

   template< class K, class V>
   void foo( K r1, K r2, V val ) {
        for(K key = r1; key < r2; ++key ) 
        { 
             mMap.insert(make_pair(key,val)); 
        }
   }

I cannot use ++key.

K could be any type which is copyable, assignable, has opertor< but no equality operator and arithmetic operators.

Oli
  • 1,313
  • 14
  • 31
  • How about `std::next`? – aschepler Nov 29 '16 at 16:08
  • for that I will need iterator of key `K`. – Oli Nov 29 '16 at 16:09
  • 1
    A `std::map` always has iterators. I'm not sure what you're asking. – aschepler Nov 29 '16 at 16:10
  • There is not enough info - do you know exactly what are all the keys r1-r2 and you have them in a list? otherwise how would you know how they look like anyway? – kabanus Nov 29 '16 at 16:15
  • I have updated the question with example – Oli Nov 29 '16 at 16:15
  • See my comment above. It's not clear how would you know what the next key is, unless you already have them all in a list. Be concrete, what are the keys? – kabanus Nov 29 '16 at 16:16
  • Your question is unanswerable without knowing more about type K. What does it mean to iterate through an object of this type? – Benjamin Lindley Nov 29 '16 at 16:21
  • Try and answer this question: Given k1 and k7, the start and end of the range, how would you know k2? If you can't answer this for yourself, than we can't help. – kabanus Nov 29 '16 at 16:26
  • So you want to know how to get the next object in a set when no next function is defined? I'm going to say the answer is no, you can't. – aschepler Nov 29 '16 at 16:27
  • I posted this question because the specifications given are very limited and I didn't come up with idea to get it done. – Oli Nov 29 '16 at 16:30
  • Please check: http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem, this isn't what you meant to ask, and can't be done. – kabanus Nov 29 '16 at 16:30
  • No equality operator means you will never know what the "end" of a range of `K`'s will be. Furthermore, given two `K`'s, it's not guaranteed that they form a total order; could be that the set of all `K` is a pre-order or partial order. So basically the notion of ranges of `K` is undefined. I agree with @kabanus that your problem is higher up the call chain. – rwols Nov 29 '16 at 16:32
  • End of range is solved by `opertor<` which is available. The problem is going to next element. – Oli Nov 29 '16 at 16:34

2 Answers2

1

The iterability of a key is irrelevant, the map implements the iteration over the set of keys. See something close in Iterate through a HashMap:

Map<String, Object> map = ...;

for (String key : map.keySet()) {
    // ...
}

or closer in Iterate keys in a C++ map:

for(std::map<Key,Val>::iterator iter = myMap.begin(); iter != myMap.end(); ++iter)
{

}
Community
  • 1
  • 1
kabanus
  • 24,623
  • 6
  • 41
  • 74
  • ... or just `for (const auto& keyvalue : myMap) { ... keyvalue.first ... keyvalue.second ... }` – rwols Nov 29 '16 at 16:12
  • Actually I want to insert keys from range `(r1, r2]`. for(K key = r1; key < r2; ++key ) { map.insert(make_pair(key,val)); } I cannot use `++key`. – Oli Nov 29 '16 at 16:13
  • Thanks, I'm a bit rusty - and not completely swimming through the new syntax. @has, your question is very unclear. Remove any references to a map in your question, and just ask how to iterate through a range of object with no iteration defined. – kabanus Nov 29 '16 at 16:14
  • @has if `K` is not iterable, then it makes no sense to talk about a range of `K`'s. You're going to have to tell us more about your `K` type. – rwols Nov 29 '16 at 16:26
0

You can use the iterator from std::map class

you get it with Begin

you can write a for loop with that iterator from type std::map<type,type>::iterator and then use all your operators as expected

Dropye
  • 214
  • 3
  • 18