【问题标题】:How to correctly resize a complex vector?如何正确调整复杂向量的大小?
【发布时间】:2021-01-30 23:35:05
【问题描述】:

我有这样的课:

class A {
    std::vector<
        std::vector<
            std::pair<
                uint32_t, std::reference_wrapper<std::vector<uint8_t>>>>>
        table;
};

在类的构造函数中,我调整了向量的大小,就像table.resize(indexSize)

但是当我尝试将项目推入时出现错误:

void A::insertItem(const uint32_t &g, const vector<uint8_t> &point)
{
    this->table[g % this->indexSize].push_back(make_pair(g, point));
}

我想当我调整大小时,我需要传递某种构造函数?

我得到的错误是:

没有重载函数实例“std::vector<_tp _alloc>::push_back [with _Tp=std::pair>>>, _Alloc=std::allocator<:pair std::reference_wrapper std::allocator>>>>>]" 匹配参数列表 --参数类型是: (std::pair>>) -- 对象类型是:std::vector<:pair std:: reference_wrapper std::allocator>>>>, std::allocator<:pair std::reference_wrapper std::allocator>>>>>>

【问题讨论】:

  • 你得到什么错误?也请发Minimal, Reproducible Example
  • @MikeCAT 刚刚添加了错误,对于一个可重现的示例,它实际上只是我发布的内容加上我执行 table.resize(/*with given indexSize*/) 的构造函数。跨度>

标签: c++ vector resize reference-wrapper


【解决方案1】:

这个例子是函数式的:

#include <vector>
#include <functional>

int main(int, const char**) {
    std::vector<uint8_t> abc = {5, 6};
    std::vector<
        std::vector<
            std::pair<uint32_t, std::reference_wrapper<std::vector<uint8_t>>>
            >
        > table;
    table[0].push_back(std::make_pair(4, std::reference_wrapper<std::vector<uint8_t>>(abc)));
    return 0;
}

你的对的第二个参数有一个需要参数的构造函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多