Concept of saving file names in 2D container/2D array

I am having images which are having two numbers for x and y position. I am going to merge these images together as you would do on a grid or on a map. In my code I can get the x and y values.

Mentally my design is not using 2 dimensions - I am using 1D container for strings:
static std::vector<std::string> Files; // source file(s)
Later when I parse the file name and I save the filename to the container:
src.Files.push_back(search_data.cFileName);
The problem is that I did not save the x and y value, which would mean I would need to parse the name later to merge the images in grid... So to enhance it I need to use some kind of container.

My idea is that I could save the x and y value like this arr[x][y]=filename;
however, I need a type because I must insert it into the vector. So I need 2D type of array. To use store it like:
static std::vector<2DArrType> Files; // source file(s)

All I have found till now is this link:
http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new
which does not uses type, but it creates the arrays. Should I create a class which will have constructor to allocate memory in similar way, and destructor to deallocate it, or is there some more professional way, which is used for cases like that? It would look strange if there would not be type or container to solve this problem (ain't it strange to create class, constructor for allocation and destructor for deallocation every time I need to create 2D container like this?).
Last edited on
Why not use a std::map? Your key could be the following:
1
2
3
struct coord
{  int x, y;
};


Then declare the map as follows:
1
2
 
std::map<coord,string> files;


You could then efficiently retrieve the filename by:
1
2
3
4
5
6
  coord    xy;
  std::map<coord,string>::const_iterator citer;

  xy.x = ...;
  xy.y = ...;
  citer = files.find (xy);


Last edited on
I have no experience with map container, but I will try it. Thanks.
Something wrong? Having error:

visual studio 10.0\vc\include\xfunctional(125): error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const COORDS'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct COORDS {  int x, y; };
std::map<COORDS,std::string>  SRC::Files;
COORDS coords;
...
if (result) {
	if ( !src.Regex.switchxy )
		{
		coords.y = firstDigit;
		coords.x = secondDigit;
		}
	else
		{
		coords.y = secondDigit;
		coords.x = firstDigit;
		}
		src.Files.insert(std::pair<COORDS,char *>(coords,search_data.cFileName));
	      }



The compiles (Visual Studio) breaks in file xfunctional
1
2
3
4
5
6
7
8
9
10
		// TEMPLATE STRUCT less
template<class _Ty>
	struct less
		: public binary_function<_Ty, _Ty, bool>
	{	// functor for operator<
	bool operator()(const _Ty& _Left, const _Ty& _Right) const
		{	// apply operator< to operands
		return (_Left < _Right);
		}
	};


On the line
 
return (_Left < _Right);
Last edited on
std::map requires that the key class have or overload the < operator.
Sorry for not mentioning that.
THe < operator is used to determine where to insert the key in the tree that std::map uses.

1
2
3
4
5
6
struct COORDS 
{  int x, y; 
    bool operator < (const COORDS & rhs) const
    { return x < rhs.x && y < rhs.y;  // Or what ever makes sense
    }
};



Great. It works :-)
Topic archived. No new replies allowed.