1 Replies - 1009 Views - Last Post: 04 August 2009 - 11:17 AM Rate Topic: -----

#1 jhendrix69  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-June 09

STL Map Help!

Posted 04 August 2009 - 08:54 AM

So I need to use a map for something else more complicated in scope, but essentially, this is what I need to do:

#include <iostream>
#include <map>
#include <vector>

using namespace std;

class Foo
{
public:	
	Foo() {};
	~Foo() {};

	void populate()
	{
		strcpy(&text[0][0], "The");
		strcpy(&text[1][0], "Cake");
		strcpy(&text[2][0], "Is");
		strcpy(&text[3][0], "A");
		strcpy(&text[4][0], "Lie");
		strcpy(&text[5][0], "Cake");
	}

	char * get(int i)
	{
		return text[i];
	}

	map <char *, int> * getmap() {return &testmap;}

private:
	char text[6][10];
	map <char *, int> testmap;

};

// This demonstrates working with STL maps using a pointer to the map
// and having the map store a char * key mapped to an int value
int main()
{
	vector <char *> vec;
	map <char *, int>::iterator it;
	string s;
	int val;
	Foo f;
	char * cptr;
	char * cptr2;
	map <char *, int> * tmap;

	tmap = f.getmap();

	f.populate();

	// Inserting elements 
	for (int i = 0; i < 5; i++)
	{
		cptr = f.get(i);
		tmap->insert(pair<char *, int>(cptr, 1));
		
		vec.push_back(cptr);
	}

	cptr2 = f.get(5);
	// Do a replace, "1" in "Cake" to "2"
	it = tmap->find(cptr2);
	(*it).second = 2;

	// Straight up loop
	for (int j = 0; j < 5; j++)
	{
		it = tmap->find(vec[j]);
		cptr = (*it).first;


		s = string(cptr);
		cout << s.c_str() << endl;
	}

	// Using iterators - I don't need that extra vector!
	for (it = tmap->begin(); it != tmap->end(); it++)
	{
		cptr = (*it).first;
		val = (*it).second;


		s = string(cptr);
		cout << s.c_str() << " " << val << endl;
	}

	cin.get();
	return 0;
}



I basically have a class that has text descriptions stored in it via a 2D array and a function that will return a pointer to each description. This function will eventually be called numerous times for different descriptions and I need to keep track of how many times each description has been called. I used a map because it seemed the most suitable container for this task (correct me if I'm wrong!). I can check for collisions each time, and if one is detected, I will simply increment the value that the description is mapped to. However this proof-of-concept code is not working for me, my code will crash at

it = tmap->find(cptr2);


What am I doing wrong? Can I not get the above functionality by using char * maps? Should I be using string maps?

This post has been edited by jhendrix69: 04 August 2009 - 09:55 AM


Is This A Good Question/Topic? 0
  • +

Replies To: STL Map Help!

#2 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: STL Map Help!

Posted 04 August 2009 - 11:17 AM

The problem you are running into is how you put the keys into the map and subsequently trying to receive them.

The keys you put into the map ("The", "Cake" ...) are allocated on the stack.
On my box they have an address of

"The" - 0x0012fe98
"Cake" - 0x0012fea2
...

When you assign the search key to cptr2 you expect the map to search the string that cptr2 is point 2. However, this is not case what you get is a comparison of pointer addresses instead. On my box cptr2 has an address of 0x0012feca.


Instead change the map to this
map<std::string, int> *tmap.

Then when you do this
tmap->insert(pair<char *, int>(cptr, 1));

you will actually have a copy of the string
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1