当前位置: 首页 > 工具软件 > iBack > 使用案例 >

C++中push_back()函数

颛孙庆
2023-12-01

push_back()函数的用法

函数将一个新的元素加到vector的最后面,位置为当前最后一个元素的下一个元素

push_back() 在Vector最后添加一个元素(参数为要插入的值)

//在vec尾部添加10
vector<int> vec;
vec.push_back(10);

//在容器中添加10
int num = 10;
vector<int> vec;
vec.push_back(num);

或者再string中最后插入一个字符;

string str;
str.push_back('d');
// 根据NCC代价矩阵计算各个视图的权重,同时选出最重要的那个视图
// last-上一次迭代中当前像素最重要的视图的ID
Eigen::ArrayXf ComputeViewWeight(cv::Mat cost, int iteration, const int refID, int &last, bool update=false)
{
    float init_good_threshold = 0.8;
    float alpha = 90;
    float beta = 0.3;
    float good_threshold = init_good_threshold * exp(-iteration * iteration / alpha);
    float bad_threshold = 1.2;
    int n1 = 2;
    int n2 = 3;
    vector<int> S_t;
    Eigen::Array<float, Eigen::Dynamic, 1> viewWeight; // 每个视图的权重,权重最大的被认为是最重要的视图
    viewWeight.resize(cost.cols);
    for (int i = 0; i < cost.cols; i++)
    {
        if (i == refID)
        {
            // viewWeight << 0.f;
            viewWeight(i) = 0;
            continue;
        }
        vector<float> S_good;
        vector<float> S_bad;

        for (int j = 0; j < cost.rows; j++)
        {
            float c = cost.at<float>(j, i);
            if (c < good_threshold)
                S_good.push_back(c);
            else if (c > bad_threshold)
                S_bad.push_back(c);
        }
        if (S_good.size() > n1 && S_bad.size() < n2)
            S_t.push_back(i);
        else
        {
            viewWeight(i) = 0.2 * (i == last);
            continue;
        }

        Eigen::Array<float, Eigen::Dynamic, 1> confidence;
        confidence.resize(S_good.size());
        for (int i = 0; i < confidence.size(); i++)
        {
            float c = S_good[i];
            float conf = exp(-c * c / (2 * beta * beta));
            confidence(i) = conf;
        }
        float weight = confidence.sum() / S_good.size();
        weight = ((i == last) + 1) * weight;
        viewWeight(i) = weight;
    }
    // 找到权重最高的view并更新last
    if(update)
    {
        Eigen::ArrayXf::Index maxIndex;
        viewWeight.maxCoeff(&maxIndex);
        last = maxIndex;
    }
    return viewWeight;
}

类似的:
pop_back() //移除最后一个元素
clear() //清空所有元素
empty() //判断vector是否为空,如果返回true为空
erase() // 删除指定元素

 类似资料: