Map index manipulation help. (for 2D image layers)

All I want to know is how can I put index position of the pair in a map with insert function and how can I get position of that pair. I thought map is easy for sorting index like vector but map have something like "tree iterator".

Here is my code for std::vector and it's working fine:

1
2
3
4
5
6
7
8
9
10
11
12
13
	std::vector<std::string> v;

	v.push_back("a");
	v.push_back("b");
	v.push_back("c");

	v.insert(v.begin() + 1, "d");

	for (unsigned int i = 0; i < v.size(); i++) {

		std::cout << v[i] << std::endl;

	}


PRINTS FINE:
a
d
b
c

And here is similar code for std::map and it's not working:

1
2
3
4
5
6
7
8
9
10
11
12
13
	std::map<std::string, std::string> scene_objects;

	scene_objects["a"] = "a";
	scene_objects["b"] = "b";
	scene_objects["c"] = "c";

	scene_objects.insert(scene_objects.begin(), std::pair<std::string, std::string>("d", "d"));

	for (auto object : scene_objects) {

		std::cout << object.first << std::endl;

	}


PRINTS:
a
b
c
d

I hope someone could help me :). Thanks.
Last edited on
That's because map is mapped by key and it's not linear like a vector.

Internally, map containers keep all their elements sorted by their key following the criterion specified by its comparison object. The elements are always inserted in its respective position following this ordering.
Last edited on
std::map is always ordered by the < function of the key type. It's not possible to have an std::map where the keys are ordered like a, d, b, c. The search algorithm requires the keys to be ordered to function correctly.
Last edited on
Maps are associative containers. Their elements are not stored sequentially.
The first parameter (the scene_objects.begin() iterator) is just a suggestion to where the element should be inserted. The order in which elements exist in a map is dependent on the internal comparison object, which is std::less by default.

Take a look at the following code. Notice how each map stores its elements in a different order depending on the comparison object with which it was initialized.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
#include <map>
#include <functional>

int main() {

	struct ComparisonStringLength {
		bool operator()(const std::string& string_one, const std::string& string_two) const {
			return (string_one.size() < string_two.size());
		}
	};

	//Uses std::less comparison object by default
	std::map<std::string, int/*, std::less<std::string>*/> map_less = { {"also", 1}, {"beautiful", 1}, {"car", 1} };

	std::map<std::string, int, std::greater<std::string>> map_greater = { { "also", 1 },{ "beautiful", 1 },{ "car", 1 } };

	std::map<std::string, int, ComparisonStringLength> map_longer = { { "also", 1 },{ "beautiful", 1 },{ "car", 1 } };

	std::cout << "Map (std::less):" << std::endl;
	for (const auto& it : map_less) {
		std::cout << it.first << std::endl;
	}
	std::cout << std::endl;

	std::cout << "Map (std::greater):" << std::endl;
	for (const auto& it : map_greater) {
		std::cout << it.first << std::endl;
	}
	std::cout << std::endl;

	std::cout << "Map (ComparisonStringLength):" << std::endl;
	for (const auto& it : map_longer) {
		std::cout << it.first << std::endl;
	}
	std::cout << std::endl;

	std::cin.ignore();
	return 0;
}
Map (std::less):
also
beautiful
car

Map (std::greater):
car
beautiful
also

Map (ComparisonStringLength):
car
also
beautiful
Last edited on
Topic archived. No new replies allowed.