Solved

Question about BOOST_FOREACH

Posted on 2014-01-25
1
161 Views
Last Modified: 2014-09-30
Suppose I have a std::vector of double called myVec, and a std::map of type <int, double> called myMap. Why is it that the following works

BOOST_FOREACH(double x, myVec) x = 10.0;

Open in new window

and the following gives an error
BOOST_FOREACH(pair<int,double> y, myMap) 
cout << y.first << "\t" << y.second << endl;

Open in new window

so that I need to use

pair<int,double> y;
BOOST_FOREACH(y, myMap) cout << y.first << "\t" << y.second << endl;

Open in new window

instead?
0
Comment
Question by:Rothbard
1 Comment
 
LVL 3

Accepted Solution

by:
kuroji earned 200 total points
ID: 40088317
It's the comma in the expression
pair<int, double>
The C Preprocessor is not type aware.

Write it like this and you're good to go
typedef std::map<int, double>::value_type map_value_type;

BOOST_FOREACH(map_value_type y, myMap) 
    std::cout << y.first << "\t" << y.second << std::endl;

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now