Solved

Question about BOOST_FOREACH

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

839 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