for_each algorithm

This is a discussion on for_each algorithm within the C++ Programming forums, part of the General Programming Boards category; Okay, so my homework assignment states, "use the for_each algorithm to print out the contents of a vector of doubles ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    45

    for_each algorithm

    Okay, so my homework assignment states, "use the for_each algorithm to print out the contents of a vector of doubles using the display function." Here is what i have, but how do i get it to send a double to display when all i have is a vector of doubles?

    Thanks


    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    void display (double n);
    
    int main()
    {
    	vector<double> numbers;
    	
    	for_each(numbers.begin(), numbers.end(), display(/*what goes here*/));
    
    	return 0;
    }
    
    void display (double n)
    {
    	cout << n << endl;
    }

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Nothing goes in there. Its assumed the current value in the for_each statement is the parameter of the function:

    Code:
    for_each(numbers.begin(), numbers.end(), display);

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    45
    isn't that cute...thank you very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 02:30 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21