我想知道是否有一种方法可以将Armadillo向量转换为std字符串。例如,如果我有这个矢量:
arma::vec myvec("1 2 3"); //create a vector of length 3
如何生产:
std::string mystring("1 2 3");
从中?
使用ostringstream、.raw_print((、.st((和.substr((的组合,如下所示。
// vec is a column vector, or use rowvec to declare a row vector
arma::vec myvec("1.2 2.3 3.4");
std::ostringstream ss;
// .st() to transpose column vector into row vector
myvec.st().raw_print(ss);
// get string version of vector with an end-of-line character at end
std::string s1 = ss.str();
// remove the end-of-line character at end
std::string s2 = s1.substr(0, (s1.size() > 0) ? (s1.size()-1) : 0);
如果您想更改格式,请查看fmtflags:
std::ostringstream ss;
ss.precision(11);
ss.setf(std::ios::fixed);
这应该有效:
std::ostringstream s;
s << myvec;
std::string mystring = s.str();