C++将列表与 count() 函数一起使用

发布于 2024-08-06 19:00:48 字数 1384 浏览 2 评论 0原文

我有一个列表 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
ambiguous

c:\program files\microsoft visual
studio 9.0\vc\include\algorithm(160) :
see declaration of 'std::count' 1>
could be 'unsigned int'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

败给现实 2024-08-13 19:00:48

它应该是 std::count(L.begin(), L.end(), 1) 所以如果这不起作用,我只能说确保你 #include < ;算法>

这段代码在 VS2008 中为我编译:

#include <list>
#include <algorithm>
#include <cassert>
using namespace std;

int main()
{
    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(L.begin(), L.end(), 1) == 2);
}

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:

#include <list>
#include <algorithm>
#include <cassert>
using namespace std;

int main()
{
    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(L.begin(), L.end(), 1) == 2);
}
淡写薰衣草的香 2024-08-13 19:00:48

确保使用命名空间 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文