C++ 转换向量<;双倍>;到向量<;字符串>;(优雅的方式)
我想知道是否有一种优雅的方法或内置函数将C++ 转换向量<;双倍>;到向量<;字符串>;(优雅的方式),c++,string,vector,double,C++,String,Vector,Double,我想知道是否有一种优雅的方法或内置函数将vector转换为vector。我所做的很简单 #include <iostream> #include <string> #include <vector> #include <sstream> std::vector<std::string> doubeVecToStr(const std::vector<double>& vec) { std::vector&
vector
转换为vector
。我所做的很简单
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
std::vector<std::string> doubeVecToStr(const std::vector<double>& vec)
{
std::vector<std::string> tempStr;
for (unsigned int i(0); i < vec.size(); ++i){
std::ostringstream doubleStr;
doubleStr << vec[i];
tempStr.push_back(doubleStr.str());
}
return tempStr;
}
int main( int argc, char* argv[] )
{
std::vector<double> doubleVec;
doubleVec.push_back(1.0);
doubleVec.push_back(2.1);
doubleVec.push_back(3.2);
std::vector<std::string> doubleStr;
doubleStr = doubeVecToStr(doubleVec);
for (unsigned int i(0); i < doubleStr.size(); ++i)
std::cout << doubleStr[i] << " ";
std::cout << std::endl;
return 0;
}
#包括
#包括
#包括
#包括
std::vector doubeVecToStr(const std::vector&vec)
{
std::vector tempStr;
for(无符号整数i(0);i doubleStr有很多方法,但标准解决方案是使用lambda进行转换:
std::transform(std::begin(doubleVec),
std::end(doubleVec),
std::back_inserter(doubleStr),
[](double d) { return std::to_string(d); }
);
您可以将其包装在函数模板中,使其与任何符合标准的容器一起工作:
template<class IteratorIn, class IteratorOut>
void to_string(IteratorIn first, IteratorIn last, IteratorOut out)
{
std::transform(first, last, out,
[](typename std::iterator_traits<IteratorIn>::value_type d) { return std::to_string(d); } );
}
注意事项:
- 如果您没有C++11编译器,请将自己的
编写到\u string
函数模板:
例如:
template<class T>
std::string my_to_string(T v)
{
std::stringstream ss;
ss << v;
return ss.str();
}
使用复制和ostream\u迭代器:
#include <vector>
#include <iostream>
#include <sstream>
#include <iterator>
int main()
{
std::vector<double> numbers{1.0, 2.1, 3.2};
std::stringstream output;
std::copy(numbers.begin(), numbers.end(), std::ostream_iterator<double>(output, " "));
std::cout << output.str() << std::endl;
}
#包括
#包括
#包括
#包括
int main()
{
std::向量数{1.0,2.1,3.2};
std::stringstream输出;
std::copy(numbers.begin()、numbers.end()、std::ostream_迭代器(输出“”);
std::cout通常,如果您有一个T
的容器,并且希望从T
的容器创建一个U
的容器,正如其他人提到的,要查找的算法是std::transform
如果你不使用C++ 11,这里是<代码> STD::变换< /Cord>用法:
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <iterator>
#include <sstream>
std::string Transformer(double d)
{
std::ostringstream doubleStr;
doubleStr << d;
return doubleStr.str();
}
int main()
{
std::vector<double> doubleVec;
doubleVec.push_back(1.0);
doubleVec.push_back(2.1);
doubleVec.push_back(3.2);
std::vector<std::string> doubleStr;
std::transform(doubleVec.begin(), doubleVec.end(), std::back_inserter(doubleStr), Transformer);
std::copy(doubleStr.begin(), doubleStr.end(), std::ostream_iterator<std::string>(std::cout, " "));
}
#包括
#包括
#包括
#包括
#包括
#包括
串变压器(双d)
{
std::ostringstream-doubleStr;
doubleStr std::begin(doubleVec)、std::end(doubleVec)?这里不需要lambda。或者可能需要lambda,因为它是重载函数,否则需要一个丑陋的强制转换。@Neil Yes!(即使它不需要,它也向OP展示了如何在需要时调用它自己的转换函数)此外,它确保调用可以内联,并且不会通过函数指针。不会输出字符串向量。@NeilKirk:这一点很好。不过,OP还是将向量转换为字符串。如果这不是他想要的,我将删除这个答案。如果你不介意使用Boost,你可以使用这个:-1问题不是关于(甚至没有提到)OP以后的目标是什么,问题是什么。为什么你需要一个字符串向量?为什么不需要在需要时转换浮点?@阿尔夫,很抱歉,但这绝对是巧合。我考虑了另一个适合这个问题的答案。再次抱歉。
std::transform(doubleVec.begin(),
doubleVec.end(),
std::back_inserter(doubleStr),
my_to_string<double> );
std::vector<std::string> stringVec;
stringVec.reserve(v.size()); // reserve space for v.size() elements
#include <vector>
#include <iostream>
#include <sstream>
#include <iterator>
int main()
{
std::vector<double> numbers{1.0, 2.1, 3.2};
std::stringstream output;
std::copy(numbers.begin(), numbers.end(), std::ostream_iterator<double>(output, " "));
std::cout << output.str() << std::endl;
}
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <iterator>
#include <sstream>
std::string Transformer(double d)
{
std::ostringstream doubleStr;
doubleStr << d;
return doubleStr.str();
}
int main()
{
std::vector<double> doubleVec;
doubleVec.push_back(1.0);
doubleVec.push_back(2.1);
doubleVec.push_back(3.2);
std::vector<std::string> doubleStr;
std::transform(doubleVec.begin(), doubleVec.end(), std::back_inserter(doubleStr), Transformer);
std::copy(doubleStr.begin(), doubleStr.end(), std::ostream_iterator<std::string>(std::cout, " "));
}