Solved

Question about BOOST_FOREACH

Posted on 2014-01-25
1
159 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 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