这里第一次是业余程序员,我有很多问题。我想做一个虚拟模型商店。我可以在向量中添加新项目,检查广告资源,但是当我想删除特定项目时(producer
& name
& scale
),它会删除最后一项元件。
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class model
{
private:
int price;
string scale, name, producer;
public:
vector<model> vectorr;
model();
virtual ~model();
int getprice();
string getscale();
string getname();
string getproducer();
void setprice(int);
void setscale(string);
void setname(string);
void setproducer(string);
friend ostream &operator>>(ostream &stream, model &mt);
model &operator=(model &mt);
};
这就是标题。
#include <iostream>
#include "header1.h"
#include <string>
#include <conio.h>
#include <vector>
using namespace std;
int main()
{
vector<machete> vectorr;
machete c;
machete t;
cout << "Welcome to the shop , what do you want to do?" << endl;
cout << "Chose your destiny : 1. Add 2.Get 3.Remove 4.Sort 0.Quit" << endl;
int menu, temp2;
string temp;
cin >> menu;
while (menu != 0) {
switch (menu) {
case 0 : cout << "Bye bye";
_getch();
exit(1);
break;
case 1 : cout << "Enter a producer" << endl;
cin >> temp;
c.setproducer(temp);
cout << "Enter a name" << endl;
cin >> temp;
c.setname(temp);
cout << "Enter a scale" << endl;
cin >> temp;
c.setscale(temp);
cout << "Enter a price" << endl;
cin >> temp2;
c.setprice(temp2);
vectorr.push_back(c);
cout << "You have succesfully added a model !" << endl;
break;
case 2 : cout << "Producer | Name | Scale | Price" << endl;
for (std::vector<model>::iterator it = vectorr.begin(); it != vectorr.end(); ++it) {
cout >> *it;
}
cout << endl;
break;
case 3 : cout << "What do you want to remove?" << endl;
cout << "Enter the producer" << endl;
string producer;
cin >> producer;
cout << "Enter the name" << endl;
string name;
cin >> name;
cout << "Enter the scale" << endl;
string scale;
cin >> scale;
for (unsigned i = 0; i < vectorr.size(); ++i) {
t.setname(vectorr[i].getnume());
t.setproducer(vectorr[i].getproducator());
t.setscale(vectorr[i].getscara());
if (t.getname() == name && t.getproducer() == producer && t.getscle() == scale) {
vectorr.erase(vectorr.begin() + i);
cout << "You have succesfully erased the " << name << " model" << endl;
break;
}
}
break;
}
cout << "Chose your destiny : 1. Add 2.Get 3.Remove 4.Sort 0.Quit" << endl;
cin >> menu;
}
return 0;
}
这是主要的。 case 3
是删除thingamajack
的问题。它总是删除向量中的最后一个元素。 if(name = name等)工作正常,测试三次以确保不是问题。无论我写什么vectorr.erase
,它都会按照我想要的方式运行。
对于凌乱的代码感到抱歉,我可能在case 3
之前做错了什么,忽略case 4
排序,还没有实现,感谢任何帮助,谢谢!
答案 0 :(得分:0)
在案例2中,您已经写过
std::vector<model>::iterator it = vectorr.begin()
而你定义了
vector<machete> vectorr;
我认为你在大砍刀和模特之间感到困惑。