【发布时间】:2021-09-20 18:01:17
【问题描述】:
我要做的是创建一个函数,根据容器中列出的轨道类型计算物体的数量。由于这是在寻找某个参数,所以我使用了count_if。
这就是结构的设置方式。
struct body{
string name;
string cartype;
string tracktype;
string price;
string dayfin;
};
代码会询问您希望算法计算什么轨道类型,例如 DirtOval、ShortOval 等。但是,无论我做什么,我都无法让 count_if 考虑我的用户输入,并且需要帮助。到目前为止,这是我的代码。
int sum;
cout << "What track type?";
getline(cin, tempStr);
for(int i = 0; i <inventory.size(); i++){
sum = count_if(inventory.begin(), inventory.end(), tempStr);
}
cout << "There are " << sum << " number of bodies for this type of track in our inventory." << endl << endl;
【问题讨论】:
-
用 lambda en.cppreference.com/w/cpp/algorithm/count 看例子你用循环计算和的平方。