Using Iterator in the list with type of pair

Hi,
As you can see I have used pair for two type of data in a list here is my simple code and i intend to:
1.Initial my list to five space with all char = a and all int = 3.
2. use an iterator

I can use these feature in the way the list is not paired.how can make seam thing in the pair style?!

cheers


1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
#include<list>
using namespace std;

int main(){
	list< pair< char, int> > myList(10,'c',3);
	list<char, int>::iterator p = myList.begin();
//	cout<<myList.front().first<<endl;

	return 0;
}
Use pair in that initializer again: list< pair< char, int> > myList(10,pair< char, int>('c',3));
list< pair< char, int> > myList(10,pair< char, int>('c',3));


It did not work and gave me an huge error. I think it's not possible to define pair List like non-pair list and in this case I must define m own iterator. :( Isn't it?
It works. But line 7 is still wrong. The interface must be the same as line 6
1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
#include<list>
using namespace std;

int main(){
	list< pair< char, int> > myList(10,'c',3);
	list< pair< char, int> >::iterator p = myList.begin(); // Here it must be pair as well
//	cout<<myList.front().first<<endl;

	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
#include<list>
using namespace std;

int main(){
	
	list< pair< char, int> > myList(10,'c',3);
//	list< pair< char, int> >::iterator p = myList.begin();

	system("pause");
	return 0;
}


I tried this codes but did not work. even without line 7!
Well

1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
#include<list>
using namespace std;

int main(){
	list< pair< char, int> > myList(10,pair< char, int>('c',3)); // Indeed pair
	list< pair< char, int> >::iterator p = myList.begin(); // Here it must be pair as well
//	cout<<myList.front().first<<endl;

	return 0;
}
Oh thanks now it works ... i didn't see any info about pair type in iterator, on the internet...even this also works:


cout<<"Hi "<<p->first;
Last edited on
Topic archived. No new replies allowed.