注册 登录
编程论坛 C++教室

关于vector的数据类型错误

CooperOne 发布于 2012-03-17 10:47, 482 次点击
程序代码:
#include <iostream>
#include <vector>
using namespace std;
vector<int> v;

bool vec(vector<int>::iterator beg,vector<int>::iterator end,int i)
{
    for(vector<int>::size_type ix=0;beg!=end;beg++)
    {
        cout<<"tips"<<endl;
        if(*beg==i)
        {
        cout<<"got it"<<endl;
        return true;
        }
    }
    cout<<"not exist"<<endl;
    return false;
}

int main()
{
int a,p;
while(cin>>a)
{
         v.push_back(a);   
}
vector<int>::iterator beg=v.begin();
vector<int>::iterator end=v.end();
cout<<"write a ..."<<endl;
vec(beg,end,cin>>p);
return 1;
}
调用vec函数时出错
error C2664: 'vec' : cannot convert parameter 3 from 'class std::basic_istream<char,struct std::char_traits<char> >' to 'int'
谁能解释下为什么不?
3 回复
#2
CooperOne2012-03-17 11:15
人工顶一下
#3
rjsp2012-03-17 12:02
感觉你一窍不通

#include <iostream>
#include <vector>
using namespace std;

bool vec( const vector<int>::const_iterator& beg, const vector<int>::const_iterator& end, int val )
{
    for( vector<int>::const_iterator itor=beg; itor!=end; ++itor )
    {
        if( *itor == val )
        {
            cout << "got it" << endl;
            return true;
        }
    }
    cout << "not exist" << endl;
    return false;
}

int main()
{
    vector<int> v;
    for( int a; cin>>a; )
    {
        v.push_back(a);   
    }
    cin.clear();
    cin.ignore();

    cout<<"write a ..."<<endl;
    int p;
    cin >> p;

    vec( v.begin(), v.end(), p );

    return 0;
}
#4
BianChengNan2012-03-17 12:20
自己要学会看编译器给的提示。楼上已经给了解答。

需要注意的是 vec(beg,end,cin>>p);
红色部分的返回值会作为参数传给vec函数。所以就报那个错了。

楼主加油
1