如何定义向量结束以使其不会无休止地持续下去? [英] How to define vectors end so it won't go on endlessly?

查看:29
本文介绍了如何定义向量结束以使其不会无休止地持续下去?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个文本文件,其中包含有关不同音乐剧艺术家的信息.

At the moment I have a text file containing information pertaining to different musicals artists.

David Byrne 1 Talking_Heads Lead-Vocals
Chris Frantz 1 Talking_Heads Drummer
Tina Weymouth 3 Talking_Heads Compass_Point_All_Stars Tom_Tom_Club Bass

按这个顺序是姓名、姓氏、他们所在的乐队数量、他们所在的乐队,最后是乐队中的角色.在搜索人物时,乐队被放入一个要显示的向量中,但该向量并没有结束,因此当搜索 Tina Weymouth 时,例如它会显示前两个条目的乐队以及 Tina 的.

In this order it goes Forname,Surname,The number of bands they were in,The bands they where in and finally there role within the band. When searching for people the bands are put into a vector to be displayed but this vector is not ending so when searching for Tina Weymouth for example it will show the previous two entries bands as well as Tina's.

while (artist >> forname >> surname >> bandnum)
    {
        for (int i = 0; i < bandnum; i++)
        {
            string tmp;
            artist >> tmp;
            band.push_back(tmp);
        }
        artist >> role;


        if (strF == forname && strS == surname) {
            system("CLS");
            cout << "Artist found" << endl;
            cout << forname << " " << surname << " ";
            ostream_iterator<string> output_iterator(cout, " ");
            copy(band.begin(), band.end(), output_iterator);
            cout<< role << endl;
            system("pause");
        }
    }

上面是使用的代码,它应该在名称之前读取数字并制作一个包含每个波段的大小的向量,而不是制作一个无限的向量.

Above is the code used it should read the number before there names and make a vector that size which contains each band instead it is making an endless vector.

推荐答案

您覆盖了 fornamesurnamebandnumrole 每次循环,但您保留 band 向量以继续增长和增长 - 保留有关过去艺术家的信息.每次循环后都需要清理和清除它!

You overwrite the values of forname, surname, bandnum and role every loop, but you leave the band vector to keep growing and growing--keeping information about past artists. You need to clean and clear it after every loop!

简单调用clear() 就是你所需要的.只需在 while 循环的开始(或结束,如果您愿意)添加以下行:

A simple call to clear() is all you need. Just add the following line at the start (or end if you prefer) of your while loop:

band.clear();

它正在运行:ideone

这篇关于如何定义向量结束以使其不会无休止地持续下去?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆