如何将dlib中的矩阵转换为std::向量
how to convert a matrix in dlib to a std::vector
我在dlib中定义了一个列向量。如何将其转换为std::vector?
typedef dlib::matrix<double,0,1> column_vector;
column_vector starting_point(4);
starting_point = 1,2,3,4;
std::vector x = ??
感谢
有很多方法。你可以通过for循环复制它。或者使用std::vector构造函数,它接受迭代器:std::vector<double> x(starting_point.begin(), starting_point.end())
。
这将是您通常对矩阵进行迭代的方式(如果矩阵只有1列,则无关紧要):
// loop over all the rows
for (unsigned int r = 0; r < starting_point.nr(); r += 1) {
// loop over all the columns
for (unsigned int c = 0; c < starting_point.nc(); c += 1) {
// do something here
}
}
那么,为什么不迭代列向量,并将每个值引入到新的std::vector
中呢?下面是一个完整的例子:
#include <iostream>
#include <dlib/matrix.h>
typedef dlib::matrix<double,0,1> column_vector;
int main() {
column_vector starting_point(4);
starting_point = 1,2,3,4;
std::vector<double> x;
// loop over the column vector
for (unsigned int r = 0; r < starting_point.nr(); r += 1) {
x.push_back(starting_point(r,0));
}
for (std::vector<double>::iterator it = x.begin(); it != x.end(); it += 1) {
std::cout << *it << std::endl;
}
}
相关文章:
- 将无符号char*转换为std::istream*C++
- 可以有效地转换 std::any 与 std::any_cast
- 尝试转换 std::chrono::d uration 会导致"rep cannot be a duration"编译错误
- 封送处理 - 转换 std::向量<char>到字符串^ 反之亦然
- 在 C++ 中转换 std::string,使其可以是 C# 中的 byte[]
- 转换 std::vector<T> 为 char *
- 转换std ::绑定到功能指针
- 强制转换 std::vector 的所有成员
- 无法转换'std::string{又名std::basic_string<char>}'到&
- 转换std::string到cv::Mat对象
- 转换std::chrono::system_clock::time_point::min()为字符串时,无效空指针错误
- 简单的模板化函数,用于转换 std::vectors - "illegal use of this type as an expression"
- Const转换std容器
- 如何强制转换std::make_unique,以便可以使用在类中声明的函数
- 如何向下转换std::shared_ptr
- 转换 std::vector<uint8_t> 为 QImage
- 使用std::dynamic_pointer_cast向上转换std::shared_ptr
- 如何转换std::vector<无符号字符>到向量<char>而无需复制
- 正在强制转换std::pair
const&T1 const, T2>const&安全的 - 如何在boost.python中转换std::string*