Python Numpy Array to C++ Vector
作者:XD / 发表: 2021年9月10日 12:41 / 更新: 2021年9月10日 12:49 / 编程笔记 / 阅读量:3297
Python Numpy Array to C++ Vector
# Save the Numpy in a txt.
import numpy as np
s = ''
data = np.load("data.npy")
c, h, w = data.shape
data = data.reshape(c*h*w)
for v in data:
s += str(v) + '\t'
path = "data.txt"
f = open(path, "w+")
f.write(s)
// load the txt file and save in a float vector
#include < iostream>
#include < fstream>
#include < string>
#include < vector>
using namespace std;
std::string txtPath = "data.txt";
ifstream InFile;
InFile.open (txtPath);
std::vector data_vector;
if (InFile)
{
string line;
float number;
for (int i = 0; i < 1; ++i)
{
getline (InFile, line);
istringstream iss(line);
while (iss >> number)
{
data_vector.push_back (number);
}
}
InFile.close ();
InFile.clear ();
}
else
throw runtime_error ("document error");