C++将列表与 count() 函数一起使用
我有一个列表 L
,它需要计算其中有多少个 1。
list<int> L;
L.push_back(14); L.push_back(5); L.push_back(22);
L.push_back(1); L.push_back(1); L.push_back(-7);
我得到的功能是:
assert ( count(...,...,...) == 2);
我需要知道什么可以替换 ...
的。
我已经尝试使用 L.begin(), L.end(), 1
来替换 ...
,但它一直给我一个错误,说这是不允许的。所以我需要替换 ...
而不添加任何额外的代码。
这是我收到的错误:
错误 C2782: 'iterator_traits<_Iter>::difference_type std::count(_InIt,_InIt,const _Ty &)' : 模板参数'_InIt'是 模棱两可
这是确切的代码 &错误。
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <cassert>
using namespace std;
int main()
{
int A1[6] = {15,8,10,9,12,13};
vector<int> V(A1, A1+6);
list<int> L;
L.push_back(14); L.push_back(5); L.push_back(22);
L.push_back(1); L.push_back(1); L.push_back(-7);
count(L.begin(), L.end(), 1);
}
错误 C2782: 'iterator_traits<_Iter>::difference_type std::count(_InIt,_InIt,const _Ty &)' : 模板参数'_InIt'是 模棱两可
c:\program files\microsoft Visual 工作室 9.0\vc\include\algorithm(160) : 请参阅“std::count”的声明 1>
可以是“无符号整数”
I have a list L
which needs to count how many 1s it has in it.
list<int> L;
L.push_back(14); L.push_back(5); L.push_back(22);
L.push_back(1); L.push_back(1); L.push_back(-7);
the function that i have been given is :
assert ( count(...,...,...) == 2);
i need to know what would replace the ...
's.
i have tried L.begin(), L.end(), 1
to replace the ...
's but it keeps giving me an error saying that is not allowed. So i need to replace the ...
's without adding any extra code.
this is the error that i have been getting:
error C2782:
'iterator_traits<_Iter>::difference_type
std::count(_InIt,_InIt,const _Ty &)' :
template parameter '_InIt' is
ambiguous
Here is the exact code & error.
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <cassert>
using namespace std;
int main()
{
int A1[6] = {15,8,10,9,12,13};
vector<int> V(A1, A1+6);
list<int> L;
L.push_back(14); L.push_back(5); L.push_back(22);
L.push_back(1); L.push_back(1); L.push_back(-7);
count(L.begin(), L.end(), 1);
}
error C2782:
'iterator_traits<_Iter>::difference_type
std::count(_InIt,_InIt,const _Ty &)' :
template parameter '_InIt' is
ambiguousc:\program files\microsoft visual
studio 9.0\vc\include\algorithm(160) :
see declaration of 'std::count' 1>
could be 'unsigned int'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它应该是
std::count(L.begin(), L.end(), 1)
所以如果这不起作用,我只能说确保你#include < ;算法>
。这段代码在 VS2008 中为我编译:
it should be
std::count(L.begin(), L.end(), 1)
so if this doesn't work all I can say is make sure you#include <algorithm>
.This code compiles for me in VS2008:
确保使用命名空间 std;在所有包含之后并确保您也包含算法并且
那么 L.begin() 和 L.end() 应该可以正常工作
Make sure you put using namespace std; after all the includes and make sure you include algorithm also and
then L.begin() and L.end() should work without any problem
使用 std::count_if
http://www.sgi.com/tech/stl/count_if .html
这是一个更好的示例:
http://msdn.microsoft.com/en-我们/库/w2d7w2x2%28VS.80%29.aspx
Use std::count_if
http://www.sgi.com/tech/stl/count_if.html
Heres a beter sample:
http://msdn.microsoft.com/en-us/library/w2d7w2x2%28VS.80%29.aspx