map question

S

sam

In the following program I am getting an error when I am trying to access
i1.first. Is this valid statement?
how can I access map value through iterator? Please help me foreach
function also.


void printall(string str, int i){
cout << str<<endl;
}
int main(){

map<string,int> m;
m["One"]=1;
m["Two"]=2;
m["Three"]=3;
map<string,int>::iterator i1;
i1= m.begin();
while(i1!=m.end()){
cout << i1.first; // Here is the problem.
++i1;
}
//foreach(i1.begin(),i1.end(),printall);
}
 
A

Andre Kostur

In the following program I am getting an error when I am trying to
access i1.first. Is this valid statement?
how can I access map value through iterator? Please help me foreach
function also.


void printall(string str, int i){
cout << str<<endl;
}
int main(){

map<string,int> m;
m["One"]=1;
m["Two"]=2;
m["Three"]=3;
map<string,int>::iterator i1;
i1= m.begin();
while(i1!=m.end()){
cout << i1.first; // Here is the problem.
++i1;
}
//foreach(i1.begin(),i1.end(),printall);
}

i1 is an iterator into a map<string, int>, thus it "points" to a pair
<string, int>. So you'll want:

cout << i1->first;


As for your for_each, you'll want it to go from "m.begin()" to "m.end()".
And printall would need to take a (const?) reference to a pair
<string,int> in order to have a chance of working. (I unfortunately
don't use the algorithms often enough to have the details at the top of
my head....)
 
S

sam

(*i).first is correct.
sam said:
I found the problem.
I have to access (*i1.first) instead of i1.fist.

sam said:
In the following program I am getting an error when I am trying to access
i1.first. Is this valid statement?
how can I access map value through iterator? Please help me foreach
function also.


void printall(string str, int i){
cout << str<<endl;
}
int main(){

map<string,int> m;
m["One"]=1;
m["Two"]=2;
m["Three"]=3;
map<string,int>::iterator i1;
i1= m.begin();
while(i1!=m.end()){
cout << i1.first; // Here is the problem.
++i1;
}
//foreach(i1.begin(),i1.end(),printall);
}
 
C

Chris \( Val \)

| In the following program I am getting an error when I am trying to access
| i1.first. Is this valid statement?
| how can I access map value through iterator? Please help me foreach
| function also.
|
|
| void printall(string str, int i){
| cout << str<<endl;
| }

void printall( std::pair<std::string, int> P ) {
std::cout << P.first << ", " << P.second << endl;
}

| int main(){
|
| map<string,int> m;
| m["One"]=1;
| m["Two"]=2;
| m["Three"]=3;
| map<string,int>::iterator i1;
| i1= m.begin();
| while(i1!=m.end()){
| cout << i1.first; // Here is the problem.

std::cout << i1 -> first;

| ++i1;
| }
| //foreach(i1.begin(),i1.end(),printall);
| }

std::for_each( m.begin(), m.end(), printall );

Cheers.
Chris Val
 
A

Artie Gold

sam said:
(*i).first is correct.

Which is typically expressed as `i->first' (as per Andre's reply).
I found the problem.
I have to access (*i1.first) instead of i1.fist.

In the following program I am getting an error when I am trying to
access
i1.first. Is this valid statement?
how can I access map value through iterator? Please help me foreach
function also.


void printall(string str, int i){
cout << str<<endl;
}
int main(){

map<string,int> m;
m["One"]=1;
m["Two"]=2;
m["Three"]=3;
map<string,int>::iterator i1;
i1= m.begin();
while(i1!=m.end()){
cout << i1.first; // Here is the problem.
++i1;
}
//foreach(i1.begin(),i1.end(),printall);
}

HTH,
--ag
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top