帮忙修改一下程序,谢谢
# include <iostream># include <vector>
using namespace std;
vector <int>::const_iterator find_val ( vector <int>::const_iterator beg , vector <int>::const_iterator end,int value , vector <int>::size_type &occurs);
void main()
{ int i ;
vector<int>::size_type ctr;
/*vector<int> vec;
*cout << "please enter num "<<endl;
*while (cin>>i)
*{ if ( i = 0) 有朋友说 i=0 是错的,到底怎么错呢? 是因为如果输入的数字不是0就退出while循环,没有给容器添加元素? 还是语法错误呢?
* break;
* else 怎么 实现 给容器vec添加一定元素(数字)后,输入一个操作退出while循环?
* vec.push_back(i);
* }
*/
int num[] = {1, 2, 3, 8, 9, 4, 5, 8, 7, 8}
vector<int> vec (num, sizeof(num)/sizeof(num([0]));
vector<int> ::const_iterator iter;
iter = find_val ( vec.begin() ,vec.end(), 8,ctr);
cout << *iter << ctr <<endl;
}
vector <int>::const_iterator find_val ( vector <int>::const_iterator beg , vector <int>::const_iterator end,int value , vector <int>::size_type &occours)
{
vector <int> ::const_iterator res_iter = end;
occours = 0;
for ( ; beg != end; ++end)
{ if (*beg == value ){
if ( res_iter == end)
res_iter = beg;
++occours;
}
}
return res_iter;
}
编译没有错误,就是不能显示ctr,和迭代器,我的目的是定义既返回一个迭代器又返回出现满足要求元素次数的函数!!!
[[italic] 本帖最后由 bclee 于 2008-1-22 15:39 编辑 [/italic]]