How do I return a vector such that I can access its values after the function call in C++?

I am trying to create an input tensor for a machine learning model using the ONNX Runtime. This is returned as a vector<Ort::Value>. After calling the function, the objects inside the vector are destroyed (I assume because the function scope ended and they get cleaned up).
My question doesn’t really concern the use of ONNX but is meant more generally on how to deal with more complex classes.

I wonder how I can call the function to access the values after the function has finished.

In my main function I create some setup code and call a function to create the desired output of vector<Ort::Value>.
I tested the functionality with an output of vector<std::pair> and was able to get output as expected.

int main() {
    // Create ONNX Session (Setup Code)
    std::string onnxFilePath = "model.onnx";
    std::string instanceName = "onnxInstance";
    Ort::Env onnxEnv{OrtLoggingLevel::ORT_LOGGING_LEVEL_WARNING, instanceName.c_str()};
    Ort::SessionOptions sessionOptions;
    sessionOptions.SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_ALL);
    Ort::Session onnxSession{onnxEnv, ORT_TSTR(onnxFilePath.c_str()), sessionOptions};

    // Read an Image
    std::string imageFilePath = "000000.jpg";
    cv::Mat img = cv::imread(imageFilePath, cv::IMREAD_COLOR);


    // Test return values with std::pair
    std::vector<std::pair<int, double>> pairs1 = returnPair(img);
    // First few values as expected:
    // (0, 0.840188), (1, 0.394383), (2, 0.783099), (3, 0.79844), (4, 0.911647),

    std::vector<std::pair<int, double>> pairs2;
    overwritePair(img, pairs2);
    // First few values as expected:
    // (0, 0.489059), (1, 0.967357), (2, 0.22906), (3, 0.109976), (4, 0.168318),

    // Get the Input Tensors
    std::vector<Ort::Value> tensors1 = returnTensor(onnxSession, img);
    // First few values wrong:
    // 2.02411e-38, 0, 2.01868e-38, 0, 0,
    // Expected:
    // 255, 244, 253, 245, 215, 

    std::vector<Ort::Value> tensors2;
    overwriteTensor(onnxSession, img, tensors2);
    // First few values wrong:
    // 2.02411e-38, 0, 2.01868e-38, 0, 0,
    // Expected:
    // 255, 244, 253, 245, 215, 

    // I wold like to access the tensors to call 
    // onnxSession::Run()
}

Following are the functions used in main():


std::vector<std::pair<int, double>> returnPair(cv::Mat &img){
    std::cout << "Function returnPair: " << std::endl;

    int input_size = img.rows * img.cols;
    std::vector<std::pair<int, double>> pairs;
    for(std::size_t i = 0; i < input_size; ++i){
        float r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX); 
        pairs.emplace_back(std::make_pair(i, r));
    }

    // sanity check to see values in function
    std::cout << "    ";
    for(std::size_t i = 0; i < 5; ++i){
        std::cout << "(" << pairs[i].first << ", " << pairs[i].second << "), ";
    }
    std::cout << std::endl << std::endl;

    return pairs;
}
void overwriteTensor(Ort::Session &onnxSession, cv::Mat &img, std::vector<Ort::Value> &tensors){
    std::cout << "Function returnTensor: " << std::endl;
    // resize to fit the onnx model
    cv::resize(img, img, cv::Size(640, 640));
    int input_size = img.rows * img.cols;
    // bring into the correct format
    cv::Mat preprocessedImage;
    cv::dnn::blobFromImage(img, preprocessedImage);
    std::vector<float> img_values(input_size);
    img_values.assign(preprocessedImage.begin<float>(), preprocessedImage.end<float>());

    // show the image values
    std::cout << "    image values: " << std::endl;
    std::cout << "    ";
    for(std::size_t i = 0; i < 5; ++i){
        std::cout << img_values[i] << ", ";
    }
    std::cout << std::endl;

    // get input name and shape
    Ort::AllocatorWithDefaultOptions allocator;

    std::vector<std::string> input_names;
    std::vector<std::int64_t> input_shapes;
    for(std::size_t i = 0; i < onnxSession.GetInputCount(); i++) {
        input_names.emplace_back(onnxSession.GetInputNameAllocated(i, allocator).get());
        input_shapes = (onnxSession.GetInputTypeInfo(i).GetTensorTypeAndShapeInfo().GetShape());
    }

    // create tensors
    Ort::MemoryInfo memInfo = Ort::MemoryInfo::CreateCpu(OrtAllocatorType::OrtArenaAllocator, OrtMemType::OrtMemTypeDefault);


    /* THESE ARE THE VALUES I'D LIKE TO ACCESS */
    tensors.emplace_back( Ort::Value::CreateTensor<float>(memInfo, 
                    img_values.data(), img_values.size(), 
                    input_shapes.data(), input_shapes.size()) );


    // sanity check to see tensors value
    float* tensor_values = tensors[0].GetTensorMutableData<float>();
    std::cout << "    tensor values: " << std::endl;
    std::cout << "    ";
    for(std::size_t i = 0; i < 5; ++i){
        std::cout << tensor_values[i] << ", ";
    }
    std::cout << std::endl << std::endl;

}

With the analog functionalities for

std::vector<Ort::Value> returnTensor(Ort::Session &onnxSession, cv::Mat &img)

and

void overwritePair(cv::Mat &img, std::vector<std::pair<int, double>> &pairs)

Why do the functions return the result I desire for the std::pair but not for the Ort::Value vectors?

And how do I write functions correctly to have access to the values after the function call has ended?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật