C++设置擦除条目问题
我在这里遇到了问题。我正在使用 C++ 多重集。这是测试文件。
Score: 3-1
Ben
Steven
Score: 1-0
Ben
Score: 0-0
Score: 1-1
Cole
Score: 1-2
Ben
我正在使用 while 循环和 ifstream (fin1) 从上面的测试文件中读取。
multiset<string, less<string> > myset;
while(!fin1.eof())
{
fin1 >> scoreName;
if(scoreName == "Score:")
{
//calculates number of matches played
}
else
{
goalCheck = scoreName.substr(1,1);
if(goalCheck == "-")
{
string lGoal, rGoal;
lGoal = scoreName.substr(0,1);
rGoal = scoreName.substr(2,1);
int leftGoal, rightGoal;
leftGoal = atoi(lGoal.c_str());
rightGoal = atoi(rGoal.c_str());
if(leftGoal > rightGoal) //if team wins
{
//some computations
}
else if(leftGoal < rightGoal) //if team loses
{
//computations
}
else if(leftGoal == rightGoal) //if team draws
{
//computations
}
else
{
myset.insert(myset.begin(), scoreName);
}
}
}
我在最后一个 else 语句中将所有名称插入到 myset 中(无论获胜/失败/平局)。但我只需要获胜/平局的比赛名称。
那些输掉比赛的名字将不会被包含在我的集合中。在上面的测试文件中,只有一场比赛输了(1-2),我想删除“Ben”。我怎样才能做到这一点?
我尝试使用 myset.erase(),但我不确定如何让它指向 Ben 并将其从 myset 中删除。
非常感谢任何帮助。 谢谢。
I encountered a problem here. I'm using C++ multiset. This is the test file.
Score: 3-1
Ben
Steven
Score: 1-0
Ben
Score: 0-0
Score: 1-1
Cole
Score: 1-2
Ben
I'm using while loop and ifstream (fin1) to read in from the test file above.
multiset<string, less<string> > myset;
while(!fin1.eof())
{
fin1 >> scoreName;
if(scoreName == "Score:")
{
//calculates number of matches played
}
else
{
goalCheck = scoreName.substr(1,1);
if(goalCheck == "-")
{
string lGoal, rGoal;
lGoal = scoreName.substr(0,1);
rGoal = scoreName.substr(2,1);
int leftGoal, rightGoal;
leftGoal = atoi(lGoal.c_str());
rightGoal = atoi(rGoal.c_str());
if(leftGoal > rightGoal) //if team wins
{
//some computations
}
else if(leftGoal < rightGoal) //if team loses
{
//computations
}
else if(leftGoal == rightGoal) //if team draws
{
//computations
}
else
{
myset.insert(myset.begin(), scoreName);
}
}
}
I'm inserting all names into myset (regardless of wins/loses/draws) in my last else statement. But I only require the names of those matches who won/draw.
Those names whose matches lost will not be included in myset. In the test file above, there's only one match that lost (1-2) and I wanted to remove "Ben". How can I do that?
I tried to use myset.erase(), but I'm not sure how to get it point to Ben and remove it from myset.
Any help is much appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我明白你想要做什么,我认为当你阅读“得分”行并且只插入以下行(即“本”)时,会更容易记住球队是赢了,平了还是输了球队还没有输。插入所有内容,然后删除您不需要的内容对我来说似乎过于复杂:)
供参考:如果您确实需要删除,则可以使用
find()
成员来查找与给定键匹配的条目,然后在返回的迭代器上使用erase()
将其删除(在检查find()
没有返回等于end()
的迭代器之后) code>,这意味着未找到该项目)。另外,您不应该将
begin()
传递给insert()
。迭代器是关于地图可能插入项目的位置的提示;实际上这很少有用。该函数有一个重载,它只接受一个参数,即要插入的项。If I understand what you're trying to do, I think it would be easier to remember whether the team had won, drawn or lost when you read the "Score" line and only insert the following lines (ie. "Ben") if the team hasn't lost. Inserting everyone and then erasing ones you didn't want seems overcomplicated to me :)
For reference: If you do need to erase, you would use the
find()
member to locate an entry matching a given key, and thenerase()
on the returned iterator to delete it (after checking thatfind()
didn't return an iterator equal toend()
, which means the item wasn't found).Also, you shouldn't pass
begin()
toinsert()
. The iterator is a hint as to where the map might insert the item; in practice that's rarely useful. There is an overload of that function which takes only one argument, the item to insert.创建一个
Score
类。将其添加为非成员operator>>()
,以便您可以轻松解析它。然后您就可以轻松决定是否将Score
对象插入集合中:Create a
Score
class. Add it a non-memberoperator>>()
so you can parse it easily. Then it will be easy for you decide whether to insert aScore
object into the set or not: