Skip to main content
gamedev.net
🔒 Locked

Multimap/C++ STL

Started by rish2005 Sep 26, 2005 at 6:50 AM 0 replies 600+ views
Original Post
rish2005
rish2005
Hi I want to store pair in a multimap. If i print the contents of the multimap, it prints the blank string. It is happening becoz while storing int value in an character array, first character encountered is null while printing. Is there any way of passing character array of fixed length in a multimap irrespective of any null in between? see prog: std::multimap *objmap2=NULL; std::multimap* populatemap() { std::multimap *objmap=NULL; objmap=new std::multimap; char a[5]; int at=7021709; memcpy(a,&at,sizeof(int)); a[4]='\0'; std::pair p1(20,a); objmap->insert(p1); return objmap; } main() { objmap2=populatemap(); std::pair::iterator , std::multimap::iterator> retPair; retPair = objmap2->equal_range(20); std::multimap::iterator currIter = retPair.first; while ( currIter != retPair.second) { cout<<" value : " << (*currIter).second << endl; currIter++; } }
Enigma
Enigma
std::multimap< int, std::string > * objmap2 = NULL;std::multimap< int, std::string > * populatemap(){	std::multimap< int, std::string > * objmap = NULL;	objmap = new std::multimap< int, std::string >;	// no null terminator required	char a[4];	int at = 7021709;	memcpy(a, &at, sizeof(int));	std::pair< const int, std::string > p1(20, std::string(a, 4));	objmap->insert(p1);	return objmap;}main(){	objmap2 = populatemap();	std::pair< std::multimap< int, std::string >::iterator , std::multimap< int, std::string >::iterator > retPair;	retPair = objmap2->equal_range(20);	std::multimap< int, std::string >::iterator currIter = retPair.first;	while (currIter != retPair.second)	{		cout << " value : " << (*currIter).second << endl;		currIter++;	}	delete objmap2;}

The std::string(charT const *, size_type n) constructor constructs a string from a character array of given length. Since std::string stores its length internally it does not have to be null terminated and so can contain embedded nulls.

The question does have be asked though, why are you wanting to store the binary representation of an integer as a string?

Enigma

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.