473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Confusion with std::map

Bob
Hi,

I'm trying to use a map with a string key, and a pointer to objects
contained in a vector. I've wrapped this in a class like so:

// cMap
template<class T> class cMap : public cList<T> { //
private:
protected:
std::map<std::string, T*> tMapOf; // map container
public:
void Add(const std::string&, const T& t); // add new object to map
void Clear();
const std::size_t Count() const; // number of list items

cMap(); // default constructor
cMap(const cMap&); // copy constructor
cMap& operator=(const cMap&); // assignment operator
virtual ~cMap(); // destructor
};

cList is a similar wrapper around a vector, defined as so:

// cList definition
template<class T> class cList { // base class for Lists
private:
protected:
std::vector<T> tListOf; // field list container
public:
void Add(const T& t); // add new object to list
void Clear();
void Reserve(const std::size_t&);
const std::size_t Count() const; // number of list items

cList(); // default constructor
cList(const cList&); // copy constructor
cList& operator=(const cList&); // assignment operator
virtual ~cList(); // destructor

T& operator[](int pos); // subscript operator
const T& operator[](int pos) const; // subscript operator
};

cMap::Add is implemented like:

template<class T>
void cMap<T>::Add(const std::string& s, const T& t)
{
tListOf.push_back(t);
tMapOf[s] = &(tListOf.back());
}
I then use a lookup function to get data, like so:

//--------------------------------------------------------------------------
-
template <class T>
const int cGenericMeasurementList<T>::ColIDFromName(const string& s)
{
if(tMapOf.find(s) != tMapOf.end())
return tMapOf[s]->ColID();
else return -1;
}
My problem is that this code works fine with Borland C++ Compiler (the one
with BCB 5 Pro) but when I compile and run with g++ (3.3 I think) the data
return by ColIDFromName is garbage.

If I replace the pointers with copies of the objects, this works fine with
either compiler and the data returned is correct, but then I have two sets
of identical data.

I'm sure I'm doing something totally stupid, but can't see what. Can anyone
advise what is wrong with my code (probably lots, as I'm new!!)???

Many thanks for your time,
Steve.
Jul 22 '05 #1
1 1943
"Bob" <no***@nowhere.com> wrote in message
news:m4***************@newsfe5-gui.ntli.net...
Hi,

I'm trying to use a map with a string key, and a pointer to objects
contained in a vector. I've wrapped this in a class like so:

<snip>

The most likely problem is that the std::vector is reallocating its storage,
which causes the pointers to be invalidated. Either use indexes instead of
the pointers, or use a container that won't invalidate your pointers.

--
David Hilsee
Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Woodster | last post by:
I have declared the following std::map<std::string, std::string> myMap; to pass myMap to functions should I be declaring functions as: void function(std::map<std::string, std::string>); ...
1
by: Antti Granqvist | last post by:
Hello! I have following object relations: Competition 1--* Category 1--* Course 1 | | * Course
2
by: Serengeti | last post by:
Hello, in my class I have a map that translates strings to pointers to some member functions. The code goes like this: class F { typedef void (Function::*MathFuncPtr)(); std::map<std::string,...
1
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
19
by: Erik Wikström | last post by:
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...
1
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug This program is developed...
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
2
by: digz | last post by:
Hi, I am trying to write a program which has two threads one of them write to a map , and the other one deletes entries based on a certain criterion.. first I cannot get the delete portion to...
8
by: mveygman | last post by:
Hi, I am writing code that is using std::map and having a bit of an issue with its performance. It appears that the std::map is significantly slower searching for an element then a sequential...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.