调整 2D 矢量大小 C++
在 C++ 中,我们可以使用两个一维向量创建一个 2D 向量。 第一个一维向量指定二维平面上的点,而第二个一维向量指定从该点开始的角度或旋转。
在这篇短文中,我们将重点讨论如何在 C++ 中调整 2d 向量的大小。
在 C++ 中调整 2D 矢量大小
要在 C++ 中调整二维向量的大小,我们需要使用名为 resize() 的函数,该函数是 STL 库的一部分。
句法:
vector::resize()
resize()
方法有两个参数,两个参数都是整数。 第一个整数指定向量的新长度,第二个整数指定其新宽度。
要调整向量的大小,我们需要使用两个大于或等于 0 的参数值来调用此函数。例如,如果我们希望 2d 向量为 10 像素长和 5 像素宽,我们将使用 长度值为 10,宽度值为 5。
第一个示例代码:
#include <iostream>
#include <vector>
using namespace std;
#define X 1
#define Y 7
int main()
{
int demo = 4;
vector<vector<int>> sam;
cout << "Vector contains:" << sam.size() << endl;
sam.resize(X, vector<int>(Y, demo));
cout << "Vector contains after resize:" << sam.size() << endl;
return 0;
}
输出:
Vector contains:0
Vector contains after resize:1
第二个示例代码:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> demovec;
//set vector content
for (int x = 2; x < 20; x++) demovec.push_back(x);
demovec.resize(2);
demovec.resize(9);
cout << "Vector contains:" << endl;
for (int x = 0; x < demovec.size(); x++)
cout << ' ' << demovec[x];
return 0;
}
输出:
Vector contains:
2 3 0 0 0 0 0 0 0
相关文章
Arduino 复位
发布时间:2024/03/13 浏览次数:315 分类:C++
-
可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。
Arduino 的字符转换为整型
发布时间:2024/03/13 浏览次数:181 分类:C++
-
可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。
Arduino 串口打印多个变量
发布时间:2024/03/13 浏览次数:381 分类:C++
-
可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。