How to insert data into a map

I have a Map that stores row col data.

In case of insert or deleting rows and cols, i have to do some work....
For example I have map [2][0],map [2][1], map [2][2], and If I insert a new row at 2 position I have to 'mode' all my map from 2 to n-rows.
Is there any fast way to 'move' data in maps?
Another way can be create a copy of the section I have to move, and paste it into a new location into the map ?

Any idea ?

If you're using the STL container named map, map::insert() moves the data for you. Is this a two-dimentional array?
Yes STL map.
But what I want is a method to 'move' map data (data or blocks of data ):
I' d like a function like this :
map_move origin, numberof rows , new_pos
When I use this function I have a new empty space at a known position.

I have :

map[0][0 to n]
map[1][0 to n]
map [2][0 to n ]

I want to insert a new value at 1, so I want :

map[0][0 to n] old map 0
map[1][0 to n] empty new data
map [2][0 to n ] (old map 1
map [3][0 to n ] (old map 2


Is there something like this ?
Last edited on
That is what insert does. Why would you want empty data at map[1]?

I explain you what I want:

I want to store information as a spreadsheet. (I dont know if I'm doing a stupid thing but ... )
Imagine I have data on 1x1, 1x2, 2x1 , and 10x1. Using map I only use 4 positions.
Ok, now I want to insert data at row 2.... Initially, I'm not going to have data (so my row 2 are going to be empty)

This is because I'm looking for a 'mover' function for a map.

I hope now you understand me .
Thanks




You don't have to save a blank spot. When you insert data a row 2, just call map::insert. It will move everything over for you. When you want to add data to row two, the call would be:

map<int, int> mymap;

mymap[ 2 ] = 9;
Thanks kooth .
But I dont know how to do it.
I have :
map <int,map <int> >;
I read my cell content, and If I have data I store it using row, col.

Mymap[0][0]=data; Mymap[0][1]=data;...; Mymap[0][n]=data;
------------------------ Mymap[1][1]=data;...; Mymap[1][n]=data;
Mymap[2][0]=data; Mymap[2][1]=data;...; Mymap[2][n]=data;
Mymap[3][0]=data; Mymap[3][1]=data;...; Mymap[3][n]=data;

Ok, if I want to retrive data for each cell, I ask to mymap for data, if there is something I have data, if not the cell is empty. I think this aproach can be interesting to reproduce a spreadsheet data.

Ok, I want to insert and empty row of cells at position 2 ?
Really I want to have the data stored for 2,0-n at 3,0-n and the 3,0-n into 4,0-n.
Sincerely I dont know how to insert data at row 2 and to expect the maps move himself the data from a group of rows to a row+1 new location.
Thanks



I don understand you....
If I have :

map [1]=3331
map [2]=2121
map [3]=82121

How can I insert data to have :

map [1]=3331
map [3]=2121
map [4]=82121

I dont want any data stored at 2.

Then don't store it there. Your map instantiation is incorrect: What two datatypes are you trying to map?
Excuse me, The last example was to explain another point of view of my problem.

The map can be map <int,int> or map <int, map<int> > it is the same.
Repeat I want to store data from a spreadsheet, there are cells with value and other not.

So, using a Map can be a good initial aproach to work.
But the problem is when I have to insert or delete rows with data or empty data.

So, maybe I have to use custom code to have internal counters of inserted an deleted rows.

Do you understand me now ?











Hey tonnot, I understand you. I think you are thinking you need to have a placeholder for empty data. What I didn't say (but implied) is that you can check for the existance of a key, and if it is not in your map, then it's empty. Does that make sense?
Use vector instead. If you want to access data by index vector is the better choice and you can insert as much as you want.
Thanks Kooth.
I know that I dont need to have a placeholder for empty data.
I only ask for a method to 'move' my data and 'insert' is not working for me (or I dont know how to use it ).
Repeat how can do the next using insert :

map [1]=3331
map [2]=2121
map [3]=82121

How can I insert data to have :

map [1]=3331
map [3]=2121
map [4]=82121

And for coder777. I dont want to user vector, because if I have row 1 and 999 (2 rows) I will have 998 empty used positions.
Set up an iterator loop and move them that way.
So I have to loop over the entire map data (from the row I want to insert, of course)

Thanks again !!
Last edited on
Yes, and it should be just a couple of lines of code. Good luck!
Topic archived. No new replies allowed.