c++ - 如何将 vector 的一个元素作为 C++ 中的引用传递给函数?

标签 c++ multithreading boost vector

我试图在 C++ 的循环中操作一个 0 初始化 vector 的值。 然而,当我打印出 vector 的值时,它只显示“myFunction”中的合理值。在它之外,值没有改变。 这是我的示例的草图:

vector<float> meanDistances(numDescriptors);  

for (int i = 0; i < numDescriptors, i++)  
{
    myFunction(arg1, arg2,...,meanDistances[i]));
    cout << "meanDistance OUTSIDE myFunction: " << meanDistances[i] << endl;
}

这是“myFunction”的样子:

void myFunction(arg1, arg2, ..., float & meanDistance)
{
    meanDistance = someFloatNumber;
    cout << "meanDistance INSIDE myFunction: " << meanDistance << endl;
}

如何将 vector 的单个元素作为函数的引用传递?

编辑:
这里还有一些代码

boost::thread_group threadGroup;
for (int i = 0; i < numDescs ; i++ )
{
    int threadIdx = i % numThreads;

    vecHists[i].convertTo(vecHists[i], CV_32F);
    threadGroup.create_thread(boost::bind( &myFunction, vecHists[i],vecHists[i],vecDistanceMats[threadIdx], meanDistances[i]));



    sleep(1);

    if(threadIdx == numThreads-1 || i == numDescs-1)
    {
        threadGroup.join_all();
        for (int j = 0; j < numThreads ; j++)
        {
            sumDistanceMats += vecDistanceMats[j];
        }
    }
}
for (int i = 0; i < numDescs ; i++ )
{
  cout << "meanDistances OUTSIDE myFunction after all threads are joined again: " << meanDistances[i] << endl;
}

编辑2:
事实证明,这个函数的问题在于 boost::threadGroup 的功能。 .如果我交换 vector<float> meanDistances通过 1x1 dim opencv Mats 的 vector vector vector<cv::Mat> meanDistances一切正常。
当然我也在修改myFunction因此:

void myFunction(arg1, arg2, ..., Mat& matMeanDistance)
{
    meanDistance.at<float>(0,0) = someFloatNumber;
    cout << "meanDistance INSIDE myFunction: " << matMeanDistance.at<float>(0,0) << endl;
}

所以不知何故 boost::threadGroup处理 float 的 vector s 不同于 cv::Mat 的 vector

最佳答案

下面您将找到一个示例,说明如何通过引用(使用 range-for)或通过 std::transform 修改 vector 中的所有元素由 lambda 辅助的算法。

#include <vector>
#include <iostream>
#include <algorithm>

// utility function to print to screen
template<typename T>
void show(const T& v) {
  for(const auto & vi : v) {
    std::cout << vi << " ";
  }
  std::cout << std::endl;
}

// function used to modify by reference
void fun(double arg, double& vi) {
  vi += arg;
}

int main() {

  // initialize ten elements to 0.0
  std::vector<double> v(10, 0.0);
  std::cout << "Initialized as: " << std::endl;
  show(v);

  // modification using references in a loop
  for(auto & vi : v) {
    fun(2.0, vi);
  }
  std::cout << "Modified to: " << std::endl;
  show(v);

  // modification using an algorithm and a lambda (this one modifies by value)
  std::transform(v.begin(), v.end(), v.begin(),
                 [](double vi) { return vi + 3; });
  std::cout << "Modified using std::transform() and lambda" << std::endl;
  show(v);
}

编译运行:

$ g++ example.cpp -std=c++14 -Wall -Wextra
$ ./a.out
Initialized as:
0 0 0 0 0 0 0 0 0 0
Modified to:
2 2 2 2 2 2 2 2 2 2
Modified using std::transform() and lambda
5 5 5 5 5 5 5 5 5 5

关于c++ - 如何将 vector 的一个元素作为 C++ 中的引用传递给函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31522405/

相关文章:

.net - 线程C#中发生异常时,进程未终止

c# - 在 C# 中访问变量是原子操作吗?

c++ - 冲突的 boost 版本

c++ - 在 C 中将浮点指针重铸为双指针的最有效方法是什么?

c++ - 将 OpenSSL RSA key 与 .Net 结合使用

c++ - 从 png、winapi 创建透明图像

c# - 在另一个线程中调用函数 C#

c++ - 为什么 C++ 不允许参数是默认参数?

c++ - Boost 在 Boost packaged_task 中绑定(bind)。为什么 boost asio 认为它不是 CompletionHandler?

c++ - boost 异步服务器类 : Debug Assertion Failed and Empty Buffer