Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

problem with iterator (map iterator)

45 views
Skip to first unread message

Jim Anderson

unread,
Jan 10, 2014, 8:52:20 AM1/10/14
to
I'm writing a small program that will use a map (i.e. map<string,int>).
it has been a while since I have written in c++, so I found an example
on the internet and modified it for my own use. But when I try to
compile my program, I get a compile error.

The code looks ok to me, so I did a search on the internet to see if
others had the same problem. I found 3 or 4 examples, but the solutions
were all related to using an iterator, when infact a const_iterator
should be used. That is not the cause of my problem, as near as I can tell.

I have reduced my code to a simple example:

1
2 #include <iostream>
3 #include <map>
4 #include <string>
5
6 using namespace std;
7
8 int main()
9 {
10 map<string, int> totals;
11
12 totals.insert(std::pair<string,int>("NJ",10));
13 totals.insert(std::pair<string,int>("NY",20));
14 totals.insert(std::pair<string,int>("PA",30));
15 totals.insert(std::pair<string,int>("CT",40));
16
17 map<int,string>::iterator iter;
18 for(iter = totals.begin(); iter != totals.end(); ++iter) {
19 cout << (*iter).first << ": " << (*iter).second << endl;
20 }
21 }


I'm using the GNU compiler g++ version 4.4.5 on crunchbang linux. The
compile error messages are:

1 g++ -g -Wall -I/home/jja/bfs/include -DLINUX -c problem.cc
2 problem.cc: In function �int main()�:
3 problem.cc:18: error: no match for �operator=� in �iter =
totals.std::map<_Key, _Tp, _Compare, _Alloc>::begin [ with _Key =
std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
_Tp = int, _Compare = std:: less<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >, _Alloc =
std::allocator<std::pai r<const std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int> >]()�
4 /usr/include/c++/4.4/bits/stl_tree.h:154: note: candidates are:
std::_Rb_tree_iterator<std::pair<const int, std ::basic_string<char,
std::char_traits<char>, std::allocator<char> > > >&
std::_Rb_tree_iterator<std::pair<const int, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > >::operator=(const
std::_Rb_tree _iterator<std::pair<const int, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > > >&)
5 problem.cc:18: error: no match for �operator!=� in �iter !=
totals.std::map<_Key, _Tp, _Compare, _Alloc>::end [ with _Key =
std::basic_string<char, std::char_traits<char>, std::allocator<char> >,
_Tp = int, _Compare = std:: less<std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >, _Alloc =
std::allocator<std::pai r<const std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int> >]()�
6 /usr/include/c++/4.4/bits/stl_tree.h:216: note: candidates are:
bool std::_Rb_tree_iterator<_Tp>::operator!=(co nst
std::_Rb_tree_iterator<_Tp>&) const [with _Tp = std::pair<const int,
std::basic_string<char, std::char_trai ts<char>, std::allocator<char>
> >]
7 make: *** [problem.o] Error 1


Can anyone help explain the compile errors/

Jim Anderson

Drew Lawson

unread,
Jan 10, 2014, 9:27:56 AM1/10/14
to
In article <laoulm$sju$1...@speranza.aioe.org>
Jim Anderson <jjander...@yahoo.com> writes:
>I'm writing a small program that will use a map (i.e. map<string,int>).
>it has been a while since I have written in c++, so I found an example
>on the internet and modified it for my own use. But when I try to
>compile my program, I get a compile error.

> 10 map<string, int> totals;

> 17 map<int,string>::iterator iter;

Those two lines do not use the same kind of map.



--
Drew Lawson So risk all or don't risk anything
You can lose all the same

Jim Anderson

unread,
Jan 10, 2014, 9:22:42 AM1/10/14
to

RESOLVED.

Please ignore my post. I spent a day looking at this compiler error and
then right after posting, I noticed that line 17 has string and int
transposed and should be:


map<string,int>::iterator iter;

not:

map<int,string>::iterator iter;

Jim Anderson

unread,
Jan 10, 2014, 9:25:20 AM1/10/14
to

Drew,

Thank you. I spent a day looking at the problem, then 5 minutes after I
posted, I saw the cause. A bit of egg on my face, but sometimes this
happens.

Jim

Luca Risolia

unread,
Jan 13, 2014, 6:09:07 PM1/13/14
to
Jim Anderson wrote:
> I noticed that line 17 has string and int
> transposed and should be:

> map<string,int>::iterator iter;

or:

decltype(totals)::iterator iter;

or, since you use iter in a for loop only:

for (auto iter = std::begin(totals); // or std::cbegin() from C++14

0 new messages