?
=?ISO-8859-1?Q?Erik_Wikstr=F6m?=
First of all, forgive me if this is the wrong place to ask this question,
if it's a stupid question (it's my second week with C++), or if this is
answered some place else (I've searched but not found anything).
Here's the problem, I have two sets of files, the name of a file contains a
number which is unique for each set but it's possible (even probable) that
two files in different sets have the same numbers. I want to store these
file-names in such a way that I can retrieve them by their number. For this
I thought I'd use a map with the number as the key an an array of strings
to store the file-names, however I've run into trouble when trying to use
this solution.
When adding the file-names to the map I don't know if a filename from the
other set already exists, but given examples at:
http://www.fredosaurus.com/notes-cpp/stl-containers/map/pair.html
http://www.fredosaurus.com/notes-cpp/stl-containers/map/map-ex-wordfreq.html
it seams like entries not found are created and if they are found I can
modify them.
Here's code that displays my problem:
#include <map>
#include <string>
#include <vector>
using std::map;
using std::string;
using std::vector;
class Files
{
public:
void nextFile(string*, string*);
void open(vector<string>*, int);
private:
int getNumber(string); // Gets the number from the filename
map<int, string[2]> fileSets;
map<int, string[2]>::iterator iter;
};
// Return the next pair of files
// I've removed a bit of code to keep it short,
// among other things checks for fileSets.end().
void Files::nextFile(string* file1, string* file2)
{
iter++;
// Return the pointers to the filenames
file1 = &(iter->second[0]);
file2 = &(iter->second[1]);
}
// Add files to the sets
// files is a vector with the filenames to be added.
// setNr is the number of the set the files should be
// added to, can be 0 or 1.
void Files:pen(vector<string>* files, int setNr)
{
if(setNr < 2)
{
for(int i = 0; i < files->size(); i++)
{
// Add the file to the correct set
int fileNumber = getNumber((*files));
(fileSets[fileNumber])[setNr] = (*files);
// This does not work ^^^^^^^
}
}
// Set the iterator
iter = fileSets.begin();
}
------------------------------------------
When compiling on gcc 3.3 I get the following:
/usr/include/c++/3.3.4/bits/stl_map.h: In member function `_Tp& std::map<_Key,
_Tp, _Compare, _Alloc>:perator[](const _Key&) [with _Key = int, _Tp =
std::string[2], _Compare = std::less<int>, _Alloc =
std::allocator<std:air<const int, std::string[2]> >]':
Files.cpp:46: instantiated from here
/usr/include/c++/3.3.4/bits/stl_map.h:319: error: ISO C++ forbids casting to an
array type `std::string[2]'
------------------------------------------
and I get similar results when using gcc 3.4. Obviously there's some problem
with the array but I can't figure out what and would appreciate a nudge in the
right direction, and since I'm still learning any other comment too.
PS:
It's not possible to get random access to a map if the key is not known
but if I have an (bidirectional) iterator pointing at some element in the
map, and know that the element I'm interested in is N elements from the one
the iterator is currently pointing at can I jump to that element by adding
N to the iterator, e.g. iter+=N ?
if it's a stupid question (it's my second week with C++), or if this is
answered some place else (I've searched but not found anything).
Here's the problem, I have two sets of files, the name of a file contains a
number which is unique for each set but it's possible (even probable) that
two files in different sets have the same numbers. I want to store these
file-names in such a way that I can retrieve them by their number. For this
I thought I'd use a map with the number as the key an an array of strings
to store the file-names, however I've run into trouble when trying to use
this solution.
When adding the file-names to the map I don't know if a filename from the
other set already exists, but given examples at:
http://www.fredosaurus.com/notes-cpp/stl-containers/map/pair.html
http://www.fredosaurus.com/notes-cpp/stl-containers/map/map-ex-wordfreq.html
it seams like entries not found are created and if they are found I can
modify them.
Here's code that displays my problem:
#include <map>
#include <string>
#include <vector>
using std::map;
using std::string;
using std::vector;
class Files
{
public:
void nextFile(string*, string*);
void open(vector<string>*, int);
private:
int getNumber(string); // Gets the number from the filename
map<int, string[2]> fileSets;
map<int, string[2]>::iterator iter;
};
// Return the next pair of files
// I've removed a bit of code to keep it short,
// among other things checks for fileSets.end().
void Files::nextFile(string* file1, string* file2)
{
iter++;
// Return the pointers to the filenames
file1 = &(iter->second[0]);
file2 = &(iter->second[1]);
}
// Add files to the sets
// files is a vector with the filenames to be added.
// setNr is the number of the set the files should be
// added to, can be 0 or 1.
void Files:pen(vector<string>* files, int setNr)
{
if(setNr < 2)
{
for(int i = 0; i < files->size(); i++)
{
// Add the file to the correct set
int fileNumber = getNumber((*files));
(fileSets[fileNumber])[setNr] = (*files);
// This does not work ^^^^^^^
}
}
// Set the iterator
iter = fileSets.begin();
}
------------------------------------------
When compiling on gcc 3.3 I get the following:
/usr/include/c++/3.3.4/bits/stl_map.h: In member function `_Tp& std::map<_Key,
_Tp, _Compare, _Alloc>:perator[](const _Key&) [with _Key = int, _Tp =
std::string[2], _Compare = std::less<int>, _Alloc =
std::allocator<std:air<const int, std::string[2]> >]':
Files.cpp:46: instantiated from here
/usr/include/c++/3.3.4/bits/stl_map.h:319: error: ISO C++ forbids casting to an
array type `std::string[2]'
------------------------------------------
and I get similar results when using gcc 3.4. Obviously there's some problem
with the array but I can't figure out what and would appreciate a nudge in the
right direction, and since I'm still learning any other comment too.
PS:
It's not possible to get random access to a map if the key is not known
but if I have an (bidirectional) iterator pointing at some element in the
map, and know that the element I'm interested in is N elements from the one
the iterator is currently pointing at can I jump to that element by adding
N to the iterator, e.g. iter+=N ?