C++ Functions with struct and vectors : <p>I just finished a program that reads a CSV file and outputs the rows using structs and vectors. My question involves these specific lines:</p>

<pre><code>displayBid(bids[i]);

void displayBid(Bid bid)
{
    cout &lt;&lt; bid.title &lt;&lt; " | " &lt;&lt; bid.amount &lt;&lt; " | " &lt;&lt; bid.fund &lt;&lt; endl;
    return;
}
</code></pre>

<p>I am not sure if I am thinking about this correctly, but how is displayBid able to take in a vector as a parameter? The displayBid function takes in a struct called of Bid type. Originally I could not get the code to compile because I was trying displayBid(bid) and I got a scope error. I figured the displayBid function would need to take in a struct instead of a vector. Thanks.</p>

<p>Source:</p>

<pre><code>#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;time.h&gt;

// FIXME (1): Reference the CSVParser library
#include "CSVparser.hpp"

using namespace std;



// forward declarations
double strToDouble(string str, char ch);

struct Bid
{
    string title;
    string fund;
    double amount;
    // Set default bid amount to 0.0
    Bid()
    {
        amount = 0.0;
    }
};



/**
 * Display the bid information
 *
 * @param bid struct containing the bid info
 */
void displayBid(Bid bid)
{
    cout &lt;&lt; bid.title &lt;&lt; " | " &lt;&lt; bid.amount &lt;&lt; " | " &lt;&lt; bid.fund &lt;&lt; endl;
    return;
}

/**
 * Prompt user for bid information
 *
 * @return Bid struct containing the bid info
 */
Bid getBid()
{
    Bid bid;

    cout &lt;&lt; "Enter title: ";
    cin.ignore();
    getline(cin, bid.title);

    cout &lt;&lt; "Enter fund: ";
    cin &gt;&gt; bid.fund;

    cout &lt;&lt; "Enter amount: ";
    cin.ignore();
    string strAmount;
    getline(cin, strAmount);
    bid.amount = strToDouble(strAmount, '$');

    return bid;
}

/**
 * Load a CSV file containing bids into a container
 *
 * @param csvPath the path to the CSV file to load
 * @return a container holding all the bids read
 */
vector&lt;Bid&gt; loadBids(string csvPath)
{
    // FIXME (2): Define a vector data structure to hold a collection of    bids.

    vector&lt;Bid&gt; bids;

    // initialize the CSV Parser using the given path
    csv::Parser file = csv::Parser(csvPath);

    // loop to read rows of a CSV file
    for (int i = 0; i &lt; file.rowCount(); i++)
    {
        // FIXME (3): create a data structure to hold data from each row     and add to vector
        Bid bid;

        bid.title = file[i][0];
        bid.fund = file[i][8];
        //Convert to double and take out $
        bid.amount = strToDouble(file[i][4], '$');

        bids.push_back(bid);
    }
    return bids;
}

/**
 * Simple C function to convert a string to a double
 * after stripping out unwanted char
 *
 * credit: http://stackoverflow.com/a/24875936
 *
 * @param ch The character to strip out
 */
double strToDouble(string str, char ch)
{
    str.erase(remove(str.begin(), str.end(), ch), str.end());
    return atof(str.c_str());
}

/**
 * The one and only main() method
 */
int main(int argc, char* argv[])
{

    // process command line arguments
    string csvPath;
    switch (argc)
    {
    case 2:
        csvPath = argv[1];
        break;
    default:
        csvPath = "eBid_Monthly_Sales_Dec_2016.csv";
    }

    // FIXME (4): Define a vector to hold all the bids
    vector&lt;Bid&gt; bids;





    // FIXME (7a): Define a timer variable
    clock_t timer;

    int choice = 0;
    while (choice != 9)
    {
        cout &lt;&lt; "Menu:" &lt;&lt; endl;
        cout &lt;&lt; "  1. Enter a Bid" &lt;&lt; endl;
        cout &lt;&lt; "  2. Load Bids" &lt;&lt; endl;
        cout &lt;&lt; "  3. Display All Bids" &lt;&lt; endl;
        cout &lt;&lt; "  9. Exit" &lt;&lt; endl;
        cout &lt;&lt; "Enter choice: ";
        cin &gt;&gt; choice;

        switch (choice)
        {
        case 1:
            cout &lt;&lt; "Not currently implemented." &lt;&lt; endl;

            break;
        case 2:
            // FIXME (7b): Initialize a timer variable before loading bids
            timer = clock();

            // FIXME (5): Complete the method call to load the bids
            bids = loadBids(csvPath);

            // FIXME (7c): Calculate elapsed time and display result
            timer = clock() - timer;
            cout &lt;&lt; bids.size() &lt;&lt; " bids loaded" &lt;&lt; endl;
            cout &lt;&lt; "time: " &lt;&lt; (float)timer/CLOCKS_PER_SEC * 1000  &lt;&lt; "   milliseconds" &lt;&lt; endl;
            cout &lt;&lt; "time: " &lt;&lt; (float)timer/CLOCKS_PER_SEC &lt;&lt; " seconds" &lt;&lt; endl;

            break;
        case 3:
            // FIXME (6): Loop and display the bids read
            for (int i = 0; i &lt; bids.size(); ++i)
            {

                displayBid(bids[i]);

            }
            cout &lt;&lt; endl;

            break;
        }
    }

    cout &lt;&lt; "Good bye." &lt;&lt; endl;

    return 0;
}
</code></pre>
