vector在c++中用法
时间: 2024-06-22 19:01:33 浏览: 9
在C++中,`std::vector`是一个非常常用且灵活的动态数组容器,它可以存储任意类型的元素,并提供了动态内存管理功能。以下是一些基本的用法:
1. 定义和初始化:
```cpp
#include <vector>
std::vector<int> vec; // 创建一个空的vector,用于存储整数
std::vector<int> vec(5); // 创建一个包含5个默认值(0)的vector
vec = {1, 2, 3, 4, 5}; // 使用初始化列表填充vector
```
2. 动态添加或删除元素:
```cpp
// 添加元素到末尾
vec.push_back(6); // 添加整数6到vector的末尾
// 删除末尾元素
vec.pop_back(); // 删除最后一个元素
// 在指定位置插入元素
vec.insert(vec.begin(), 7); // 在起始位置插入整数7
```
3. 访问和操作元素:
```cpp
int element = vec; // 获取第三个元素
vec = 8; // 修改第一个元素为8
// 遍历vector
for (int i : vec) {
std::cout << i << " ";
}
```
4. 大小和容量:
```cpp
size_t size = vec.size(); // 获取vector的元素个数
size_t capacity = vec.capacity(); // 获取vector的可用存储空间大小
```
相关问题
Vector3d在C++中的使用方法
Vector3d是一个三维向量类,常用于三维图形相关的计算中。在C++中,可以使用以下方法来使用Vector3d类:
1. 引入Vector3d类库
```c++
#include <Eigen/Dense>
```
2. 定义Vector3d对象
```c++
Eigen::Vector3d v1(1.0, 2.0, 3.0); // 用(1.0, 2.0, 3.0)初始化一个Vector3d对象
Eigen::Vector3d v2; // 定义一个默认的Vector3d对象
```
3. 通过成员函数获取向量元素
```c++
double x = v1.x(); // 获取向量v1的x元素
double y = v1.y(); // 获取向量v1的y元素
double z = v1.z(); // 获取向量v1的z元素
```
4. 向量运算
```c++
Eigen::Vector3d v3 = v1 + v2; // 向量加法
Eigen::Vector3d v4 = v1 - v2; // 向量减法
Eigen::Vector3d v5 = v1 * 2.0; // 向量数乘
Eigen::Vector3d v6 = v1.cross(v2); // 向量叉乘
double dot = v1.dot(v2); // 向量点乘
double norm = v1.norm(); // 向量模长
```
以上是Vector3d在C++中的基本使用方法,希望对你有所帮助。
Vector4d在C++中的用法
在C++中,Vector4d通常是由程序员自己定义的一个类或结构体。以下是一个简单的Vector4d类的示例:
```c++
class Vector4d {
public:
double x, y, z, w;
Vector4d(double x = 0, double y = 0, double z = 0, double w = 0) {
this->x = x;
this->y = y;
this->z = z;
this->w = w;
}
Vector4d operator+(const Vector4d& other) const {
return Vector4d(x + other.x, y + other.y, z + other.z, w + other.w);
}
Vector4d operator-(const Vector4d& other) const {
return Vector4d(x - other.x, y - other.y, z - other.z, w - other.w);
}
Vector4d operator*(double scalar) const {
return Vector4d(x * scalar, y * scalar, z * scalar, w * scalar);
}
double dot(const Vector4d& other) const {
return x * other.x + y * other.y + z * other.z + w * other.w;
}
Vector4d cross(const Vector4d& other) const {
return Vector4d(y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x, 0);
}
double norm() const {
return sqrt(x * x + y * y + z * z + w * w);
}
void normalize() {
double n = norm();
if (n > 0) {
x /= n;
y /= n;
z /= n;
w /= n;
}
}
};
```
上述代码定义了一个四维向量类Vector4d,它包括四个成员变量x、y、z和w,以及一些常用的向量运算方法,如加减乘除、点积、叉积、求模和归一化等。程序员可以使用这个类来进行四维向量的计算和操作,例如:
```c++
Vector4d v1(1, 2, 3, 0);
Vector4d v2(4, 5, 6, 0);
Vector4d v3 = v1 + v2;
Vector4d v4 = v1 - v2;
Vector4d v5 = v1 * 2;
double dot = v1.dot(v2);
Vector4d cross = v1.cross(v2);
double norm = v1.norm();
v1.normalize();
```
以上代码演示了如何创建两个四维向量v1和v2,以及如何使用Vector4d类中的各种方法对它们进行操作和计算。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)