I want to swap random elements of a map, but I don't seem to be able to access a map other than by key.
It doesn't matter which elements are swapped, as long as there are random elements swapped.
#include<iostream>
#include <map>
usingnamespace std;
int main()
{
map <constchar*, int> List;
List["ene"] = 1;
List["tweede"] = 2;
List["derde"] = 3;
List["vierde"] = 4;
List["vijfde"] = 5;
// I want to swap the 3th and 4th item, but I don't want to access them by
// key, but like it was an array.
swap(List.begin()+3, List.begin()+4);
for(map<constchar*, int>::iterator iter = List.begin(); iter != List.end(); iter++ ) {
cout << (*iter).first << " is " << (*iter).second << endl;
}
}
there some problem with you code I think:
First :
the map type's iterator doesn't support the " + n" operator, the only why to change it is "++";
Second:
Even you tried ++ three times, but according to the template declaration
template <class T> void swap ( T& a, T& b );
the T is a typename , so it only changed the iterator only , the function do nothing
Third :
If you tried sway(* iterator, * interator), then compiler will show you a error, that the can not use the default assignment operator...
That makes no sense. The whole purpose of a map is to associate a key and value(s). Other languages call it a "dictionary" or "associative array." There is no "index" order.
Your only two options are to
1. do as adir suggests and iterate through the map, element by element, or
2. do as I suggest and know the keys for the elements you want.
If neither of these work, perhaps you are using the wrong data type. What exactly are you trying to do with this?
maps guarantee O(log N) lookups by storing elements in a tree in order.
You can't just go and change elements willy-nilly in a map. If you really need to do that, then map isn't the right container.
If you want to be able to do "iterator + N" then you need a container that supports random access iterators. vector would fit the bill there.
The order is only defined internally to be a linear, strict-weak, increasing ordered set. From the user's point of view, there is no order. He shouldn't care.
Further, exchanging the pairs' seconds doesn't do anything to the map's internal storage. (Copy constructors work wonders.) It is not incongruous to modify a key's associated value. That is the purpose of a map: access to storage of mutable data via some form of unique key.
Lastly, maps are unique among all STL associative containers in that they do implement the [] operator.
Oops, I knew I'd get flamed for my response because I realized last night while I was caulking my bathroom sink that you were trying to swap associated values, not keys, in which case the original problem could be solved using std::advance().
jsmith, after I added the missing bracket and tested your code I got the following output.
ene is 1
tweede is 2
derde is 3
vierde is 4
vijfde is 5
It seems like when constructing an iterator a copy of the map is made and changes made to the copy don't affect the map itself.
One solution is keeping track of all the keys with a std::list is the only solution.
Another one is using 2 lists where one works as a key list.
Another solution is iterating through the keys a random number of times but creating a new iterator every step would be to slow.
Oops. Notice in my code above lines 20 and 21 advance the iterators the same amount... That's why things didn't appear to change. Try changing one of the 3's to 2 or 4.