Vector.push_back(对<int,int>(x1,x2));不起作用

Vector.push_back(pair<int,int>(x1,x2 )); does not work

我使用 DLIB parallel_for 循环进行一些处理并将坐标添加到已在循环外声明的向量>。但是我不能在循环中使用 vector.push_back() 函数。

Verified whether there are any declaration issues. Passed the vector pointer to the parallel_for loop lambda function.

    //Store cordinates of respective face_image
    std::vector<pair<int,int>> xy_coords;

    //Create a dlib image window
    window.clear_overlay();
    window.set_image(dlib_frame);

    auto detections = f_detector(dlib_frame);

    dlib::parallel_for(0, detections.size(), [&,detections,xy_coords](long i)
        {
            auto det_face = detections[i];

            //Display Face data to the user
            cout << "Face Found! " << "Area: " << det_face.area() << "X: " <<det_face.left() << "Y: " << det_face.bottom() << endl;

            //Get the Shape details from the face
            auto shape = sp(dlib_frame, det_face);


            //Extract Face Image from frame
            matrix<rgb_pixel> face_img;
            extract_image_chip(dlib_frame, get_face_chip_details(shape, 150, 0.25), face_img);
            faces.push_back(face_img);
            //Add the coordinates to the coordinates vector

            xy_coords.push_back(std::pair<int,int>((int)det_face.left(),(int)det_face.bottom()));

            //Add face to dlib image window
            window.add_overlay(det_face);

        });

您的 lambda 正在通过副本捕获 xy_coords,您在 lamdba 内部推入的那个与外部不一样。尝试像 [&,&xy_coords,detections][&,detections].

这样的引用来捕获它

有关详细信息,请参阅此内容: https://en.cppreference.com/w/cpp/language/lambda#Lambda_capture