关于C++ vector erase的问题,初学者求助
#include<iostream>#include<string>#include<vector>usingnamespacestd;classDotCom{vecto...
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class DotCom
{
vector<int> locations;
public:
DotCom(int loc[ ]);
string check(int guess);
};
DotCom::DotCom(int loc[ ])
{
for(int i=0;i<3;i++)
locations.push_back(loc[i]);
}
string DotCom::check(int guess)
{
string result = "miss";
for (int i=0;i<locations.size();i++){
if (guess == locations[i]) {
result = "hit";
locations.erase(&locations[i]);
break;
}
}
return result;
}
string DotCom::check(int guess)
{
string result = "miss";
for( vector<int>::iterator it = locations.begin();it != locations.end(); it++){
if( guess == *it){
result = "hit";
locations.erase(it);
break;
}
}
return result;
}
int main()
{
int a[ ]={1,2,3};
DotCom one(a);
int guess;
int n = 0;
while(n != 3){
cout << "GUESS: ";
cin >> guess;
string result = one.check(guess);
if (result == "hit") n++;
if (n == 3)
cout<<"sank"<<endl;
else
cout << result <<endl;
}
return 0;
}
在VS2010下运行的错误提示是 error C2664: “std::_Vector_iterator<_Myvec> std::vector<_Ty>::erase(std::_Vector_const_iterator<_Myvec>)”: 不能将参数 1 从“int *”转换为“std::_Vector_const_iterator<_Myvec>”
error C2084: 函数“std::string DotCom::check(int)”已有主体
希望好心人可以解答一下 展开
#include <string>
#include <vector>
using namespace std;
class DotCom
{
vector<int> locations;
public:
DotCom(int loc[ ]);
string check(int guess);
};
DotCom::DotCom(int loc[ ])
{
for(int i=0;i<3;i++)
locations.push_back(loc[i]);
}
string DotCom::check(int guess)
{
string result = "miss";
for (int i=0;i<locations.size();i++){
if (guess == locations[i]) {
result = "hit";
locations.erase(&locations[i]);
break;
}
}
return result;
}
string DotCom::check(int guess)
{
string result = "miss";
for( vector<int>::iterator it = locations.begin();it != locations.end(); it++){
if( guess == *it){
result = "hit";
locations.erase(it);
break;
}
}
return result;
}
int main()
{
int a[ ]={1,2,3};
DotCom one(a);
int guess;
int n = 0;
while(n != 3){
cout << "GUESS: ";
cin >> guess;
string result = one.check(guess);
if (result == "hit") n++;
if (n == 3)
cout<<"sank"<<endl;
else
cout << result <<endl;
}
return 0;
}
在VS2010下运行的错误提示是 error C2664: “std::_Vector_iterator<_Myvec> std::vector<_Ty>::erase(std::_Vector_const_iterator<_Myvec>)”: 不能将参数 1 从“int *”转换为“std::_Vector_const_iterator<_Myvec>”
error C2084: 函数“std::string DotCom::check(int)”已有主体
希望好心人可以解答一下 展开
1个回答
展开全部
string DotCom::check(int guess)
{
string result = "miss";
for (int i=0;i<locations.size();i++){
if (guess == locations[i]) {
result = "hit";
locations.erase(&locations[i]);
break;
}
}
return result;
}
定义了两遍
{
string result = "miss";
for (int i=0;i<locations.size();i++){
if (guess == locations[i]) {
result = "hit";
locations.erase(&locations[i]);
break;
}
}
return result;
}
定义了两遍
追问
谢谢您,可是为什么用第一个check还是有error C2664呢?
追答
locations.erase(&locations[i]);
参数类型不对,没有提供指针类型,必须用迭代器
浙江启扬智能科技有限公司
2022-05-05 广告
2022-05-05 广告
选择arm开发板,可以参考一下几个点:1、确定一种CPU,去了解其核心架构,CPU资源有哪些,一些芯片厂商都会有说明的,可以去它们网站上看看;2、确定要使用什么操作系统,比如Linux啊,安卓这些等等;3、确定开发或者学习要用的哪些接口,比...
点击进入详情页
本回答由浙江启扬智能科技有限公司提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询