使用数组向量的正确方法 [英] Correct way to work with vector of arrays

查看:16
本文介绍了使用数组向量的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能告诉我处理数组向量的正确方法是什么吗?

Could someone tell what is the correct way to work with a vector of arrays?

我声明了一个数组向量(vector)但得到错误:请求从int"到非标量类型float [4]"的转换 尝试 resize 时.出了什么问题?

I declared a vector of arrays (vector<float[4]>) but got error: conversion from 'int' to non-scalar type 'float [4]' requested when trying to resize it. What is going wrong?

推荐答案

您不能将数组存储在 vector 或任何其他容器中.要存储在容器中的元素类型(称为容器的值类型)必须既可复制构造又可赋值.数组都不是.

You cannot store arrays in a vector or any other container. The type of the elements to be stored in a container (called the container's value type) must be both copy constructible and assignable. Arrays are neither.

但是,您可以使用 array 类模板,例如 Boost、TR1 和 C++0x 提供的模板:

You can, however, use an array class template, like the one provided by Boost, TR1, and C++0x:

std::vector<std::array<double, 4> >

(您需要将 std::array 替换为 std::tr1::array 以使用 C++ TR1 中包含的模板,或 boost::array 使用 来自 Boost 库的模板. 或者,您可以自己编写;这非常简单.)

(You'll want to replace std::array with std::tr1::array to use the template included in C++ TR1, or boost::array to use the template from the Boost libraries. Alternatively, you can write your own; it's quite straightforward.)

这篇关于使用数组向量的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆