LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-19-2006, 04:48 AM   #1
krishnacins
LQ Newbie
 
Registered: Mar 2006
Posts: 17

Rep: Reputation: 0
STL Map problem


Hi,
I want to use a map variable like ...
map<int(Key), string(val)> mymap;
it gives error.......when i insert the value using insert function.

plz guide me that how can i use int value for the Key in map... that i can insert the value according int key

thanks & regards
Krishna

Last edited by krishnacins; 04-19-2006 at 04:54 AM.
 
Old 04-19-2006, 05:02 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Hmmm as far as I am aware you don't need a comp func because you are dealing with keys which are ints, so how are you trying to insert and what's the error you get?
 
Old 04-19-2006, 05:14 AM   #3
krishnacins
LQ Newbie
 
Registered: Mar 2006
Posts: 17

Original Poster
Rep: Reputation: 0
if im using like ...
map<int, char*> mymap;

mymap.insert(1,"hello");
it gives this error.....

error: no matching function for call to `std::map<int,
char*, std::less<int>, std::allocator<std:air<const int, char*> > >::
insert(int, char[50])'
/usr/include/c++/3.3.3/bits/stl_map.h:340: error: candidates are:
std:air<typename std::_Rb_tree<_Key, std:air<const _Key, _Tp>,
std::_Select1st<std:air<const _Key, _Tp> >, _Compare, _Alloc>::iterator,
bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const std:air<const
_Key, _Tp>&) [with _Key = int, _Tp = char*, _Compare = std::less<int>,
_Alloc = std::allocator<std:air<const int, char*> >]
 
Old 04-19-2006, 05:43 AM   #4
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
hmm that char* could be another problem.

try:
Code:
char* str = new char[6];
*str = "hello\0";
mymap[1] = str;
but dont forget to clean up the pointer or better still use a std::string.

lol I do alot of "hmmm"ing

Last edited by dmail; 04-19-2006 at 05:47 AM.
 
Old 04-20-2006, 12:23 AM   #5
krishnacins
LQ Newbie
 
Registered: Mar 2006
Posts: 17

Original Poster
Rep: Reputation: 0
#include <map>
#include <iostream>
#include <string>
using namespace std;

int main()
{
typedef map<int,string> MYMAP;
MYMAP myMap;
myMap.insert(MYMAP::value_type(1,"Sanjay"));
myMap.insert(MYMAP::value_type(2,"Kumar"));
MYMAP::iterator iter;
for(iter = myMap.begin(); iter != myMap.end(); iter++)
{
cout<< "Name : " << (*iter).second << endl;
}
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
GCC + STL Compile Problem Hairein Programming 6 09-23-2004 08:43 AM
problem with using STL set and its funciton erase , insert ashwinipahuja Programming 5 06-02-2004 12:29 AM
Problem due to STL Library vipinsharma Programming 9 06-01-2004 03:46 AM
gcc STL include problem finidi Programming 3 01-14-2003 06:55 AM
C++ STL problem jaycee999 Programming 5 05-31-2002 03:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration