Caroline小蝎 2025-03-21 15:19 采纳率: 0%
浏览 5
已结题

c++:用array构造vector时报错

#include <vector>

using namespace std;

int main()
{
    int arr[5] = { 1,2,3,4,5 };
    vector<int> vec(arr, arr + 5);
    return 0;
}

错误信息:
no instance of constructor "std::vector<_Ty, _Alloc>::vector [with _Ty=int, _Alloc=std::allocator]" matches the argument list

版本:
Visual Studio 2022 Community

  • 写回答

2条回答 默认 最新

  • 道友老李 JWE233286一种基于机器视觉的水表指针读数识别及修正的方法 专利发明者 2025-03-21 15:19
    关注
    让【道友老李】来帮你解答,本回答参考gpt编写,并整理提供,如果还有疑问可以点击头像关注私信或评论。
    如果答案让您满意,请采纳、关注,非常感谢!
    这个错误信息表明在创建vector对象时,没有找到匹配的构造函数。在这段代码中,arr数组被传递给vector构造函数,但是vector类并没有接受这种参数类型的构造函数。 修复方法是将arr数组转换为指针类型,如下所示:
    #include <vector>
    using namespace std;
    int main() {
        int arr[5] = {1, 2, 3, 4, 5};
        vector<int> vec(arr, arr + 5);
        return 0;
    }
    

    这样,在传递参数的时候就能匹配到vector类的构造函数了。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月21日
  • 修改了问题 3月21日
  • 创建了问题 3月21日