I need help with inserting a string for both key and value to a map.

while(!myHuffFile.eof()){
		string word, code;
		int freq;
		myHuffFile >> word >> freq >> code;
		if(word == ""){
			break;
		}else{
                        _fileMap.insert(pair<string,string>(word,code));
			_freqMap.insert(pair<string,int>(word,freq));
		}
	}

error:

error: conversion from `std::_Rb_tree_iterator<std::pair<const std::string, std::string> >' to non-scalar type `std::_Rb_tree_iterator<std::pair<const std::string, int> >' requested

can you please help me with this.

thanks

drjay

Recommended Answers

All 5 Replies

As written there is nothing wrong with this (except I think that you would benefit from a typedef. e.g.

typedef std::map<std::string,int> mTYPE;
mTYPE aMap;
//....
aMap.insert(mTYPE::value_type(word,i));

However, I would guess that you have swapped the declarations of fileMap and freqMap.

It that is not the problem, please make a short test program and post that.

there are two maps.
_fileMap<string,string> and the other is _freqMap<string, int>

_freqMap works fine but _fileMap doesn't. I don't think typedef will work here. I'll try it though.

i figured it out! thanks!

i figured it out! thanks!

Please specify what the issue was and what solution did you apply over it.

It's been awhile, let me see if I can remember what happened.

I had 2 maps: map<string,int> and map <string,string>

My map<string,string> was giving me the above mentioned error.

What I was doing:

typedef std::map<std::string,int> SOMETYPE1;
typedef std::map<std::string,string> SOMETYPE2;

SOMETYPE1 Map1;
SOMETYPE1 Map2;

Map1.insert(word,i); //or something similar to this

The code was long and the two declarations weren't next to each other. I used SOMETYPE1 for both maps and that was the error.

Yes its a stupid mistake! It took me 6 hours to figure it out. But then again that happens, that how you learn!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.