【发布时间】:2019-05-07 13:10:10
【问题描述】:
我基本上是在尝试使用矢量,但是它存在问题。顺便说一句,我要解决的问题是 USACO 2014 年 12 月铜问题 #4。代码在下面。
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int num,min,max;
cin>>num>>min>>max;
vector <pair<int,int> > cow;
for(int a=0;a<num;num++)
{
int temp;
int temp2;
cin>>temp>>temp2;
cow.push_back (temp2);
if(temp=="NS")
cow[a].second=0;
else if(temp=="S")
cow[a].second=1;
}
sort(cow.begin(),cow.end());
int count=0;
cout<<"Count="<<count<<endl;
for(int b=0;b<num;b++)
{
cout<<"Weight: "<<cow[b].first;
if(cow[b].second==0)
cout<<"Spots: NO"<<endl;
else if(cow[b].second==1)
cout<<"Spots: YES"<<endl;
}
}
预期的结果应该是向量应该按数字顺序排列,但我卡在第一步。此外,它给我的错误说:No matching member function for call to 'push_back'
我不知道如何处理这个问题,我也找不到任何关于类似问题的在线资源。有人可以帮忙吗?
*edit: 将 int temp 更改为 string temp
【问题讨论】:
-
cow是vector <pair<int,int> >而不是vector<int>。 -
cow.push_back (temp2);?你的意思是cow.push_back({ temp, temp2 });?此外,那些if (temp == "NS")比较不适用于int和字符串文字。 -
我刚刚添加了问题参考,供可能想要查看问题并查看它是否有帮助的人使用。老实说,这不是创建算法的问题,而是创建并将值输入向量的基本问题,所以我认为这并不重要。