C++ 具有结构和向量的函数
C++ Functions with struct and vectors
我刚刚完成了一个程序,该程序读取CSV文件并使用结构和向量输出行。我的问题涉及以下具体行:
displayBid(bids[i]);
void displayBid(Bid bid)
{
cout << bid.title << " | " << bid.amount << " | " << bid.fund << endl;
return;
}
我不确定我是否正确考虑了这一点,但是 displayBid 如何能够将向量作为参数?displayBid 函数接受一个名为 Bid 类型的结构。最初我无法获得要编译的代码,因为我正在尝试 displayBid(bid( 并且我遇到了范围错误。我认为 displayBid 函数需要接受结构而不是向量。谢谢。
源:
#include <algorithm>
#include <iostream>
#include <time.h>
// 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 << bid.title << " | " << bid.amount << " | " << bid.fund << endl;
return;
}
/**
* Prompt user for bid information
*
* @return Bid struct containing the bid info
*/
Bid getBid()
{
Bid bid;
cout << "Enter title: ";
cin.ignore();
getline(cin, bid.title);
cout << "Enter fund: ";
cin >> bid.fund;
cout << "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<Bid> loadBids(string csvPath)
{
// FIXME (2): Define a vector data structure to hold a collection of bids.
vector<Bid> 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 < 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<Bid> bids;
// FIXME (7a): Define a timer variable
clock_t timer;
int choice = 0;
while (choice != 9)
{
cout << "Menu:" << endl;
cout << " 1. Enter a Bid" << endl;
cout << " 2. Load Bids" << endl;
cout << " 3. Display All Bids" << endl;
cout << " 9. Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Not currently implemented." << 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 << bids.size() << " bids loaded" << endl;
cout << "time: " << (float)timer/CLOCKS_PER_SEC * 1000 << " milliseconds" << endl;
cout << "time: " << (float)timer/CLOCKS_PER_SEC << " seconds" << endl;
break;
case 3:
// FIXME (6): Loop and display the bids read
for (int i = 0; i < bids.size(); ++i)
{
displayBid(bids[i]);
}
cout << endl;
break;
}
}
cout << "Good bye." << endl;
return 0;
}
它不能,您传递的是出价结构向量的第 i 个元素的副本,而不是整个向量。
如果我明白你的意思......
case 3:
// FIXME (6): Loop and display the bids read
displayBid(bids);
cout << endl;
break;
我会定义 displayBid 如下:
void displayBid(std::vector<Bid> &bids)
{
for_each(begin(bids), end(bids), [](Bid& b) {
cout << b.title << " | " << b.amount << " | " << b.fund << endl;
});
}
如果您无法更改显示出价,您可以创建一个辅助方法...像这样:
void displayBids(std::vector<Bid> &bids)
{
for_each(begin(bids), end(bids), [](Bid& b) {displayBid(b)});
}
未测试的代码...您需要几个包含(例如:算法等(...
相关文章:
- 函数向量_指针有不同的原型,我可以构建一个吗
- 如何使用向量的template_back函数
- 如何删除除 ArduinoSTL 的向量函数之外的所有函数
- 在使用标准向量函数时引发'std::bad_alloc'实例后调用的终止
- 我可以为向量函数返回 NULL<double> 吗?
- 将多个向量(函数结果)组合到一个使用模板中
- 如何在 c++ 中制作通用字符串到向量函数
- 为什么简单的C++向量函数会产生越界的下标
- 为什么我的向量函数的返回值总是 1
- 模板化向量函数参数的std::is_base_of
- 我的程序没有运行,多维向量函数
- C 如何使2D向量函数
- 如何从 v8 返回向量<T> C++ 函数
- C++ 如何使此向量函数接受任何分隔符
- 我想返回一个向量函数,<Competition>但被告知竞争是未声明的,并且我正在使用未定义的类'std::vector'
- C++:如何在类中使用向量函数
- 自己的"Insert"方法(向量)函数模板
- c++中的内存泄漏:循环中的向量函数
- 如何添加一个元素到一个向量函数
- 向量函数'at'