标签: find

c ++ std :: find自定义比较器不起作用

我现在正在学习如何使用std :: find和自定义比较器.

但是,通过在线指导,我面临编译器错误.

链接到我的代码.

以下是我的代码:

    #include <iostream>
    #include <algorithm>
    #include <pair.h>
    #include <vector>

    using namespace std;

    int main()
    {
        struct comp
        {
            comp(const int& input) : _input(input) {}
            bool operator()(const pair<int, int>& iPair)
            {
                return (iPair.first == _input);
            }
            int _input;
        };

        pair<int, int> pair1(1,3);
        pair<int, int> pair2(2,4);

        vector<pair<int, int> > vec;
        vec.push_back(pair1);
        vec.push_back(pair2);

        vector<pair<int,int> >::iterator it = find(vec.begin(), vec.end(), comp(1));
        if(it != vec.end())
        {
            cout << it->second << endl;
        }

        return 0;
    }
Run Code Online (Sandbox Code Playgroud)

错误如下:

In function …
Run Code Online (Sandbox Code Playgroud)

c++ stl vector find custom-compare

-3
推荐指数
1
解决办法
897
查看次数

在数组中查找重复项

int[] array = { 10, 5, 10, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 11, 12, 12 };
for (int i = 0; i < array.Length; i++)
{
    int count = 0;
    for (int j = 0; j < array.Length; j++)
    {
        if (array[i] == array[j])
        {
            count = count + 1;
        }
    }
    Console.WriteLine(array[i] + " repeats " + count + " times");
}
Console.ReadKey();
Run Code Online (Sandbox Code Playgroud)

输出

我不想使用 LINQ!我的问题是,如何只打印一次结果。例如:

10次​​重复2次
5次重复3次
2次重复2次
...

谢谢。

c# arrays find

-3
推荐指数
1
解决办法
2万
查看次数

为什么在 C++ 中查找数组中的元素最有效?

我需要一个快速的 STL 容器来查找其中是否存在元素,因此我测试了数组、向量、集合和无序集合。我认为集合针对查找元素进行了优化,因为值是唯一且有序的,但 1000 万次迭代最快的是:数组(0.3 秒)向量(1.7 秒)无序集合(1.9 秒)集合(3 秒)

这是代码:

#include <algorithm>
#include <iostream>
#include <set>
#include <unordered_set>
#include <vector>

int main() {
    using std::cout, std::endl, std::set, std::unordered_set, std::vector, std::find;
    
    int i;
    
    const long ITERATIONS = 10000000;
    
    int a[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
    for (int i = 0; i < ITERATIONS; i++) {
        if (find(a, a + 16, rand() % 64) == a + 16) {}
        else {} …
Run Code Online (Sandbox Code Playgroud)

c++ set find

-3
推荐指数
1
解决办法
97
查看次数

列表元素没有迭代

我想知道如何在没有迭代的情况下在列表中查找元素

python iteration list find

-7
推荐指数
2
解决办法
933
查看次数

if 条件中 find() c++ stl 中 == a.end() 的用途是什么?

我刚刚解决了一个 codeforces 问题,我在作者的教程解决方案中发现了有关 find() c++ stl 的新内容...但我无法理解。在这里,find(a.begin(), a.end(), s-i) == a.end()它是==a.end()做什么的?

(问题链接:http://codeforces.com/contest/1293/problem/A

//Author's tutorial solution
#pragma GCC optimize("Ofast")

#include <bits/stdc++.h>
using namespace std;

#define endl '\n'

int n, s, k;
vector<int> a;

void Input() {
    cin >> n >> s >> k; a.clear(); a.resize(k);
    for (auto &z: a) cin >> z;
}

void Solve() {
    for (int i=0; i<=k; i++) {
        if (s-i >= 1 && find(a.begin(), a.end(), s-i) == a.end()) {cout << i …
Run Code Online (Sandbox Code Playgroud)

c++ stl find

-8
推荐指数
1
解决办法
4041
查看次数

标签 统计

find ×5

c++ ×3

stl ×2

arrays ×1

c# ×1

custom-compare ×1

iteration ×1

list ×1

python ×1

set ×1

vector ×1